Skip to content

Commit 7dffe1e

Browse files
committed
added check for sending multiple events
1 parent b0b092b commit 7dffe1e

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

src/Internal/Service/DispatcherService.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@ public function dispatch(EventInterface $event): void
2727

2828
public function flush(): void
2929
{
30-
foreach (array_keys($this->events) as $event) {
30+
foreach ($this->events as $event => $value) {
3131
$this->dispatcher->flush($event);
3232
}
33+
34+
$this->dispatcher->forgetPushed();
35+
$this->events = [];
3336
}
3437

3538
public function forgot(): void
3639
{
37-
foreach (array_keys($this->events) as $event) {
40+
foreach ($this->events as $event => $value) {
3841
$this->dispatcher->forget($event);
3942
}
43+
44+
$this->events = [];
4045
}
4146
}

src/Services/TransactionService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function apply(array $wallets, array $objects): array
6262
{
6363
$transactions = $this->atmService->makeTransactions($objects); // q1
6464
$totals = $this->assistantService->getSums($objects);
65+
assert(count($objects) === count($transactions));
6566

6667
foreach ($totals as $walletId => $total) {
6768
$wallet = $wallets[$walletId] ?? null;

tests/Units/Domain/EventTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
1313
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
1414
use Bavix\Wallet\Models\Transaction;
15+
use Bavix\Wallet\Objects\Cart;
16+
use Bavix\Wallet\Services\PurchaseServiceInterface;
1517
use Bavix\Wallet\Test\Infra\Exceptions\UnknownEventException;
1618
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
19+
use Bavix\Wallet\Test\Infra\Factories\ItemFactory;
1720
use Bavix\Wallet\Test\Infra\Listeners\BalanceUpdatedThrowDateListener;
1821
use Bavix\Wallet\Test\Infra\Listeners\BalanceUpdatedThrowIdListener;
1922
use Bavix\Wallet\Test\Infra\Listeners\BalanceUpdatedThrowUuidListener;
@@ -162,4 +165,67 @@ public function testTransactionCreatedThrowListener(): void
162165

163166
$buyer->deposit(100);
164167
}
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+
}
165231
}

0 commit comments

Comments
 (0)