|
| 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