From 11733389bb4e3d8b749ce42f5009732313d2a1ab Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Mon, 22 Jul 2024 13:55:03 +0300 Subject: [PATCH 1/3] add unit. Atomic rollback --- tests/Units/Service/AtomicServiceTest.php | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/Units/Service/AtomicServiceTest.php b/tests/Units/Service/AtomicServiceTest.php index 8c29cc82e..932c24797 100644 --- a/tests/Units/Service/AtomicServiceTest.php +++ b/tests/Units/Service/AtomicServiceTest.php @@ -66,4 +66,37 @@ public function testBlockIter3(): void self::assertSame($iterations * 2000, $user->balanceInt); } + + public function testRollback(): void + { + $atomic = app(AtomicServiceInterface::class); + + /** @var Buyer $user */ + $user = BuyerFactory::new()->create(); + + $user->deposit(1000); + + self::assertSame(1000, $user->balanceInt); + + try { + $atomic->block($user, function () use ($user) { + $user->forceWithdraw(1000); + $user->forceWithdraw(1000); + $user->forceWithdraw(1000); + $user->deposit(5000); + + throw new \Exception(); + }); + + self::assertTrue(false); // check + } catch (\Throwable) { + } + + self::assertTrue($user->wallet->refreshBalance()); // check + + $userFromDb = Buyer::find($user->getKey()); + + self::assertSame(1000, $userFromDb->balanceInt); + self::assertSame(1000, $user->balanceInt); + } } From 2e302cd439a65d0202864bacd20c42b66fa7472d Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Mon, 22 Jul 2024 19:06:21 +0300 Subject: [PATCH 2/3] add phpdocs --- tests/Units/Service/AtomicServiceTest.php | 43 ++++++++++++++++++----- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/tests/Units/Service/AtomicServiceTest.php b/tests/Units/Service/AtomicServiceTest.php index 932c24797..3d6f386e9 100644 --- a/tests/Units/Service/AtomicServiceTest.php +++ b/tests/Units/Service/AtomicServiceTest.php @@ -67,36 +67,63 @@ public function testBlockIter3(): void self::assertSame($iterations * 2000, $user->balanceInt); } + /** + * Tests the rollback functionality of the AtomicService. + * + * This test creates a new Buyer and deposits 1000 units into their wallet. Then, it attempts to + * withdraw 3000 units from the wallet within an atomic block. Since there are not enough funds, + * an exception is thrown. The test then checks that the balance of the wallet has not changed. + * + * @return void + */ public function testRollback(): void { + // Create a new instance of the AtomicService $atomic = app(AtomicServiceInterface::class); - + + // Create a new Buyer and deposit 1000 units into their wallet /** @var Buyer $user */ $user = BuyerFactory::new()->create(); - $user->deposit(1000); - self::assertSame(1000, $user->balanceInt); + // Check that the balance of the wallet is 1000 units + $this->assertSame(1000, $user->balanceInt); try { + // Start an atomic block and attempt to withdraw 3000 units from the wallet $atomic->block($user, function () use ($user) { + // Withdraw 1000 units from the wallet $user->forceWithdraw(1000); + // Withdraw 1000 units from the wallet $user->forceWithdraw(1000); + // Withdraw 1000 units from the wallet $user->forceWithdraw(1000); + // Deposit 5000 units into the wallet $user->deposit(5000); + // Throw an exception to simulate an error throw new \Exception(); }); - self::assertTrue(false); // check - } catch (\Throwable) { + // This should not be reached + $this->assertTrue(false); // check + } catch (\Throwable $e) { + // Intentionally left empty } - self::assertTrue($user->wallet->refreshBalance()); // check + // Refresh the balance of the wallet to ensure it has not changed + $this->assertTrue($user->wallet->refreshBalance()); // check + + // Retrieve the Buyer from the database and check that the balance is still 1000 units + /** + * @var Buyer $userFromDb + */ $userFromDb = Buyer::find($user->getKey()); - self::assertSame(1000, $userFromDb->balanceInt); - self::assertSame(1000, $user->balanceInt); + // Check that the balance of the wallet is 1000 units + $this->assertSame(1000, $userFromDb->balanceInt); + // Check that the balance of the wallet is 1000 units + $this->assertSame(1000, $user->balanceInt); } } From e42573d06c363d82d3eba1a6b978e018a474658c Mon Sep 17 00:00:00 2001 From: Github bot Date: Mon, 22 Jul 2024 16:09:42 +0000 Subject: [PATCH 3/3] autofix --- tests/Units/Service/AtomicServiceTest.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/Units/Service/AtomicServiceTest.php b/tests/Units/Service/AtomicServiceTest.php index 3d6f386e9..f16f5bfd4 100644 --- a/tests/Units/Service/AtomicServiceTest.php +++ b/tests/Units/Service/AtomicServiceTest.php @@ -73,8 +73,6 @@ public function testBlockIter3(): void * This test creates a new Buyer and deposits 1000 units into their wallet. Then, it attempts to * withdraw 3000 units from the wallet within an atomic block. Since there are not enough funds, * an exception is thrown. The test then checks that the balance of the wallet has not changed. - * - * @return void */ public function testRollback(): void { @@ -116,9 +114,7 @@ public function testRollback(): void // Retrieve the Buyer from the database and check that the balance is still 1000 units - /** - * @var Buyer $userFromDb - */ + /** @var Buyer $userFromDb */ $userFromDb = Buyer::find($user->getKey()); // Check that the balance of the wallet is 1000 units