Skip to content

Commit

Permalink
unfinished reconstituteFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
albert committed Apr 13, 2019
1 parent 5ed5e6c commit 7f424a3
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 3 deletions.
73 changes: 73 additions & 0 deletions spec/AlbertDonCelis/DDD/Domain/HistorySpec.php
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);
}

}
20 changes: 18 additions & 2 deletions spec/AlbertDonCelis/DDD/Example/Domain/BasketEventSourcedSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
namespace spec\AlbertDonCelis\DDD\Example\Domain;

use AlbertDonCelis\DDD\Domain\AbstractEventSourced;
use AlbertDonCelis\DDD\Domain\History;
use AlbertDonCelis\DDD\Example\Domain\BasketEventSourced;
use AlbertDonCelis\DDD\Example\Domain\BasketWasPickedUp;
use AlbertDonCelis\DDD\Example\Domain\ValueObject\BasketId;
use AlbertDonCelis\DDD\Example\Domain\ValueObject\ProductId;
use Buttercup\Protects\DomainEvent;
use Buttercup\Protects\DomainEvents;
use Buttercup\Protects\IdentifiesAggregate;
use Faker\Factory;
use Faker\Generator;
use PhpSpec\ObjectBehavior;
Expand Down Expand Up @@ -48,15 +52,27 @@ public function it_should_return_domain_events()
public function it_should_get_the_events_from_the_basket(BasketId $basketId)
{
$this->beConstructedThrough('pickUp', [ $basketId ]);

$this->getRecordedEvents()->count()->shouldReturn(1);
}

public function it_should_add_a_product_to_a_basket(ProductId $productId)
{

$this->addProduct($productId, $this->faker->firstName)->shouldBeNull();
$this->getRecordedEvents()->count()->shouldReturn(2);
}

public function it_should_reconstitute_form_the_history(
History $history,
IdentifiesAggregate $aggregateId
)
{
$BasketUuId = Factory::create()->uuid;

$this->beConstructedThrough('reconstituteFrom', [ $history ]);

$history->aggregateId()->shouldBeCalledOnce()->willReturn($aggregateId);
$aggregateId->__toString()->willReturn($BasketUuId);

$this->shouldHaveType(BasketEventSourced::class);
}
}
2 changes: 1 addition & 1 deletion src/AlbertDonCelis/DDD/Domain/AbstractEventSourced.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Buttercup\Protects\DomainEvents;
use Buttercup\Protects\RecordsEvents;

abstract class AbstractEventSourced implements RecordsEvents
abstract class AbstractEventSourced implements RecordsEvents, EventSourcedInterface
{
/**
* @var DomainEvents []
Expand Down
14 changes: 14 additions & 0 deletions src/AlbertDonCelis/DDD/Domain/EventSourcedInterface.php
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);
}
33 changes: 33 additions & 0 deletions src/AlbertDonCelis/DDD/Domain/History.php
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;
}
}
9 changes: 9 additions & 0 deletions src/AlbertDonCelis/DDD/Example/Domain/BasketEventSourced.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AlbertDonCelis\DDD\Example\Domain;

use AlbertDonCelis\DDD\Domain\AbstractEventSourced;
use AlbertDonCelis\DDD\Domain\History;
use AlbertDonCelis\DDD\Example\Domain\ValueObject\BasketId;
use AlbertDonCelis\DDD\Example\Domain\ValueObject\ProductId;

Expand Down Expand Up @@ -41,4 +42,12 @@ public function addProduct(ProductId $productId, string $productName): void
new ProductWasAddedToBasket($this->basketId, $productId, $productName)
);
}

public static function reconstituteFrom(History $aggregateHistory)
{
$basketId = $aggregateHistory->aggregateId();
$basketEventSourced = new self(new BasketId($basketId));

return $basketEventSourced;
}
}

0 comments on commit 7f424a3

Please sign in to comment.