|
6 | 6 |
|
7 | 7 | use Bavix\Wallet\Exceptions\AmountInvalid; |
8 | 8 | use Bavix\Wallet\Exceptions\BalanceIsEmpty; |
| 9 | +use Bavix\Wallet\Exceptions\InsufficientFunds; |
9 | 10 | use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; |
10 | 11 | use Bavix\Wallet\Internal\Exceptions\ModelNotFoundException; |
| 12 | +use Bavix\Wallet\Internal\Service\DatabaseServiceInterface; |
11 | 13 | use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface; |
12 | 14 | use Bavix\Wallet\Models\Transaction; |
13 | 15 | use Bavix\Wallet\Models\Transfer; |
|
17 | 19 | use Bavix\Wallet\Test\Infra\Models\Item; |
18 | 20 | use Bavix\Wallet\Test\Infra\Models\UserCashier; |
19 | 21 | use Bavix\Wallet\Test\Infra\Models\UserMulti; |
| 22 | +use Bavix\Wallet\Test\Infra\PackageModels\Wallet; |
20 | 23 | use Bavix\Wallet\Test\Infra\TestCase; |
21 | 24 | use function compact; |
22 | 25 | use Illuminate\Database\QueryException; |
@@ -522,4 +525,44 @@ public function testDecimalPlaces(): void |
522 | 525 | $user->deposit(1_000_000_000); |
523 | 526 | self::assertSame(1000., (float) $wallet->balanceFloat); |
524 | 527 | } |
| 528 | + |
| 529 | + public function testMultiWalletTransactionState(): void |
| 530 | + { |
| 531 | + /** @var UserMulti $user */ |
| 532 | + $user = UserMultiFactory::new()->create(); |
| 533 | + |
| 534 | + /** @var Wallet[] $wallets */ |
| 535 | + $wallets = []; |
| 536 | + foreach (range(1, 10) as $item) { |
| 537 | + $wallets[] = $user->createWallet(['name' => 'index'.$item]); |
| 538 | + } |
| 539 | + |
| 540 | + self::assertCount(10, $wallets); |
| 541 | + |
| 542 | + $funds = null; |
| 543 | + |
| 544 | + try { |
| 545 | + app(DatabaseServiceInterface::class)->transaction(function () use ($wallets) { |
| 546 | + foreach ($wallets as $key => $wallet) { |
| 547 | + $wallet->deposit(1000 + $key); // 1000 + [0...9] |
| 548 | + $wallet->withdraw(100); |
| 549 | + $wallet->deposit(50); |
| 550 | + |
| 551 | + self::assertSame(950 + $key, $wallet->balanceInt); |
| 552 | + } |
| 553 | + |
| 554 | + $wallet = reset($wallets); |
| 555 | + self::assertIsObject($wallet); |
| 556 | + |
| 557 | + $wallet->withdraw(1000); // failed |
| 558 | + }); |
| 559 | + } catch (InsufficientFunds $funds) { |
| 560 | + self::assertSame(ExceptionInterface::INSUFFICIENT_FUNDS, $funds->getCode()); |
| 561 | + } |
| 562 | + |
| 563 | + self::assertNotNull($funds); |
| 564 | + foreach ($wallets as $wallet) { |
| 565 | + self::assertSame(0, $wallet->balanceInt); |
| 566 | + } |
| 567 | + } |
525 | 568 | } |
0 commit comments