| 
12 | 12 | use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;  | 
13 | 13 | use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;  | 
14 | 14 | use Bavix\Wallet\Models\Transaction;  | 
 | 15 | +use Bavix\Wallet\Objects\Cart;  | 
 | 16 | +use Bavix\Wallet\Services\PurchaseServiceInterface;  | 
15 | 17 | use Bavix\Wallet\Test\Infra\Exceptions\UnknownEventException;  | 
16 | 18 | use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;  | 
 | 19 | +use Bavix\Wallet\Test\Infra\Factories\ItemFactory;  | 
17 | 20 | use Bavix\Wallet\Test\Infra\Listeners\BalanceUpdatedThrowDateListener;  | 
18 | 21 | use Bavix\Wallet\Test\Infra\Listeners\BalanceUpdatedThrowIdListener;  | 
19 | 22 | use Bavix\Wallet\Test\Infra\Listeners\BalanceUpdatedThrowUuidListener;  | 
@@ -162,4 +165,67 @@ public function testTransactionCreatedThrowListener(): void  | 
162 | 165 | 
 
  | 
163 | 166 |         $buyer->deposit(100);  | 
164 | 167 |     }  | 
 | 168 | + | 
 | 169 | +    /**  | 
 | 170 | +     * @throws ExceptionInterface  | 
 | 171 | +     */  | 
 | 172 | +    public function testTransactionCreatedMultiListener(): void  | 
 | 173 | +    {  | 
 | 174 | +        /** @var array<array<int, int>> $transactionIds */  | 
 | 175 | +        /** @var array<array<int, int>> $transactionCounts */  | 
 | 176 | +        $transactionIds = [];  | 
 | 177 | +        $transactionCounts = [];  | 
 | 178 | +        Event::listen(  | 
 | 179 | +            TransactionCreatedEventInterface::class,  | 
 | 180 | +            static function (TransactionCreatedEventInterface $event) use (  | 
 | 181 | +                &$transactionIds,  | 
 | 182 | +                &$transactionCounts  | 
 | 183 | +            ): void {  | 
 | 184 | +                $transactionCounts[$event->getWalletId()] = ($transactionCounts[$event->getWalletId()] ?? 0) + 1;  | 
 | 185 | +                $transactionIds[$event->getWalletId()][] = $event->getId();  | 
 | 186 | +            },  | 
 | 187 | +        );  | 
 | 188 | + | 
 | 189 | +        /** @var Buyer $buyer */  | 
 | 190 | +        $buyer = BuyerFactory::new()->create();  | 
 | 191 | +        self::assertSame(0, $buyer->wallet->balanceInt);  | 
 | 192 | + | 
 | 193 | +        $products = ItemFactory::times(10)->create([  | 
 | 194 | +            'quantity' => 1,  | 
 | 195 | +        ]);  | 
 | 196 | + | 
 | 197 | +        $cart = app(Cart::class)->withItems($products);  | 
 | 198 | +        foreach ($cart->getItems() as $product) {  | 
 | 199 | +            self::assertSame(0, $product->getBalanceIntAttribute());  | 
 | 200 | +        }  | 
 | 201 | + | 
 | 202 | +        self::assertSame($buyer->balance, $buyer->wallet->balance);  | 
 | 203 | + | 
 | 204 | +        $depositTransaction = $buyer->deposit($cart->getTotal($buyer));  | 
 | 205 | +        self::assertNotNull($depositTransaction); // +1  | 
 | 206 | + | 
 | 207 | +        self::assertSame($buyer->balance, $buyer->wallet->balance);  | 
 | 208 | + | 
 | 209 | +        $transfers = $buyer->payCart($cart); // +10  | 
 | 210 | +        self::assertCount(count($cart), $transfers);  | 
 | 211 | +        self::assertTrue((bool) app(PurchaseServiceInterface::class)->already($buyer, $cart->getBasketDto()));  | 
 | 212 | +        self::assertSame(0, $buyer->balanceInt);  | 
 | 213 | + | 
 | 214 | +        $resultIds = [(int) $depositTransaction->getKey()];  | 
 | 215 | +        $valueIds = $transactionIds[$buyer->wallet->getKey()] ?? [];  | 
 | 216 | +        foreach ($transfers as $transfer) {  | 
 | 217 | +            $resultIds[] = (int) $transfer->withdraw->getKey();  | 
 | 218 | +            self::assertSame(1, $transactionCounts[$transfer->deposit->wallet_id] ?? 0);  | 
 | 219 | +        }  | 
 | 220 | + | 
 | 221 | +        sort($valueIds);  | 
 | 222 | +        sort($resultIds);  | 
 | 223 | + | 
 | 224 | +        self::assertSame(1 + 10, $transactionCounts[$buyer->wallet->getKey()] ?? 0);  | 
 | 225 | + | 
 | 226 | +        self::assertCount(1 + 10, $resultIds);  | 
 | 227 | +        self::assertCount(1 + 10, $valueIds);  | 
 | 228 | + | 
 | 229 | +        self::assertSame($valueIds, $resultIds);  | 
 | 230 | +    }  | 
165 | 231 | }  | 
0 commit comments