Skip to content

Commit

Permalink
NamedEvents names should be unique (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
unixslayer authored Jun 30, 2023
1 parent 32e8816 commit fe48575
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/PdoEventSourcing/src/Config/EventSourcingModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Test\Ecotone\EventSourcing\Fixture\UniqueEventNames;

use Ecotone\Modelling\Attribute\NamedEvent;

#[NamedEvent('event')]
final class EventWithDuplicatedName
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Test\Ecotone\EventSourcing\Fixture\UniqueEventNames;

use Ecotone\Modelling\Attribute\NamedEvent;

#[NamedEvent('event')]
final class EventWithUniqueName
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Test\Ecotone\EventSourcing\Integration;

use Ecotone\Lite\EcotoneLite;
use Ecotone\Lite\Test\Configuration\InMemoryRepositoryBuilder;
use Ecotone\Messaging\Config\ModulePackageList;
use Ecotone\Messaging\Config\ServiceConfiguration;
use PHPUnit\Framework\TestCase;
use Test\Ecotone\EventSourcing\Fixture\Ticket\Ticket;

final class UniqueNamedEventsTest extends TestCase
{
public function test_event_names_should_be_unique(): void
{
$this->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()
;
}
}

0 comments on commit fe48575

Please sign in to comment.