From c346098f9397fc7b76c0a9cb9c3b8341a1d43c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20K=C3=BCttel?= Date: Sat, 27 Apr 2024 19:42:00 +0200 Subject: [PATCH] test(transaction): add some test cases for nested transactions Test whether we can start two nested transactions ... 1. ... complete them and see the result in the database 2. ... rollback the outer one after completing the inner one, and see that the result is _not_ in the database. 3. ... roll back the inner one and see if the result of the outer one is in the database after completing. --- .../Live/TransactionDBDebugTrueTest.php | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tests/system/Database/Live/TransactionDBDebugTrueTest.php b/tests/system/Database/Live/TransactionDBDebugTrueTest.php index e9a39847b939..a44ffa5c954b 100644 --- a/tests/system/Database/Live/TransactionDBDebugTrueTest.php +++ b/tests/system/Database/Live/TransactionDBDebugTrueTest.php @@ -234,4 +234,90 @@ public function testTransBegin(): void $this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']); } + + public function testNestedTransactions(): void + { + $builder = $this->db->table('job'); + + $this->db->transStart() or $this->fail('Failed to start transaction'); + + $jobData = [ + 'name' => 'Grocery Sales', + 'description' => 'Fresh!', + ]; + $builder->insert($jobData); + + $this->db->transStart() or $this->fail('Failed to start inner transaction'); + + $jobData = [ + 'name' => 'Comedian', + 'description' => 'Theres something in your teeth', + ]; + $builder->insert($jobData); + + $this->db->transComplete() or $this->fail('Failed to complete inner transaction'); + + $this->db->transComplete() or $this->fail('Failed to complete outer transaction'); + + $this->seeInDatabase('job', ['name' => 'Grocery Sales']); + $this->seeInDatabase('job', ['name' => 'Comedian']); + } + + + public function testNestedTransactionsRollbackOuter(): void + { + $builder = $this->db->table('job'); + + $this->db->transStart() or $this->fail('Failed to start transaction'); + + $jobData = [ + 'name' => 'Grocery Sales', + 'description' => 'Fresh!', + ]; + $builder->insert($jobData); + + $this->db->transStart() or $this->fail('Failed to start inner transaction'); + + $jobData = [ + 'name' => 'Comedian', + 'description' => 'Theres something in your teeth', + ]; + $builder->insert($jobData); + + $this->db->transComplete() or $this->fail('Failed to complete inner transaction'); + + $this->db->transRollback() or $this->fail('Failed to rollback outer transaction'); + + $this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']); + $this->dontSeeInDatabase('job', ['name' => 'Comedian']); + } + + + public function testNestedTransactionsRollbackInner(): void + { + $builder = $this->db->table('job'); + + $this->db->transStart() or $this->fail('Failed to start transaction'); + + $jobData = [ + 'name' => 'Grocery Sales', + 'description' => 'Fresh!', + ]; + $builder->insert($jobData); + + $this->db->transStart() or $this->fail('Failed to start inner transaction'); + + $jobData = [ + 'name' => 'Comedian', + 'description' => 'Theres something in your teeth', + ]; + $builder->insert($jobData); + + $this->db->transRollback() or $this->fail('Failed to rollback inner transaction'); + + $this->db->transComplete() or $this->fail('Failed to complete outer transaction'); + + $this->seeInDatabase('job', ['name' => 'Grocery Sales']); + $this->dontSeeInDatabase('job', ['name' => 'Comedian']); + } }