From 04bb63f02762623628133441940c7573e2cc5b9b Mon Sep 17 00:00:00 2001 From: Codeliner Date: Sat, 5 Jul 2014 17:14:58 +0200 Subject: [PATCH] Fix #23: Merge command/event map configuration --- composer.json | 6 +- .../LocalSynchronousInitializer.php | 22 +++- .../PersistedEventDispatcherTest.php | 3 +- .../Initializer/LocalInitializerTest.php | 103 ++++++++++++++++++ 4 files changed, 127 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 306df76..e8b8703 100644 --- a/composer.json +++ b/composer.json @@ -25,8 +25,8 @@ "require": { "php": ">=5.4", "rhumsaa/uuid" : "2.5.*", - "codeliner/php-equalsbuilder": "1.1.*", - "codeliner/array-reader": "1.0.*", + "codeliner/php-equalsbuilder": "~1.1", + "codeliner/array-reader": "~1.1", "beberlei/assert": "*", "zendframework/zend-servicemanager" : "2.3.*", "zendframework/zend-eventmanager" : "2.3.*", @@ -40,7 +40,7 @@ }, "suggest": { "chrisboulton/php-resque": "Use php-resque as Message Dispatcher", - "prooph/event-store": "Use ProophEventStore and let ServiceBus dispatch persited DomainEvents" + "prooph/event-store": "Use ProophEventStore and let ServiceBus dispatch persisted DomainEvents" }, "autoload": { "psr-0": { diff --git a/src/Prooph/ServiceBus/Initializer/LocalSynchronousInitializer.php b/src/Prooph/ServiceBus/Initializer/LocalSynchronousInitializer.php index f3cb97b..ce34cd3 100644 --- a/src/Prooph/ServiceBus/Initializer/LocalSynchronousInitializer.php +++ b/src/Prooph/ServiceBus/Initializer/LocalSynchronousInitializer.php @@ -11,6 +11,7 @@ namespace Prooph\ServiceBus\Initializer; +use Codeliner\ArrayReader\ArrayReader; use Prooph\ServiceBus\LifeCycleEvent\InitializeEvent; use Prooph\ServiceBus\Service\Definition; use Zend\EventManager\EventManagerInterface; @@ -134,7 +135,14 @@ public function initializeLocalServiceBus(InitializeEvent $e) $serviceBusManager->setDefaultCommandBus('local-command-bus'); $serviceBusManager->setDefaultEventBus('local-event-bus'); - $commandMap = array(); + $configReader = new ArrayReader($configuration); + + $commandMap = $configReader->arrayValue( + str_replace(".", "\.", Definition::CONFIG_ROOT) . '.' + . Definition::COMMAND_BUS . '.' + . 'local-command-bus' . '.' + . Definition::COMMAND_MAP + ); foreach ($this->commandHandlers as $commandName => $commandHandler) { $serviceBusManager->setService($commandName . '_local_handler', $commandHandler); @@ -147,10 +155,18 @@ public function initializeLocalServiceBus(InitializeEvent $e) Definition::MESSAGE_DISPATCHER => 'in_memory_message_dispatcher' ); - $eventMap = array(); + $eventMap = $configReader->arrayValue( + str_replace(".", "\.", Definition::CONFIG_ROOT) . '.' + . Definition::EVENT_BUS . '.' + . 'local-event-bus' . '.' + . Definition::EVENT_MAP + ); foreach ($this->eventHandlers as $eventName => $handlersOfEvent) { - $eventMap[$eventName] = array(); + + if (! isset($eventMap[$eventName])) { + $eventMap[$eventName] = array(); + } foreach ($handlersOfEvent as $handlerIndex => $eventHandler) { $serviceBusManager->setService($eventName . '_local_handler_' . $handlerIndex, $eventHandler); diff --git a/tests/Prooph/ServiceBusTest/EventStoreFeature/PersistedEventDispatcherTest.php b/tests/Prooph/ServiceBusTest/EventStoreFeature/PersistedEventDispatcherTest.php index 0ff3cf7..c942f7f 100644 --- a/tests/Prooph/ServiceBusTest/EventStoreFeature/PersistedEventDispatcherTest.php +++ b/tests/Prooph/ServiceBusTest/EventStoreFeature/PersistedEventDispatcherTest.php @@ -164,7 +164,8 @@ function(UserCreated $e) use (&$userCreatedEventReceived) { $config = array( "adapter" => array( - "Prooph\EventStore\Adapter\Zf2\Zf2EventStoreAdapter" => array( + 'type' => "Prooph\EventStore\Adapter\Zf2\Zf2EventStoreAdapter", + 'options' => array( 'connection' => array( 'driver' => 'Pdo_Sqlite', 'database' => ':memory:' diff --git a/tests/Prooph/ServiceBusTest/Initializer/LocalInitializerTest.php b/tests/Prooph/ServiceBusTest/Initializer/LocalInitializerTest.php index 270280c..0afe721 100644 --- a/tests/Prooph/ServiceBusTest/Initializer/LocalInitializerTest.php +++ b/tests/Prooph/ServiceBusTest/Initializer/LocalInitializerTest.php @@ -12,12 +12,15 @@ namespace Prooph\ServiceBusTest\Initializer; use Prooph\ServiceBus\Initializer\LocalSynchronousInitializer; +use Prooph\ServiceBus\Service\Definition; +use Prooph\ServiceBus\Service\ServiceBusConfiguration; use Prooph\ServiceBus\Service\ServiceBusManager; use Prooph\ServiceBusTest\Mock\DoSomething; use Prooph\ServiceBusTest\Mock\HandleCommandHandler; use Prooph\ServiceBusTest\Mock\OnEventHandler; use Prooph\ServiceBusTest\Mock\SomethingDone; use Prooph\ServiceBusTest\TestCase; +use Zend\ServiceManager\Config; /** * Class LocalInitializerTest @@ -57,5 +60,105 @@ public function it_initializes_a_local_service_bus_environment() $this->assertEquals('event payload', $somethingDoneHandler->lastEvent()->data()); $this->assertEquals(2, $somethingDoneHandler->eventCount()); } + + /** + * @test + */ + public function it_merges_configured_command_map_with_attached_command_handlers() + { + $serviceBusConfig = new ServiceBusConfiguration(array( + Definition::CONFIG_ROOT => array( + Definition::COMMAND_BUS => array( + 'local-command-bus' => array( + Definition::COMMAND_MAP => array( + 'My\Custom\Command' => 'My\Custom\CommandHandler' + ) + ) + ) + ) + )); + + $serviceBusManager = new ServiceBusManager($serviceBusConfig); + + $localEnv = new LocalSynchronousInitializer(); + + $doSomething = DoSomething::fromData('test payload'); + + $doSomethingHandler = new HandleCommandHandler(); + + $localEnv->setCommandHandler($doSomething, $doSomethingHandler); + + $serviceBusManager->events()->attachAggregate($localEnv); + + $serviceBusManager->initialize(); + + $configuration = $serviceBusManager->get('configuration'); + + $localCommandBusConfig = $configuration[Definition::CONFIG_ROOT][Definition::COMMAND_BUS]['local-command-bus']; + + $checkConfig = array( + Definition::COMMAND_MAP => array( + 'My\Custom\Command' => 'My\Custom\CommandHandler', + 'Prooph\ServiceBusTest\Mock\DoSomething' => 'Prooph\ServiceBusTest\Mock\DoSomething_local_handler' + ), + Definition::QUEUE => 'local-queue', + Definition::MESSAGE_DISPATCHER => 'in_memory_message_dispatcher' + ); + + $this->assertEquals($checkConfig, $localCommandBusConfig); + } + + /** + * @test + */ + public function it_merges_configured_event_map_with_attached_command_handlers() + { + $somethingDone = SomethingDone::fromData('event payload'); + + $serviceBusConfig = new ServiceBusConfiguration(array( + Definition::CONFIG_ROOT => array( + Definition::EVENT_BUS => array( + 'local-event-bus' => array( + Definition::EVENT_MAP => array( + 'My\Custom\Event' => array('My\Custom\EventHandler'), + get_class($somethingDone) => array('My\Custom\SomethingDoneHandler'), + + ) + ) + ) + ) + )); + + $serviceBusManager = new ServiceBusManager($serviceBusConfig); + + $localEnv = new LocalSynchronousInitializer(); + + $somethingDoneHandler = new OnEventHandler(); + + $localEnv->addEventHandler($somethingDone, $somethingDoneHandler); + + $serviceBusManager->events()->attachAggregate($localEnv); + + $serviceBusManager->initialize(); + + $configuration = $serviceBusManager->get('configuration'); + + $localEventBusConfig = $configuration[Definition::CONFIG_ROOT][Definition::EVENT_BUS]['local-event-bus']; + + $checkConfig = array( + Definition::EVENT_MAP => array( + 'My\Custom\Event' => array('My\Custom\EventHandler'), + get_class($somethingDone) => array( + 'My\Custom\SomethingDoneHandler', + 'Prooph\ServiceBusTest\Mock\SomethingDone_local_handler_0' + ), + + ), + Definition::QUEUE => 'local-queue', + Definition::MESSAGE_DISPATCHER => 'in_memory_message_dispatcher' + ); + + $this->assertEquals($checkConfig, $localEventBusConfig); + } } \ No newline at end of file