|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bavix\Wallet\Test\Units\Domain; |
| 6 | + |
| 7 | +use Bavix\Wallet\Interfaces\ProductLimitedInterface; |
| 8 | +use Bavix\Wallet\Interfaces\Wallet; |
| 9 | +use Bavix\Wallet\Models\Transfer; |
| 10 | +use Bavix\Wallet\Objects\Cart; |
| 11 | +use Bavix\Wallet\Services\PurchaseServiceInterface; |
| 12 | +use Bavix\Wallet\Test\Infra\Factories\BuyerFactory; |
| 13 | +use Bavix\Wallet\Test\Infra\Factories\ItemFactory; |
| 14 | +use Bavix\Wallet\Test\Infra\Factories\ItemMetaFactory; |
| 15 | +use Bavix\Wallet\Test\Infra\Models\Buyer; |
| 16 | +use Bavix\Wallet\Test\Infra\Models\Item; |
| 17 | +use Bavix\Wallet\Test\Infra\Models\ItemMeta; |
| 18 | +use Bavix\Wallet\Test\Infra\PackageModels\Transaction; |
| 19 | +use Bavix\Wallet\Test\Infra\TestCase; |
| 20 | +use Bavix\Wallet\Traits\HasWallets; |
| 21 | +use function count; |
| 22 | +use Illuminate\Database\Eloquent\Collection; |
| 23 | + |
| 24 | +/** |
| 25 | + * @internal |
| 26 | + */ |
| 27 | +final class CartReceivingTest extends TestCase |
| 28 | +{ |
| 29 | + public function testCartMeta(): void |
| 30 | + { |
| 31 | + /** @var Buyer $buyer */ |
| 32 | + $buyer = BuyerFactory::new()->create(); |
| 33 | + /** @var ItemMeta $product */ |
| 34 | + $product = ItemMetaFactory::new()->create([ |
| 35 | + 'quantity' => 1, |
| 36 | + ]); |
| 37 | + |
| 38 | + $expected = 'pay'; |
| 39 | + |
| 40 | + $payment = $buyer->createWallet([ |
| 41 | + 'name' => 'Dollar', |
| 42 | + 'meta' => [ |
| 43 | + 'currency' => 'USD', |
| 44 | + ], |
| 45 | + ]); |
| 46 | + |
| 47 | + $receiving = $product->createWallet([ |
| 48 | + 'name' => 'Dollar', |
| 49 | + 'meta' => [ |
| 50 | + 'currency' => 'USD', |
| 51 | + ], |
| 52 | + ]); |
| 53 | + |
| 54 | + $cart = app(Cart::class) |
| 55 | + ->withItem($product, receiving: $receiving) |
| 56 | + ->withMeta([ |
| 57 | + 'type' => $expected, |
| 58 | + ]) |
| 59 | + ; |
| 60 | + |
| 61 | + $amount = $cart->getTotal($buyer); |
| 62 | + |
| 63 | + self::assertSame(0, $buyer->balanceInt); |
| 64 | + self::assertNotNull($payment->deposit($amount)); |
| 65 | + |
| 66 | + $transfers = $payment->payCart($cart); |
| 67 | + self::assertCount(1, $transfers); |
| 68 | + |
| 69 | + $transfer = current($transfers); |
| 70 | + |
| 71 | + /** @var Transaction[] $transactions */ |
| 72 | + $transactions = [$transfer->deposit, $transfer->withdraw]; |
| 73 | + foreach ($transactions as $transaction) { |
| 74 | + self::assertSame($product->price, $transaction->meta['price']); |
| 75 | + self::assertSame($product->name, $transaction->meta['name']); |
| 76 | + self::assertSame($expected, $transaction->meta['type']); |
| 77 | + } |
| 78 | + |
| 79 | + self::assertSame((int) $amount, $receiving->balanceInt); |
| 80 | + self::assertSame('USD', $receiving->currency); |
| 81 | + |
| 82 | + self::assertSame(0, $payment->balanceInt); |
| 83 | + self::assertSame('USD', $payment->currency); |
| 84 | + } |
| 85 | + |
| 86 | + public function testPay(): void |
| 87 | + { |
| 88 | + /** @var Buyer $buyer */ |
| 89 | + $buyer = BuyerFactory::new()->create(); |
| 90 | + /** @var Collection<int, Item> $products */ |
| 91 | + $products = ItemFactory::times(10)->create([ |
| 92 | + 'quantity' => 1, |
| 93 | + ]); |
| 94 | + |
| 95 | + $cart = app(Cart::class); |
| 96 | + foreach ($products as $product) { |
| 97 | + $receiving = $product->createWallet([ |
| 98 | + 'name' => 'Dollar', |
| 99 | + 'meta' => [ |
| 100 | + 'currency' => 'USD', |
| 101 | + ], |
| 102 | + ]); |
| 103 | + |
| 104 | + $cart = $cart->withItem($product, pricePerItem: 1, receiving: $receiving); |
| 105 | + } |
| 106 | + |
| 107 | + self::assertCount(10, $cart->getItems()); |
| 108 | + |
| 109 | + foreach ($cart->getItems() as $product) { |
| 110 | + self::assertSame(0, $product->getWallet('dollar')?->balanceInt); |
| 111 | + } |
| 112 | + |
| 113 | + $payment = $buyer->createWallet([ |
| 114 | + 'name' => 'Dollar', |
| 115 | + 'meta' => [ |
| 116 | + 'currency' => 'USD', |
| 117 | + ], |
| 118 | + ]); |
| 119 | + |
| 120 | + $payment->deposit($cart->getTotal($buyer)); |
| 121 | + |
| 122 | + self::assertSame(10, $payment->balanceInt); |
| 123 | + |
| 124 | + $transfers = $payment->payCart($cart); |
| 125 | + |
| 126 | + self::assertCount(count($cart), $transfers); |
| 127 | + self::assertTrue((bool) app(PurchaseServiceInterface::class)->already($payment, $cart->getBasketDto())); |
| 128 | + self::assertSame(0, $payment->balanceInt); |
| 129 | + |
| 130 | + foreach ($transfers as $transfer) { |
| 131 | + self::assertSame(Transfer::STATUS_PAID, $transfer->status); |
| 132 | + self::assertNull($transfer->status_last); |
| 133 | + } |
| 134 | + |
| 135 | + foreach ($cart->getItems() as $product) { |
| 136 | + /** @var Item $product */ |
| 137 | + self::assertSame(1, $product->getWallet('dollar')?->balanceInt); |
| 138 | + } |
| 139 | + |
| 140 | + self::assertTrue($payment->refundCart($cart)); |
| 141 | + foreach ($transfers as $transfer) { |
| 142 | + $transfer->refresh(); |
| 143 | + self::assertSame(Transfer::STATUS_REFUND, $transfer->status); |
| 144 | + self::assertSame(Transfer::STATUS_PAID, $transfer->status_last); |
| 145 | + } |
| 146 | + |
| 147 | + self::assertSame(10, $payment->balanceInt); |
| 148 | + } |
| 149 | +} |
0 commit comments