-
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 15, 2019
1 parent
93f9355
commit 57c4c06
Showing
10 changed files
with
181 additions
and
0 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
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
14 changes: 14 additions & 0 deletions
14
spec/AlbertDonCelis/DDD/Infrastructure/EventStore/GlobalFunction.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,14 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: albert | ||
* Date: 2019-04-15 | ||
* Time: 10:27 | ||
*/ | ||
|
||
namespace AlbertDonCelis\DDD\Infrastructure\EventStore { | ||
|
||
function date($format) { | ||
return $format; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
spec/AlbertDonCelis/DDD/Infrastructure/EventStore/PDOEventStoreSpec.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,81 @@ | ||
<?php | ||
|
||
namespace spec\AlbertDonCelis\DDD\Infrastructure\EventStore; | ||
|
||
require_once 'GlobalFunction.php'; | ||
|
||
use AlbertDonCelis\DDD\Domain\DomainEventInterface; | ||
use AlbertDonCelis\DDD\Example\Domain\BasketEventSourced; | ||
use AlbertDonCelis\DDD\Example\Domain\ValueObject\BasketId; | ||
use AlbertDonCelis\DDD\Infrastructure\EventStore\EventStoreInterface; | ||
use AlbertDonCelis\DDD\Infrastructure\EventStore\PDOEventStore; | ||
use Faker\Factory; | ||
use Faker\Generator; | ||
use PhpSpec\ObjectBehavior; | ||
/** | ||
* Class PDOEventStoreSpec | ||
* @package spec\AlbertDonCelis\DDD\Infrastructure\EventStore | ||
* | ||
* @mixin PDOEventStore | ||
*/ | ||
class PDOEventStoreSpec extends ObjectBehavior | ||
{ | ||
/** @var \PDO $pdo */ | ||
private $pdo; | ||
|
||
/** @var Generator $faker */ | ||
private $faker; | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(PDOEventStore::class); | ||
$this->shouldHaveType(EventStoreInterface::class); | ||
} | ||
|
||
public function let(\PDO $pdo) | ||
{ | ||
$this->pdo = $pdo; | ||
$this->faker = Factory::create(); | ||
$this->beConstructedWith($pdo); | ||
} | ||
|
||
private function insertDataString(): string | ||
{ | ||
return <<<EOF | ||
INSERT INTO events (aggregate_id, `event_name`, `entity_type`, created_at, `data`) VALUES (:aggregate_id, :event_name, :entity_type, :created_at, :data) | ||
EOF; | ||
|
||
} | ||
|
||
public function it_should_insert_data_of_a_domain_events(\PDOStatement $statement) | ||
{ | ||
$this->pdo->prepare($this->insertDataString())->shouldBeCalledTimes(1)->willReturn($statement); | ||
|
||
$basketId = new BasketId($this->faker->uuid); | ||
|
||
$basketEventSourced = BasketEventSourced::pickUp($basketId); | ||
|
||
$recordEvents = $basketEventSourced->getRecordedEvents(); | ||
|
||
/** @var DomainEventInterface $event */ | ||
foreach($recordEvents as $event) { | ||
|
||
$statement->execute([ | ||
':aggregate_id' => (string) $basketId, | ||
':event_name' => $event->eventName(), | ||
':entity_type' => $event->entityType(), | ||
':created_at' => date('Y-m-d H:i:s'), | ||
':data' => json_encode($event->data()) | ||
|
||
])->shouldBeCalledTimes(count($recordEvents)); | ||
|
||
} | ||
|
||
$this->commit($recordEvents); | ||
|
||
} | ||
} | ||
|
||
function date($format) { | ||
return $format; | ||
} |
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 |
---|---|---|
|
@@ -34,4 +34,9 @@ public function entityType(): string | |
{ | ||
return "Basket"; | ||
} | ||
|
||
public function data(): array | ||
{ | ||
return []; | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
src/AlbertDonCelis/DDD/Infrastructure/EventStore/EventStoreInterface.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,18 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: albert | ||
* Date: 2019-04-15 | ||
* Time: 09:33 | ||
*/ | ||
|
||
namespace AlbertDonCelis\DDD\Infrastructure\EventStore; | ||
|
||
use AlbertDonCelis\DDD\Domain\History; | ||
use Buttercup\Protects\DomainEvents; | ||
use Buttercup\Protects\IdentifiesAggregate; | ||
|
||
interface EventStoreInterface | ||
{ | ||
public function commit(DomainEvents $domainEvents): void; | ||
} |
41 changes: 41 additions & 0 deletions
41
src/AlbertDonCelis/DDD/Infrastructure/EventStore/PDOEventStore.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,41 @@ | ||
<?php | ||
|
||
namespace AlbertDonCelis\DDD\Infrastructure\EventStore; | ||
|
||
use AlbertDonCelis\DDD\Domain\DomainEventInterface; | ||
use AlbertDonCelis\DDD\Domain\History; | ||
use Buttercup\Protects\DomainEvents; | ||
use Buttercup\Protects\IdentifiesAggregate; | ||
|
||
class PDOEventStore implements EventStoreInterface | ||
{ | ||
/** @var \PDO $pdo */ | ||
private $pdo; | ||
|
||
public function __construct(\PDO $pdo) | ||
{ | ||
$this->pdo = $pdo; | ||
} | ||
|
||
private function eventStoreInsertString(): string | ||
{ | ||
return <<<EOF | ||
INSERT INTO events (aggregate_id, `event_name`, `entity_type`, created_at, `data`) VALUES (:aggregate_id, :event_name, :entity_type, :created_at, :data) | ||
EOF; | ||
} | ||
public function commit(DomainEvents $domainEvents): void | ||
{ | ||
$stmt = $this->pdo->prepare($this->eventStoreInsertString()); | ||
|
||
/** @var DomainEventInterface $event */ | ||
foreach ($domainEvents as $event) { | ||
$stmt->execute([ | ||
':aggregate_id' => (string) $event->getAggregateId(), | ||
':event_name' => $event->eventName(), | ||
':entity_type' => $event->entityType(), | ||
':created_at' => date('Y-m-d H:i:s'), | ||
':data' => json_encode($event->data()) | ||
]); | ||
} | ||
} | ||
} |