|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bavix\Wallet\Test\Units\Domain; |
| 6 | + |
| 7 | +use Bavix\Wallet\External\Dto\Extra; |
| 8 | +use Bavix\Wallet\External\Dto\Option; |
| 9 | +use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface; |
| 10 | +use Bavix\Wallet\Models\Transaction; |
| 11 | +use Bavix\Wallet\Test\Infra\Factories\BuyerFactory; |
| 12 | +use Bavix\Wallet\Test\Infra\Models\Buyer; |
| 13 | +use Bavix\Wallet\Test\Infra\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * @internal |
| 17 | + */ |
| 18 | +final class BlockTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @see https://github.com/bavix/laravel-wallet/issues/174 |
| 22 | + * @see https://github.com/bavix/laravel-wallet/issues/416 |
| 23 | + */ |
| 24 | + public function testBlockTransfer(): void |
| 25 | + { |
| 26 | + /** @var Buyer $buyer1 */ |
| 27 | + /** @var Buyer $buyer2 */ |
| 28 | + [$buyer1, $buyer2] = BuyerFactory::times(2)->create(); |
| 29 | + $uuidFactory = app(UuidFactoryServiceInterface::class); |
| 30 | + $idempotent = $uuidFactory->uuid4(); |
| 31 | + |
| 32 | + $transfer = $buyer1->forceTransfer($buyer2, 500, new Extra( |
| 33 | + deposit: new Option( |
| 34 | + [ |
| 35 | + 'type' => 'block', |
| 36 | + 'idempotent' => $idempotent, |
| 37 | + ], |
| 38 | + confirmed: false, |
| 39 | + ), |
| 40 | + withdraw: [ |
| 41 | + 'idempotent' => $idempotent, |
| 42 | + ], |
| 43 | + )); |
| 44 | + |
| 45 | + self::assertSame(-500, $buyer1->balanceInt); |
| 46 | + self::assertSame(0, $buyer2->balanceInt); |
| 47 | + |
| 48 | + self::assertTrue($transfer->from->is($buyer1->wallet)); |
| 49 | + self::assertTrue($transfer->to->is($buyer2->wallet)); |
| 50 | + |
| 51 | + self::assertSame(-500, $transfer->withdraw->amountInt); |
| 52 | + self::assertTrue($transfer->withdraw->confirmed); |
| 53 | + |
| 54 | + self::assertSame(500, $transfer->deposit->amountInt); |
| 55 | + self::assertFalse($transfer->deposit->confirmed); |
| 56 | + |
| 57 | + /** @var Transaction $transaction */ |
| 58 | + $transaction = $buyer2->wallet->walletTransactions() |
| 59 | + ->where('meta->type', 'block') |
| 60 | + ->where('confirmed', false) |
| 61 | + ->first() |
| 62 | + ; |
| 63 | + |
| 64 | + self::assertTrue($transfer->deposit->is($transaction)); |
| 65 | + self::assertTrue($buyer2->wallet->confirm($transaction)); |
| 66 | + } |
| 67 | +} |
0 commit comments