@@ -67,36 +67,63 @@ public function testBlockIter3(): void
6767 self ::assertSame ($ iterations * 2000 , $ user ->balanceInt );
6868 }
6969
70+ /**
71+ * Tests the rollback functionality of the AtomicService.
72+ *
73+ * This test creates a new Buyer and deposits 1000 units into their wallet. Then, it attempts to
74+ * withdraw 3000 units from the wallet within an atomic block. Since there are not enough funds,
75+ * an exception is thrown. The test then checks that the balance of the wallet has not changed.
76+ *
77+ * @return void
78+ */
7079 public function testRollback (): void
7180 {
81+ // Create a new instance of the AtomicService
7282 $ atomic = app (AtomicServiceInterface::class);
73-
83+
84+ // Create a new Buyer and deposit 1000 units into their wallet
7485 /** @var Buyer $user */
7586 $ user = BuyerFactory::new ()->create ();
76-
7787 $ user ->deposit (1000 );
7888
79- self ::assertSame (1000 , $ user ->balanceInt );
89+ // Check that the balance of the wallet is 1000 units
90+ $ this ->assertSame (1000 , $ user ->balanceInt );
8091
8192 try {
93+ // Start an atomic block and attempt to withdraw 3000 units from the wallet
8294 $ atomic ->block ($ user , function () use ($ user ) {
95+ // Withdraw 1000 units from the wallet
8396 $ user ->forceWithdraw (1000 );
97+ // Withdraw 1000 units from the wallet
8498 $ user ->forceWithdraw (1000 );
99+ // Withdraw 1000 units from the wallet
85100 $ user ->forceWithdraw (1000 );
101+ // Deposit 5000 units into the wallet
86102 $ user ->deposit (5000 );
87103
104+ // Throw an exception to simulate an error
88105 throw new \Exception ();
89106 });
90107
91- self ::assertTrue (false ); // check
92- } catch (\Throwable ) {
108+ // This should not be reached
109+ $ this ->assertTrue (false ); // check
110+ } catch (\Throwable $ e ) {
111+ // Intentionally left empty
93112 }
94113
95- self ::assertTrue ($ user ->wallet ->refreshBalance ()); // check
114+ // Refresh the balance of the wallet to ensure it has not changed
115+ $ this ->assertTrue ($ user ->wallet ->refreshBalance ()); // check
116+
117+ // Retrieve the Buyer from the database and check that the balance is still 1000 units
96118
119+ /**
120+ * @var Buyer $userFromDb
121+ */
97122 $ userFromDb = Buyer::find ($ user ->getKey ());
98123
99- self ::assertSame (1000 , $ userFromDb ->balanceInt );
100- self ::assertSame (1000 , $ user ->balanceInt );
124+ // Check that the balance of the wallet is 1000 units
125+ $ this ->assertSame (1000 , $ userFromDb ->balanceInt );
126+ // Check that the balance of the wallet is 1000 units
127+ $ this ->assertSame (1000 , $ user ->balanceInt );
101128 }
102129}
0 commit comments