Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure command, query and event names are unique #17

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/EventEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,16 @@ public function enableMetadataForwarding(): self
public function registerCommand(string $commandName, PayloadSchema $schema): self
{
$this->assertNotInitialized(__METHOD__);
if (\array_key_exists($commandName, $this->commandMap)) {

if ($this->isKnownQuery($commandName)) {
throw new RuntimeException("Query with name $commandName was already registered.");
}

if ($this->isKnownEvent($commandName)) {
throw new RuntimeException("Event with name $commandName was already registered.");
}

if ($this->isKnownCommand($commandName)) {
throw new RuntimeException("Command $commandName was already registered.");
}

Expand All @@ -441,7 +450,15 @@ public function registerEvent(string $eventName, PayloadSchema $schema): self
{
$this->assertNotInitialized(__METHOD__);

if (\array_key_exists($eventName, $this->eventMap)) {
if ($this->isKnownCommand($eventName)) {
throw new RuntimeException("Command with name $eventName was already registered.");
}

if ($this->isKnownQuery($eventName)) {
throw new RuntimeException("Query with name $eventName was already registered.");
}

if ($this->isKnownEvent($eventName)) {
throw new RuntimeException("Event $eventName was already registered.");
}

Expand All @@ -461,6 +478,14 @@ public function registerQuery(string $queryName, PayloadSchema $payloadSchema =
$payloadSchema = $this->schema()->emptyPayloadSchema();
}

if ($this->isKnownCommand($queryName)) {
throw new RuntimeException("Command with name $queryName was already registered.");
}

if ($this->isKnownEvent($queryName)) {
throw new RuntimeException("Event with name $queryName was already registered.");
}

if ($this->isKnownQuery($queryName)) {
throw new RuntimeException("Query $queryName was already registered");
}
Expand Down
116 changes: 116 additions & 0 deletions tests/EventEngineUniqueNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* This file is part of event-engine/php-engine.
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace EventEngineTest;

use EventEngine\DocumentStore\DocumentStore;
use EventEngine\DocumentStore\InMemoryDocumentStore;
use EventEngine\EventEngine;
use EventEngine\EventStore\EventStore;
use EventEngine\JsonSchema\JsonSchema;
use EventEngine\JsonSchema\JustinRainbowJsonSchema;
use EventEngine\Logger\DevNull;
use EventEngine\Logger\SimpleMessageEngine;
use EventEngine\Messaging\Message;
use EventEngine\Persistence\InMemoryConnection;
use EventEngine\Prooph\V7\EventStore\InMemoryEventStore;
use EventEngine\Prooph\V7\EventStore\ProophEventStore;
use EventEngine\Runtime\PrototypingFlavour;
use EventEngine\Schema\Schema;
use Psr\Container\ContainerInterface;
use Ramsey\Uuid\Uuid;

class EventEngineUniqueNameTest extends BasicTestCase
{
const CMD_REGISTER_USER = 'RegisterUser';
const EV_USER_REGISTERED = 'UserHasRegistered';
const QY_LIST_USERS = 'ListUsers';
/**
* @var EventEngine
*/
private $eventEngine;

protected function setUp()
{
parent::setUp();

$this->eventEngine = new EventEngine(new JustinRainbowJsonSchema());

$this->eventEngine->registerCommand(self::CMD_REGISTER_USER, JsonSchema::object([]));
$this->eventEngine->registerEvent(self::EV_USER_REGISTERED, JsonSchema::object([]));
$this->eventEngine->registerQuery(self::QY_LIST_USERS, JsonSchema::object([]));
}
/**
* @test
*/
public function it_does_not_allow_to_register_command_as_event()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp('/Command with name/');

$this->eventEngine->registerEvent(self::CMD_REGISTER_USER, JsonSchema::object([]));
}

/**
* @test
*/
public function it_does_not_allow_to_register_command_as_query()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp('/Command with name/');

$this->eventEngine->registerQuery(self::CMD_REGISTER_USER, JsonSchema::object([]));
}

/**
* @test
*/
public function it_does_not_allow_to_register_event_as_command()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp('/Event with name/');

$this->eventEngine->registerCommand(self::EV_USER_REGISTERED, JsonSchema::object([]));
}

/**
* @test
*/
public function it_does_not_allow_to_register_event_as_query()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp('/Event with name/');

$this->eventEngine->registerQuery(self::EV_USER_REGISTERED, JsonSchema::object([]));
}

/**
* @test
*/
public function it_does_not_allow_to_register_query_as_command()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp('/Query with name/');

$this->eventEngine->registerCommand(self::QY_LIST_USERS, JsonSchema::object([]));
}

/**
* @test
*/
public function it_does_not_allow_to_register_query_as_event()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp('/Query with name/');

$this->eventEngine->registerEvent(self::QY_LIST_USERS, JsonSchema::object([]));
}
}