Skip to content

Commit 1c4cd46

Browse files
committed
update units
1 parent 8ecf1b6 commit 1c4cd46

File tree

2 files changed

+132
-81
lines changed

2 files changed

+132
-81
lines changed

tests/Units/Domain/BalanceTest.php

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Bavix\Wallet\Test\Units\Domain;
66

77
use function app;
8-
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
98
use Bavix\Wallet\Models\Wallet;
109
use Bavix\Wallet\Services\BookkeeperServiceInterface;
1110
use Bavix\Wallet\Services\RegulatorServiceInterface;
@@ -183,84 +182,4 @@ public function testEqualWallet(): void
183182
self::assertSame($wallet->getKey(), $wallet->wallet->wallet->getKey());
184183
self::assertSame($wallet->getKey(), $wallet->wallet->wallet->wallet->getKey());
185184
}
186-
187-
/**
188-
* @see https://github.com/bavix/laravel-wallet/issues/49
189-
*/
190-
public function testForceUpdate(): void
191-
{
192-
/** @var Buyer $buyer */
193-
$buyer = BuyerFactory::new()->create();
194-
$wallet = $buyer->wallet;
195-
196-
self::assertSame(0, $wallet->balanceInt);
197-
198-
$wallet->deposit(1000);
199-
self::assertSame(1000, $wallet->balanceInt);
200-
self::assertSame(0, (int) app(RegulatorServiceInterface::class)->diff($wallet));
201-
202-
Wallet::whereKey($buyer->wallet->getKey())
203-
->update(['balance' => 10])
204-
;
205-
206-
/**
207-
* Create a state when the cache is empty.
208-
* For example, something went wrong and your database has incorrect data.
209-
* Unfortunately, the library will work with what is.
210-
* But there is an opportunity to recount the balance.
211-
*
212-
* Here is an example:
213-
*/
214-
app(BookkeeperServiceInterface::class)->missing($buyer->wallet);
215-
self::assertSame(1000, (int) $wallet->getRawOriginal('balance'));
216-
217-
/**
218-
* We load the model from the base and our balance is 10.
219-
*/
220-
$wallet->refresh();
221-
self::assertSame(10, $wallet->balanceInt);
222-
self::assertSame(10, (int) $wallet->getRawOriginal('balance'));
223-
224-
/**
225-
* Now we fill the cache with relevant data (PS, the data inside the model will be updated).
226-
*/
227-
$wallet->refreshBalance();
228-
self::assertSame(1000, $wallet->balanceInt);
229-
self::assertSame(1000, (int) $wallet->getRawOriginal('balance'));
230-
}
231-
232-
public function testTransactionRollback(): void
233-
{
234-
/** @var Buyer $buyer */
235-
$buyer = BuyerFactory::new()->create();
236-
self::assertFalse($buyer->relationLoaded('wallet'));
237-
$wallet = $buyer->wallet;
238-
239-
self::assertFalse($wallet->exists);
240-
self::assertSame(0, $wallet->balanceInt);
241-
self::assertTrue($wallet->exists);
242-
243-
$bookkeeper = app(BookkeeperServiceInterface::class);
244-
$regulator = app(RegulatorServiceInterface::class);
245-
246-
$wallet->deposit(1000);
247-
self::assertSame(0, (int) $regulator->diff($wallet));
248-
self::assertSame(1000, (int) $regulator->amount($wallet));
249-
self::assertSame(1000, (int) $bookkeeper->amount($wallet));
250-
self::assertSame(1000, $wallet->balanceInt);
251-
252-
app(DatabaseServiceInterface::class)->transaction(function () use ($wallet, $regulator, $bookkeeper) {
253-
$wallet->deposit(10000);
254-
self::assertSame(10000, (int) $regulator->diff($wallet));
255-
self::assertSame(11000, (int) $regulator->amount($wallet));
256-
self::assertSame(1000, (int) $bookkeeper->amount($wallet));
257-
258-
return false; // rollback
259-
});
260-
261-
self::assertSame(0, (int) $regulator->diff($wallet));
262-
self::assertSame(1000, (int) $regulator->amount($wallet));
263-
self::assertSame(1000, (int) $bookkeeper->amount($wallet));
264-
self::assertSame(1000, $wallet->balanceInt);
265-
}
266185
}

