diff --git a/src/EventSourcingAnalyzer.php b/src/EventSourcingAnalyzer.php index eb19d31..a9e0a76 100644 --- a/src/EventSourcingAnalyzer.php +++ b/src/EventSourcingAnalyzer.php @@ -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; @@ -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); } } diff --git a/tests/EventSourcingAnalyzerTest.php b/tests/EventSourcingAnalyzerTest.php index 889b7c8..00c83b5 100644 --- a/tests/EventSourcingAnalyzerTest.php +++ b/tests/EventSourcingAnalyzerTest.php @@ -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); } /** @@ -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); @@ -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 @@ -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 diff --git a/tests/_files/building_added.json b/tests/_files/building_added.json index c94fe98..ec0f140 100644 --- a/tests/_files/building_added.json +++ b/tests/_files/building_added.json @@ -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",