Skip to content

Commit

Permalink
Merge pull request #484 from damiantw/should-dispatch-fix
Browse files Browse the repository at this point in the history
Only Dispatch If Event Has Async Handler
  • Loading branch information
sebastiandedeyne committed Sep 20, 2024
2 parents 1accf4f + 0989efe commit cf90636
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/StoredEvents/StoredEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function shouldDispatchJob(): bool
/** @var \Spatie\EventSourcing\EventHandlers\EventHandlerCollection $eventHandlers */
$eventHandlers = Projectionist::allEventHandlers();

return $eventHandlers->asyncEventHandlers()->count() > 0;
return $eventHandlers->forEvent($this)->asyncEventHandlers()->isNotEmpty();
}

protected function instantiateEvent(?ShouldBeStored $originalEvent): void
Expand Down
28 changes: 28 additions & 0 deletions tests/EventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\EventSourcing\Tests;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
Expand All @@ -10,6 +11,7 @@
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertInstanceOf;

use Spatie\EventSourcing\EventHandlers\Projectors\Projector;
use Spatie\EventSourcing\Facades\Projectionist;
use Spatie\EventSourcing\StoredEvents\HandleStoredEventJob;
use Spatie\EventSourcing\StoredEvents\Models\EloquentStoredEvent;
Expand Down Expand Up @@ -191,3 +193,29 @@

Queue::assertPushedOn('testQueue', HandleStoredEventJob::class);
});

it('only queues event if event has async handler', function () {
Bus::fake();

$syncProjector = new class extends Projector {
public function onMoneyAddedEvent(MoneyAddedEvent $event)
{
}
};

$asyncProjector = new class extends Projector implements ShouldQueue {
public function onMoneySubtractedEvent(MoneySubtractedEvent $event)
{
}
};

Projectionist::addProjectors([$syncProjector, $asyncProjector]);

event(new MoneyAddedEvent($this->account, 1234));

Bus::assertNotDispatched(HandleStoredEventJob::class);

event(new MoneySubtractedEvent($this->account, 1234));

Bus::assertDispatched(HandleStoredEventJob::class);
});
4 changes: 4 additions & 0 deletions tests/TestClasses/Projectors/QueuedProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
namespace Spatie\EventSourcing\Tests\TestClasses\Projectors;

use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneyAddedEventWithQueueOverride;

class QueuedProjector extends BalanceProjector implements ShouldQueue
{
public function onMoneyAddedEventWithQueueOverride(MoneyAddedEventWithQueueOverride $event)
{
}
}

0 comments on commit cf90636

Please sign in to comment.