68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\Infra\Adapters\Events;
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
use Infrastructure\Adapters\Events\LaravelDomainEventDispatcher;
|
|
use Shared\DDD\Event as DomainEvent;
|
|
|
|
describe('LaravelDomainEventDispatcher', function (): void {
|
|
it('dispatches immediately when not buffering', function (): void {
|
|
// GIVEN
|
|
Event::fake();
|
|
$dispatcher = new LaravelDomainEventDispatcher();
|
|
$event = new class () implements DomainEvent
|
|
{
|
|
};
|
|
|
|
// WHEN
|
|
$dispatcher->dispatch($event);
|
|
|
|
// THEN
|
|
Event::assertDispatched($event::class, 1);
|
|
});
|
|
|
|
it('buffers events and dispatches them only on outermost flush', function (): void {
|
|
// GIVEN
|
|
Event::fake();
|
|
$dispatcher = new LaravelDomainEventDispatcher();
|
|
$event = new class () implements DomainEvent
|
|
{
|
|
};
|
|
|
|
// WHEN
|
|
$dispatcher->beginBuffering();
|
|
$dispatcher->beginBuffering();
|
|
$dispatcher->dispatch($event);
|
|
|
|
Event::assertNothingDispatched();
|
|
|
|
$dispatcher->flush();
|
|
Event::assertNothingDispatched();
|
|
|
|
$dispatcher->flush();
|
|
|
|
// THEN
|
|
Event::assertDispatched($event::class, 1);
|
|
});
|
|
|
|
it('discards buffered events without dispatching them', function (): void {
|
|
// GIVEN
|
|
Event::fake();
|
|
$dispatcher = new LaravelDomainEventDispatcher();
|
|
$event = new class () implements DomainEvent
|
|
{
|
|
};
|
|
|
|
// WHEN
|
|
$dispatcher->beginBuffering();
|
|
$dispatcher->dispatch($event);
|
|
$dispatcher->discard();
|
|
$dispatcher->flush();
|
|
|
|
// THEN
|
|
Event::assertNothingDispatched();
|
|
});
|
|
});
|