Skip to content

Commit

Permalink
Fix #23: Merge command/event map configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
codeliner committed Jul 5, 2014
1 parent de51aa0 commit 04bb63f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand All @@ -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": {
Expand Down
22 changes: 19 additions & 3 deletions src/Prooph/ServiceBus/Initializer/LocalSynchronousInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:'
Expand Down
103 changes: 103 additions & 0 deletions tests/Prooph/ServiceBusTest/Initializer/LocalInitializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 04bb63f

Please sign in to comment.