Skip to content

Commit

Permalink
Merge 7dafe95 into c709369
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Sep 23, 2019
2 parents c709369 + 7dafe95 commit 771d12d
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/AggregateRootBehaviourWithRequiredHistory.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace EventSauce\EventSourcing;

use Generator;

trait AggregateRootBehaviourWithRequiredHistory
{
use AggregateRootBehaviour {
AggregateRootBehaviour::reconstituteFromEvents as private defaultAggregateRootReconstitute;
}

public static function reconstituteFromEvents(AggregateRootId $aggregateRootId, Generator $events): AggregateRoot
{
$aggregateRoot = static::defaultAggregateRootReconstitute($aggregateRootId, $events);

if ($aggregateRoot->aggregateRootVersion() === 0) {
throw new InvalidAggregateRootReconstitutionException();
}

return $aggregateRoot;
}
}
@@ -1,6 +1,6 @@
<?php

namespace EventSauce\EventSourcing\Integration\TestingAggregates;
namespace EventSauce\EventSourcing\Integration;

use EventSauce\EventSourcing\AggregateRootId;
use Ramsey\Uuid\Uuid;
Expand Down
@@ -0,0 +1,52 @@
<?php

namespace EventSauce\EventSourcing\Integration\RequiringHistoryWithAggregateRootConstruction;

use EventSauce\EventSourcing\AggregateRoot;
use EventSauce\EventSourcing\ConstructingAggregateRootRepository;
use EventSauce\EventSourcing\InMemoryMessageRepository;
use EventSauce\EventSourcing\Integration\DummyAggregateRootId;
use EventSauce\EventSourcing\InvalidAggregateRootReconstitutionException;
use PHPUnit\Framework\TestCase;

class AggregateRootConstructionTest extends TestCase
{
/**
* @test
*/
public function expecting_an_exception_without_history()
{
$this->expectException(InvalidAggregateRootReconstitutionException::class);
$repository = new ConstructingAggregateRootRepository(
AggregateThatRequiredHistoryForReconstitutionStub::class,
new InMemoryMessageRepository()
);

$repository->retrieve(DummyAggregateRootId::fromString('nope'));
}

/**
* @test
*/
public function expecting_an_aggregate_when_there_is_history()
{
$repository = new ConstructingAggregateRootRepository(
AggregateThatRequiredHistoryForReconstitutionStub::class,
new InMemoryMessageRepository()
);
$id = DummyAggregateRootId::fromString('nope');
$repository->persistEvents($id, 1, new DummyInternalEvent());
$aggregateRoot = $repository->retrieve($id);
$this->assertInstanceOf(AggregateThatRequiredHistoryForReconstitutionStub::class, $aggregateRoot);
}

/**
* @test
*/
public function constructing_the_aggregate_using_a_named_constructor()
{
$id = DummyAggregateRootId::fromString('nope');
$aggregate = AggregateThatRequiredHistoryForReconstitutionStub::start($id);
$this->assertInstanceOf(AggregateRoot::class, $aggregate);
}
}
@@ -0,0 +1,25 @@
<?php

namespace EventSauce\EventSourcing\Integration\RequiringHistoryWithAggregateRootConstruction;

use EventSauce\EventSourcing\AggregateRoot;
use EventSauce\EventSourcing\AggregateRootBehaviourWithRequiredHistory;
use EventSauce\EventSourcing\Integration\DummyAggregateRootId;

class AggregateThatRequiredHistoryForReconstitutionStub implements AggregateRoot
{
use AggregateRootBehaviourWithRequiredHistory;

public static function start(DummyAggregateRootId $id)
{
$aggregate = new static($id);
$aggregate->recordThat(new DummyInternalEvent());

return $aggregate;
}

protected function applyDummyInternalEvent(DummyInternalEvent $event)
{
// can be ignored
}
}
@@ -0,0 +1,8 @@
<?php

namespace EventSauce\EventSourcing\Integration\RequiringHistoryWithAggregateRootConstruction;

class DummyInternalEvent
{

}
1 change: 1 addition & 0 deletions src/Integration/TestingAggregates/DummyAggregate.php
Expand Up @@ -6,6 +6,7 @@

use EventSauce\EventSourcing\AggregateRoot;
use EventSauce\EventSourcing\AggregateRootBehaviour;
use EventSauce\EventSourcing\Integration\DummyAggregateRootId;

class DummyAggregate implements AggregateRoot
{
Expand Down
Expand Up @@ -8,6 +8,7 @@
use EventSauce\EventSourcing\AggregateRootRepository;
use EventSauce\EventSourcing\AggregateRootTestCase;
use EventSauce\EventSourcing\Header;
use EventSauce\EventSourcing\Integration\DummyAggregateRootId;
use EventSauce\EventSourcing\Message;
use EventSauce\EventSourcing\PointInTime;
use EventSauce\EventSourcing\Time\Clock;
Expand Down
2 changes: 2 additions & 0 deletions src/Integration/TestingAggregates/InitiatorCommand.php
Expand Up @@ -3,6 +3,8 @@
namespace EventSauce\EventSourcing\Integration\TestingAggregates;


use EventSauce\EventSourcing\Integration\DummyAggregateRootId;

class InitiatorCommand
{
/**
Expand Down
10 changes: 10 additions & 0 deletions src/InvalidAggregateRootReconstitutionException.php
@@ -0,0 +1,10 @@
<?php

namespace EventSauce\EventSourcing;

use RuntimeException;
use Throwable;

class InvalidAggregateRootReconstitutionException extends RuntimeException
{
}

0 comments on commit 771d12d

Please sign in to comment.