-
Notifications
You must be signed in to change notification settings - Fork 0
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
albert
committed
Apr 13, 2019
1 parent
5ed5e6c
commit 7f424a3
Showing
6 changed files
with
148 additions
and
3 deletions.
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,73 @@ | ||
<?php | ||
|
||
namespace spec\AlbertDonCelis\DDD\Domain; | ||
|
||
use AlbertDonCelis\DDD\Domain\History; | ||
use Buttercup\Protects\CorruptAggregateHistory; | ||
use Buttercup\Protects\DomainEvent; | ||
use Buttercup\Protects\DomainEvents; | ||
use Buttercup\Protects\IdentifiesAggregate; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
/** | ||
* Class HistorySpec | ||
* @package spec\AlbertDonCelis\DDD\Domain | ||
* | ||
* @mixin History | ||
*/ | ||
class HistorySpec extends ObjectBehavior | ||
{ | ||
/** @var IdentifiesAggregate $aggregateId */ | ||
private $aggregateId; | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(History::class); | ||
$this->shouldHaveType(DomainEvents::class); | ||
} | ||
|
||
public function let(IdentifiesAggregate $aggregate) | ||
{ | ||
$this->aggregateId = $aggregate; | ||
|
||
$this->beConstructedWith($aggregate, []); | ||
} | ||
|
||
public function it_should_throw_exception_CorruptAggregateHistory( | ||
IdentifiesAggregate $aggregateId, DomainEvent $event) | ||
{ | ||
|
||
$events = []; | ||
$randomEvent = rand(1,5); | ||
for ($countEvent = 0; $randomEvent > $countEvent; $countEvent++) { | ||
$event->getAggregateId()->shouldBeCalled($randomEvent)->willReturn($aggregateId); | ||
$aggregateId->equals($aggregateId)->shouldBeCalled($randomEvent)->willReturn(false); | ||
array_push($events, $event); | ||
} | ||
$this->shouldThrow(CorruptAggregateHistory::class) | ||
->during__construct($aggregateId, $events); | ||
} | ||
|
||
public function it_should_not_throw_exception_CorruptAggregateHistory( | ||
IdentifiesAggregate $aggregateId, | ||
DomainEvent $event) | ||
{ | ||
$events = []; | ||
$randomEvent = rand(1,5); | ||
for ($countEvent = 0; $randomEvent > $countEvent; $countEvent++) { | ||
array_push($events, $event); | ||
$event->getAggregateId()->shouldBeCalled($randomEvent)->willReturn($aggregateId); | ||
$aggregateId->equals($aggregateId)->shouldBeCalled($randomEvent)->willReturn(true); | ||
} | ||
|
||
$this->shouldNotThrow(CorruptAggregateHistory::class) | ||
->during__construct($aggregateId, $events); | ||
} | ||
|
||
public function it_should_get_aggregate_id() | ||
{ | ||
$this->aggregateId()->shouldReturn($this->aggregateId); | ||
} | ||
|
||
} |
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,14 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: albert | ||
* Date: 2019-04-13 | ||
* Time: 10:54 | ||
*/ | ||
|
||
namespace AlbertDonCelis\DDD\Domain; | ||
|
||
interface EventSourcedInterface | ||
{ | ||
public static function reconstituteFrom(History $aggregateHistory); | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace AlbertDonCelis\DDD\Domain; | ||
|
||
use Buttercup\Protects\CorruptAggregateHistory; | ||
use Buttercup\Protects\DomainEvent; | ||
use Buttercup\Protects\DomainEvents; | ||
use Buttercup\Protects\IdentifiesAggregate; | ||
|
||
class History extends DomainEvents | ||
{ | ||
|
||
/** @var IdentifiesAggregate $aggregateId */ | ||
private $aggregateId; | ||
|
||
public function __construct(IdentifiesAggregate $aggregateId, array $events) | ||
{ | ||
/** @var DomainEvent $event */ | ||
foreach ($events as $event) { | ||
if (!$event->getAggregateId()->equals($aggregateId)) { | ||
throw new CorruptAggregateHistory(); | ||
} | ||
} | ||
parent::__construct($events); | ||
|
||
$this->aggregateId = $aggregateId; | ||
} | ||
|
||
public function aggregateId() | ||
{ | ||
return $this->aggregateId; | ||
} | ||
} |
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