From fe48575ae58e28c666338d472a66154dfea2a0eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Zaj=C4=85c?= Date: Fri, 30 Jun 2023 16:17:39 +0200 Subject: [PATCH] NamedEvents names should be unique (#158) --- .../src/Config/EventSourcingModule.php | 4 +++ ...php => EventSourcingMessagingTestCase.php} | 0 .../EventWithDuplicatedName.php | 12 +++++++ .../UniqueEventNames/EventWithUniqueName.php | 12 +++++++ .../Integration/UniqueNamedEventsTest.php | 35 +++++++++++++++++++ 5 files changed, 63 insertions(+) rename packages/PdoEventSourcing/tests/{EventSourcingMessagingTest.php => EventSourcingMessagingTestCase.php} (100%) create mode 100644 packages/PdoEventSourcing/tests/Fixture/UniqueEventNames/EventWithDuplicatedName.php create mode 100644 packages/PdoEventSourcing/tests/Fixture/UniqueEventNames/EventWithUniqueName.php create mode 100644 packages/PdoEventSourcing/tests/Integration/UniqueNamedEventsTest.php diff --git a/packages/PdoEventSourcing/src/Config/EventSourcingModule.php b/packages/PdoEventSourcing/src/Config/EventSourcingModule.php index 71f7d1a13..6d0ab6f04 100644 --- a/packages/PdoEventSourcing/src/Config/EventSourcingModule.php +++ b/packages/PdoEventSourcing/src/Config/EventSourcingModule.php @@ -112,6 +112,10 @@ public static function create(AnnotationFinder $annotationRegistrationService, I /** @var NamedEvent $attribute */ $attribute = $annotationRegistrationService->getAttributeForClass($namedEventClass, NamedEvent::class); + if (array_key_exists($attribute->getName(), $fromNameToClassMapping)) { + throw new \RuntimeException(sprintf('Named Events should have unique names. However, `%s` is used more than once.', $attribute->getName())); + } + $fromClassToNameMapping[$namedEventClass] = $attribute->getName(); $fromNameToClassMapping[$attribute->getName()] = $namedEventClass; } diff --git a/packages/PdoEventSourcing/tests/EventSourcingMessagingTest.php b/packages/PdoEventSourcing/tests/EventSourcingMessagingTestCase.php similarity index 100% rename from packages/PdoEventSourcing/tests/EventSourcingMessagingTest.php rename to packages/PdoEventSourcing/tests/EventSourcingMessagingTestCase.php diff --git a/packages/PdoEventSourcing/tests/Fixture/UniqueEventNames/EventWithDuplicatedName.php b/packages/PdoEventSourcing/tests/Fixture/UniqueEventNames/EventWithDuplicatedName.php new file mode 100644 index 000000000..ca74b48a2 --- /dev/null +++ b/packages/PdoEventSourcing/tests/Fixture/UniqueEventNames/EventWithDuplicatedName.php @@ -0,0 +1,12 @@ +expectExceptionObject(new \RuntimeException('Named Events should have unique names. However, `event` is used more than once.')); + + EcotoneLite::bootstrapForTesting( + [Ticket::class], + configuration: ServiceConfiguration::createWithDefaults() + ->withSkippedModulePackageNames(ModulePackageList::allPackagesExcept([ModulePackageList::EVENT_SOURCING_PACKAGE])) + ->withExtensionObjects([ + InMemoryRepositoryBuilder::createForAllEventSourcedAggregates(), + ]) + ->withNamespaces([ + 'Test\Ecotone\EventSourcing\Fixture\UniqueEventNames', + ]), + pathToRootCatalog: __DIR__ . '/../../', + ) + ->getFlowTestSupport() + ; + } +}