Skip to content

Commit

Permalink
Merge pull request #438 from 27pchrisl/fix-apply-types
Browse files Browse the repository at this point in the history
Added an extra rejection to prevent events being passed to invalid handlers
  • Loading branch information
sebastiandedeyne authored Sep 5, 2023
2 parents 6ba1fb0 + 503477b commit 83a06c8
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/AggregateRoots/AggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function handleCommand(object $command): static
->public()
->protected()
->reject(fn (Method $method) => in_array($method->getName(), ['handleCommand', 'recordThat', 'apply', 'tap']))
->reject(fn (Method $method) => $method->accepts(null))
->accepts($command)
->first()
) {
Expand All @@ -86,6 +87,7 @@ public function handleCommand(object $command): static
->public()
->protected()
->reject(fn (Method $method) => in_array($method->getName(), ['recordThat', 'apply', 'tap']))
->reject(fn (Method $method) => $method->accepts(null))
->accepts($command)
->first();

Expand Down Expand Up @@ -256,6 +258,7 @@ protected function apply(ShouldBeStored $event): void
->public()
->protected()
->reject(fn (Method $method) => in_array($method->getName(), ['handleCommand', 'recordThat', 'apply', 'tap']))
->reject(fn (Method $method) => $method->accepts(null))
->accepts($event)
->all()
->each(function (Method $method) use ($event) {
Expand Down
1 change: 1 addition & 0 deletions src/EventHandlers/AppliesEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private function applyStoredEvent(StoredEvent | ShouldBeStored $event)
->public()
->protected()
->reject(fn (Method $method) => in_array($method->getName(), ['apply', 'recordThat']))
->reject(fn (Method $method) => $method->accepts(null))
->accepts($event)
->all()
->each(
Expand Down
3 changes: 3 additions & 0 deletions src/EventHandlers/HandlesEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function handles(StoredEvent $storedEvent): bool
return Handlers::new($this)
->public()
->protected()
->reject(fn (Method $method) => $method->accepts(null))
->accepts($storedEvent->event)
->all()
->isNotEmpty();
Expand All @@ -28,6 +29,7 @@ public function handle(StoredEvent $storedEvent): void
Handlers::new($this)
->public()
->protected()
->reject(fn (Method $method) => $method->accepts(null))
->accepts($event)
->all()
->each(function (Method $method) use ($event) {
Expand All @@ -45,6 +47,7 @@ public function getEventHandlingMethods(): Collection
return Handlers::new($this)
->public()
->protected()
->reject(fn (Method $method) => $method->accepts(null))
->all()
->groupBy(fn (Method $method) => $method->getTypes()->first()?->getName())
->filter(function (Collection $group, string $key) {
Expand Down
8 changes: 8 additions & 0 deletions tests/AggregateRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\Reactors\DoubleBalanceReactor;
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\Reactors\SendMailReactor;
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyAdded;
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyIncremented;
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyMultiplied;
use Spatie\EventSourcing\Tests\TestClasses\FakeUuid;
use Spatie\EventSourcing\Tests\TestClasses\Models\Account;
Expand Down Expand Up @@ -246,6 +247,13 @@
assertEquals(123, $aggregateRoot->balance);
});

it('should apply to aggregate root event handlers using interfaces', function () {
$aggregateRoot = AccountAggregateRoot::retrieve($this->aggregateUuid);
$aggregateRoot->recordThat(new MoneyIncremented(123));

assertEquals(123, $aggregateRoot->balance);
});

it('can persist aggregate roots in a transaction', function () {
Mail::fake();

Expand Down
6 changes: 4 additions & 2 deletions tests/AggregateRoots/CommandHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
class AddItem
{
public function __construct(
#[AggregateUuid] public string $cartUuid,
#[AggregateUuid]
public string $cartUuid,
public string $name
) {
}
Expand All @@ -26,7 +27,8 @@ public function __construct(
class ClearCart
{
public function __construct(
#[AggregateUuid] public string $cartUuid
#[AggregateUuid]
public string $cartUuid
) {
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Commands/CommandBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
class AddItem
{
public function __construct(
#[AggregateUuid] public string $cartUuid,
#[AggregateUuid]
public string $cartUuid,
public string $name
) {
}
Expand Down
14 changes: 13 additions & 1 deletion tests/TestClasses/AggregateRoots/AccountAggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Spatie\EventSourcing\Tests\TestClasses\AggregateRoots;

use Exception;
use Spatie\EventSourcing\AggregateRoots\AggregateRoot;
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyAdded;
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyAddedInterface;
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyMultiplied;

class AccountAggregateRoot extends AggregateRoot
Expand Down Expand Up @@ -38,7 +40,7 @@ public function multiplyMoney(int $amount): self
return $this;
}

protected function applyMoneyAdded(MoneyAdded $event)
protected function applyMoneyAdded(MoneyAdded|MoneyAddedInterface $event)
{
$this->balance += $event->amount;
}
Expand All @@ -47,4 +49,14 @@ public function applyMoneyMultiplied(MoneyMultiplied $event)
{
$this->balance = $this->math->multiply($this->balance, $event->amount);
}

public function mixedMethod($param): void
{
throw new Exception("Method should not be called by apply()");
}

public function variadicMethod(...$param): void
{
throw new Exception("Method should not be called by apply()");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents;

interface MoneyAddedInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents;

use Spatie\EventSourcing\StoredEvents\ShouldBeStored;

class MoneyIncremented extends ShouldBeStored implements MoneyAddedInterface
{
public int $amount;

public function __construct(int $amount)
{
$this->amount = $amount;
}
}

0 comments on commit 83a06c8

Please sign in to comment.