Skip to content

Commit

Permalink
ensure version on event reconstitution
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Oct 4, 2019
1 parent 7b69a90 commit 61b1dc2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Event/AbstractAggregateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Gears\Event\Time\SystemTimeProvider;
use Gears\Event\Time\TimeProvider;
use Gears\EventSourcing\Aggregate\AggregateVersion;
use Gears\EventSourcing\Event\Exception\AggregateEventException;
use Gears\Identity\Identity;

/**
Expand Down Expand Up @@ -61,11 +62,26 @@ final protected static function occurred(Identity $aggregateId, array $payload,
/**
* {@inheritdoc}
*
* @throws AggregateEventException
*
* @return mixed|self
*/
final public static function reconstitute(array $payload, \DateTimeImmutable $createdAt, array $attributes = [])
{
$event = new static($attributes['aggregateId'], $payload, $createdAt);

if (!isset($attributes['aggregateVersion'])
|| !$attributes['aggregateVersion'] instanceof AggregateVersion
|| (new AggregateVersion(0))->isEqualTo($attributes['aggregateVersion'])
) {
throw new AggregateEventException(\sprintf(
'Invalid aggregate version, "%s" given',
$attributes['aggregateVersion'] instanceof AggregateVersion
? $attributes['aggregateVersion']->getValue()
: \gettype($attributes['aggregateVersion'])
));
}

$event->version = $attributes['aggregateVersion'];

if (isset($attributes['metadata'])) {
Expand Down
16 changes: 16 additions & 0 deletions src/Event/AbstractEmptyAggregateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Gears\Event\Time\SystemTimeProvider;
use Gears\Event\Time\TimeProvider;
use Gears\EventSourcing\Aggregate\AggregateVersion;
use Gears\EventSourcing\Event\Exception\AggregateEventException;
use Gears\Identity\Identity;

/**
Expand Down Expand Up @@ -58,13 +59,28 @@ final protected static function occurred(Identity $aggregateId, ?TimeProvider $t
/**
* {@inheritdoc}
*
* @throws AggregateEventException
*
* @return mixed|self
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
final public static function reconstitute(array $payload, \DateTimeImmutable $createdAt, array $attributes = [])
{
$event = new static($attributes['aggregateId'], $createdAt);

if (!isset($attributes['aggregateVersion'])
|| !$attributes['aggregateVersion'] instanceof AggregateVersion
|| (new AggregateVersion(0))->isEqualTo($attributes['aggregateVersion'])
) {
throw new AggregateEventException(\sprintf(
'Invalid aggregate version, "%s" given',
$attributes['aggregateVersion'] instanceof AggregateVersion
? $attributes['aggregateVersion']->getValue()
: \gettype($attributes['aggregateVersion'])
));
}

$event->version = $attributes['aggregateVersion'];

if (isset($attributes['metadata'])) {
Expand Down
17 changes: 17 additions & 0 deletions tests/EventSourcing/Event/AbstractAggregateEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Gears\EventSourcing\Tests\Event;

use Gears\EventSourcing\Aggregate\AggregateVersion;
use Gears\EventSourcing\Event\Exception\AggregateEventException;
use Gears\EventSourcing\Tests\Stub\AbstractAggregateEventStub;
use Gears\Identity\UuidIdentity;
use PHPUnit\Framework\TestCase;
Expand All @@ -32,6 +33,22 @@ public function testCreation(): void
static::assertLessThanOrEqual(new \DateTimeImmutable('now'), $aggregateEvent->getCreatedAt());
}

public function testReconstitutionInvalidVersion(): void
{
$this->expectException(AggregateEventException::class);
$this->expectExceptionMessage('Invalid aggregate version, "0" given');

AbstractAggregateEventStub::reconstitute(
[],
new \DateTimeImmutable('now'),
[
'aggregateId' => UuidIdentity::fromString('3247cb6e-e9c7-4f3a-9c6c-0dec26a0353f'),
'aggregateVersion' => new AggregateVersion(0),
'metadata' => ['userId' => '123456'],
]
);
}

public function testReconstitution(): void
{
$identity = UuidIdentity::fromString('3247cb6e-e9c7-4f3a-9c6c-0dec26a0353f');
Expand Down
17 changes: 17 additions & 0 deletions tests/EventSourcing/Event/AbstractEmptyAggregateEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Gears\EventSourcing\Tests\Event;

use Gears\EventSourcing\Aggregate\AggregateVersion;
use Gears\EventSourcing\Event\Exception\AggregateEventException;
use Gears\EventSourcing\Tests\Stub\AbstractEmptyAggregateEventStub;
use Gears\Identity\UuidIdentity;
use PHPUnit\Framework\TestCase;
Expand All @@ -32,6 +33,22 @@ public function testCreation(): void
static::assertLessThanOrEqual(new \DateTimeImmutable('now'), $aggregateEvent->getCreatedAt());
}

public function testReconstitutionInvalidVersion(): void
{
$this->expectException(AggregateEventException::class);
$this->expectExceptionMessage('Invalid aggregate version, "0" given');

AbstractEmptyAggregateEventStub::reconstitute(
[],
new \DateTimeImmutable('now'),
[
'aggregateId' => UuidIdentity::fromString('3247cb6e-e9c7-4f3a-9c6c-0dec26a0353f'),
'aggregateVersion' => new AggregateVersion(0),
'metadata' => ['userId' => '123456'],
]
);
}

public function testReconstitution(): void
{
$identity = UuidIdentity::fromString('3247cb6e-e9c7-4f3a-9c6c-0dec26a0353f');
Expand Down

0 comments on commit 61b1dc2

Please sign in to comment.