Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...estingAggregates/DummyAggregateRootId.php → src/Integration/DummyAggregateRootId.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...tegration/RequiringHistoryWithAggregateRootConstruction/AggregateRootConstructionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...istoryWithAggregateRootConstruction/AggregateThatRequiredHistoryForReconstitutionStub.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Integration/RequiringHistoryWithAggregateRootConstruction/DummyInternalEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace EventSauce\EventSourcing\Integration\RequiringHistoryWithAggregateRootConstruction; | ||
|
||
class DummyInternalEvent | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace EventSauce\EventSourcing; | ||
|
||
use RuntimeException; | ||
use Throwable; | ||
|
||
class InvalidAggregateRootReconstitutionException extends RuntimeException | ||
{ | ||
} |