Skip to content

Commit

Permalink
Fix filtering for events
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrokeil committed Feb 19, 2021
1 parent bcc35fc commit e506205
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 35 deletions.
72 changes: 41 additions & 31 deletions src/EventSourcingAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ private function filterEventsWithConnectionOf(Node $node): array
case VertexType::TYPE_EVENT:
foreach ($this->node->sources() as $source) {
if ($this->areNodesEqual($source, $node)) {
$vertices[] = $source;
$vertices[] = $this->node;
}
}
foreach ($this->node->targets() as $target) {
if ($this->areNodesEqual($target, $node)) {
$vertices[] = $target;
$vertices[] = $this->node;
}
}
break;
Expand Down Expand Up @@ -181,47 +181,57 @@ public function aggregateMap(): AggregateConnectionMap
$commandMap = $this->commandMap();
$eventMap = $this->eventMap();

/** @var Node $vertex */
foreach ($this->filterVerticesByType(VertexType::TYPE_AGGREGATE) as $vertex) {
$aggregate = Vertex::fromCodyNode($vertex, $this->filterName, $this->metadataFactory);
/** @var Node $aggregateVertex */
foreach ($this->filterVerticesByType(VertexType::TYPE_AGGREGATE) as $aggregateVertex) {
$aggregate = Vertex::fromCodyNode($aggregateVertex, $this->filterName, $this->metadataFactory);
$name = $aggregate->name();

if (false === $this->aggregateConnectionMap->has($name)) {
$this->aggregateConnectionMap = $this->aggregateConnectionMap->with(
$name,
// @phpstan-ignore-next-line
new AggregateConnection($aggregate)
);
$commandVertices = $this->filterCommandsWithConnectionOf($vertex);
$eventVertices = $this->filterEventsWithConnectionOf($vertex);
if (true === $this->aggregateConnectionMap->has($name)) {
continue;
}
// @phpstan-ignore-next-line
$aggregateConnection = new AggregateConnection($aggregate);

$countCommandVertices = \count($commandVertices);
$this->aggregateConnectionMap = $this->aggregateConnectionMap->with($name, $aggregateConnection);
$commandVertices = $this->filterCommandsWithConnectionOf($aggregateVertex);
$eventVertices = $this->filterEventsWithConnectionOf($aggregateVertex);

if ($countCommandVertices > 1) {
throw new RuntimeException(
\sprintf('Multiple command connections to aggregate "%s" found. Can not handle it.', $name)
);
}
$countCommandVertices = \count($commandVertices);

if ($countCommandVertices === 1) {
$command = Vertex::fromCodyNode(\current($commandVertices), $this->filterName);
if ($countCommandVertices > 1) {
throw new RuntimeException(
\sprintf('Multiple command connections to aggregate "%s" found. Can not handle it.', $name)
);
}

if (true === $commandMap->has($command->name())) {
$events = [];
if ($countCommandVertices === 1) {
$command = Vertex::fromCodyNode(\current($commandVertices), $this->filterName);

foreach ($eventVertices as $eventVertex) {
$event = Vertex::fromCodyNode($eventVertex, $this->filterName);
if (true === $commandMap->has($command->name())) {
$events = [];

if ($eventMap->has($event->name())) {
$events[] = $eventMap->vertex($event->name());
}
foreach ($eventVertices as $eventVertex) {
$event = Vertex::fromCodyNode($eventVertex, $this->filterName);

if ($eventMap->has($event->name())) {
$events[] = $eventMap->vertex($event->name());
}
// @phpstan-ignore-next-line
$map = $this->aggregateConnectionMap->aggregateConnection($name)->withCommandEvents($commandMap->vertex($command->name()), ...$events);
$this->aggregateConnectionMap = $this->aggregateConnectionMap->with($name, $map);
}
// @phpstan-ignore-next-line
$aggregateConnection = $aggregateConnection->withCommandEvents($commandMap->vertex($command->name()), ...$events);
}
} elseif (\count($eventVertices) > 0) {
foreach ($eventVertices as $eventVertex) {
$events = [];
$event = Vertex::fromCodyNode($eventVertex, $this->filterName);

if ($eventMap->has($event->name())) {
$events[] = $eventMap->vertex($event->name());
}
}
$aggregateConnection = $aggregateConnection->withEvents(...$events);
}
$this->aggregateConnectionMap = $this->aggregateConnectionMap->with($name, $aggregateConnection);
}
}

Expand Down
40 changes: 37 additions & 3 deletions tests/EventSourcingAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ public function it_returns_aggregate_map_of_command_node(): void
$commandMap = $aggregate->commandMap();
$this->assertCount(1, $commandMap);
$this->assertCommandAddBuilding($commandMap->current());

$eventMap = $aggregate->eventMap();
$this->assertCount(0, $eventMap);
}

/**
* @test
*/
public function it_returns_aggregate_map_of_event_node(): void
{
$node = JsonNode::fromJson(\file_get_contents(self::FILES_DIR . 'building_added.json'));

$eventSourcingAnalyzer = new EventSourcingAnalyzer($node, $this->filter);
$aggregateMap = $eventSourcingAnalyzer->aggregateMap();

$this->assertCount(1, $aggregateMap);
$aggregate = $aggregateMap->current();
$this->assertAggregateBuilding($aggregate->aggregate(), 'buTwEKKNLBBo6WAERYN1Gn');

$eventMap = $aggregate->eventMap();
$this->assertCount(1, $eventMap);
$this->assertEventBuildingAdded($eventMap->current());

$commandMap = $aggregate->commandMap();
$this->assertCount(0, $commandMap);
}

/**
Expand Down Expand Up @@ -119,7 +144,8 @@ public function it_returns_aggregate_map_of_aggregate_node(): void

$commandMap = $aggregate->commandMap();
$this->assertCount(1, $commandMap);
$this->assertCommandCheckInUser($commandMap->current());
$command = $commandMap->current();
$this->assertCommandCheckInUser($command);

$eventMap = $aggregate->eventMap();
$this->assertCount(2, $eventMap);
Expand All @@ -129,6 +155,14 @@ public function it_returns_aggregate_map_of_aggregate_node(): void
$eventMap->next();
$event = $eventMap->current();
$this->assertEventDoubleCheckInDetected($event);

$commandsToEventsMap = $aggregate->commandsToEventsMap();

$this->assertCount(1, $commandsToEventsMap);
$this->assertTrue($commandsToEventsMap->offsetExists($command));

$events = $commandsToEventsMap->offsetGet($command);
$this->assertCount(2, $events);
}

private function assertAggregateBuilding(AggregateType $aggregate, string $id): void
Expand All @@ -148,8 +182,8 @@ private function assertCommandAddBuilding(CommandType $command): void
private function assertEventBuildingAdded(EventType $event): void
{
$this->assertSame('ctuHbHKF1pwqQfa3VZ1nfz', $event->id());
$this->assertSame('BuildingAdded', $event->name());
$this->assertSame('BuildingAdded', $event->label());
$this->assertSame('Building Added', $event->name());
$this->assertSame('Building Added', $event->label());
}

private function assertCommandCheckInUser(CommandType $command): void
Expand Down
2 changes: 1 addition & 1 deletion tests/_files/building_added.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"node": {
"id": "tF2ZuZCXsdQMhRmRXydfuW",
"id": "ctuHbHKF1pwqQfa3VZ1nfz",
"name": "Building Added",
"type": "event",
"link": "https://inspect.event-engine.io/inspectio/board/3fc5479e-cf3c-459a-96fd-5b62d002f843?cells=tF2ZuZCXsdQMhRmRXydfuW&clicks=1",
Expand Down

0 comments on commit e506205

Please sign in to comment.