Skip to content

Commit

Permalink
Merge pull request event-engine#17 from bitExpert/fix/check_name_uniq…
Browse files Browse the repository at this point in the history
…ueness

Ensure command, query and event names are unique
  • Loading branch information
codeliner authored Sep 2, 2019
2 parents 991f608 + 7fabe44 commit 54795d2
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 2 deletions.
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 <[email protected]>
*
* 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([]));
}
}

0 comments on commit 54795d2

Please sign in to comment.