tests/Units/Domain/StateTest.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Test\Units\Domain;
6+
7+
use function app;
8+
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
9+
use Bavix\Wallet\Models\Wallet;
10+
use Bavix\Wallet\Services\BookkeeperServiceInterface;
11+
use Bavix\Wallet\Services\RegulatorServiceInterface;
12+
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
13+
use Bavix\Wallet\Test\Infra\Models\Buyer;
14+
use Bavix\Wallet\Test\Infra\TestCase;
15+
16+
/**
17+
* @internal
18+
*/
19+
class StateTest extends TestCase
20+
{
21+
/**
22+
* @see https://github.com/bavix/laravel-wallet/issues/49
23+
*/
24+
public function testForceUpdate(): void
25+
{
26+
/** @var Buyer $buyer */
27+
$buyer = BuyerFactory::new()->create();
28+
$wallet = $buyer->wallet;
29+
30+
self::assertSame(0, $wallet->balanceInt);
31+
32+
$wallet->deposit(1000);
33+
self::assertSame(1000, $wallet->balanceInt);
34+
self::assertSame(0, (int) app(RegulatorServiceInterface::class)->diff($wallet));
35+
36+
Wallet::whereKey($buyer->wallet->getKey())
37+
->update(['balance' => 10])
38+
;
39+
40+
/**
41+
* Create a state when the cache is empty.
42+
* For example, something went wrong and your database has incorrect data.
43+
* Unfortunately, the library will work with what is.
44+
* But there is an opportunity to recount the balance.
45+
*
46+
* Here is an example:
47+
*/
48+
app(BookkeeperServiceInterface::class)->missing($buyer->wallet);
49+
self::assertSame(1000, (int) $wallet->getRawOriginal('balance'));
50+
51+
/**
52+
* We load the model from the base and our balance is 10.
53+
*/
54+
$wallet->refresh();
55+
self::assertSame(10, $wallet->balanceInt);
56+
self::assertSame(10, (int) $wallet->getRawOriginal('balance'));
57+
58+
/**
59+
* Now we fill the cache with relevant data (PS, the data inside the model will be updated).
60+
*/
61+
$wallet->refreshBalance();
62+
self::assertSame(1000, $wallet->balanceInt);
63+
self::assertSame(1000, (int) $wallet->getRawOriginal('balance'));
64+
}
65+
66+
public function testTransactionRollback(): void
67+
{
68+
/** @var Buyer $buyer */
69+
$buyer = BuyerFactory::new()->create();
70+
self::assertFalse($buyer->relationLoaded('wallet'));
71+
$wallet = $buyer->wallet;
72+
73+
self::assertFalse($wallet->exists);
74+
self::assertSame(0, $wallet->balanceInt);
75+
self::assertTrue($wallet->exists);
76+
77+
$bookkeeper = app(BookkeeperServiceInterface::class);
78+
$regulator = app(RegulatorServiceInterface::class);
79+
80+
$wallet->deposit(1000);
81+
self::assertSame(0, (int) $regulator->diff($wallet));
82+
self::assertSame(1000, (int) $regulator->amount($wallet));
83+
self::assertSame(1000, (int) $bookkeeper->amount($wallet));
84+
self::assertSame(1000, $wallet->balanceInt);
85+
86+
app(DatabaseServiceInterface::class)->transaction(function () use ($wallet, $regulator, $bookkeeper) {
87+
$wallet->deposit(10000);
88+
self::assertSame(10000, (int) $regulator->diff($wallet));
89+
self::assertSame(11000, (int) $regulator->amount($wallet));
90+
self::assertSame(1000, (int) $bookkeeper->amount($wallet));
91+
92+
return false; // rollback
93+
});
94+
95+
self::assertSame(0, (int) $regulator->diff($wallet));
96+
self::assertSame(1000, (int) $regulator->amount($wallet));
97+
self::assertSame(1000, (int) $bookkeeper->amount($wallet));
98+
self::assertSame(1000, $wallet->balanceInt);
99+
}
100+
101+
public function testRefreshInTransaction(): void
102+
{
103+
/** @var Buyer $buyer */
104+
$buyer = BuyerFactory::new()->create();
105+
$buyer->deposit(10000);
106+
107+
$bookkeeper = app(BookkeeperServiceInterface::class);
108+
$regulator = app(RegulatorServiceInterface::class);
109+
110+
$bookkeeper->increase($buyer->wallet, 100);
111+
self::assertSame(10100, $buyer->balanceInt);
112+
113+
app(DatabaseServiceInterface::class)->transaction(function () use ($bookkeeper, $regulator, $buyer) {
114+
self::assertTrue($buyer->wallet->refreshBalance());
115+
self::assertSame(-100, (int) $regulator->diff($buyer->wallet));
116+
self::assertSame(10100, (int) $bookkeeper->amount($buyer->wallet));
117+
self::assertSame(10000, $buyer->balanceInt); // bookkeeper.amount+regulator.diff
118+
119+
return false; // rollback. cancel refreshBalance
120+
});
121+
122+
self::assertSame(0, (int) $regulator->diff($buyer->wallet));
123+
self::assertSame(10100, (int) $bookkeeper->amount($buyer->wallet));
124+
self::assertSame(10100, $buyer->balanceInt);
125+
126+
self::assertTrue($buyer->wallet->refreshBalance());
127+
128+
self::assertSame(0, (int) $regulator->diff($buyer->wallet));
129+
self::assertSame(10000, (int) $bookkeeper->amount($buyer->wallet));
130+
self::assertSame(10000, $buyer->balanceInt);
131+
}
132+
}

0 commit comments

Comments
 (0)