Skip to content

Commit

Permalink
Merge pull request #176 from prooph/tests
Browse files Browse the repository at this point in the history
add tests, small bugfix in exception handling
  • Loading branch information
prolic committed Dec 17, 2017
2 parents 3df548f + aef9d68 commit 3b95e1e
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Plugin/Router/EventRouter.php
Expand Up @@ -101,7 +101,7 @@ public function to($eventListener): EventRouter
'Cannot map listener %s to an event. Please use method route before calling method to',
is_object($eventListener)
? get_class($eventListener)
: is_string($eventListener) ? $eventListener : gettype($eventListener)
: (is_string($eventListener) ? $eventListener : gettype($eventListener))
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Router/RegexRouter.php
Expand Up @@ -97,7 +97,7 @@ public function to($handler): RegexRouter
'Cannot map handler %s to a pattern. Please use method route before calling method to',
is_object($handler)
? get_class($handler)
: is_string($handler) ? $handler : gettype($handler)
: (is_string($handler) ? $handler : gettype($handler))
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Router/SingleHandlerRouter.php
Expand Up @@ -88,7 +88,7 @@ public function to($messageHandler): SingleHandlerRouter
'Cannot map handler %s to a message. Please use method route before calling method to',
is_object($messageHandler)
? get_class($messageHandler)
: is_string($messageHandler) ? $messageHandler : gettype($messageHandler)
: (is_string($messageHandler) ? $messageHandler : gettype($messageHandler))
));
}

Expand Down
10 changes: 6 additions & 4 deletions tests/Plugin/Guard/FinalizeGuardTest.php
Expand Up @@ -40,12 +40,14 @@ protected function setUp(): void
public function it_allows_when_authorization_service_grants_access_without_deferred(): void
{
$authorizationService = $this->prophesize(AuthorizationService::class);
$authorizationService->isGranted('test_event')->willReturn(true);
$authorizationService->isGranted('test_event')->willReturn(true)->shouldBeCalled();

$messageBus = new EventBus();

$routeGuard = new FinalizeGuard($authorizationService->reveal());
$routeGuard->attachToMessageBus($this->messageBus);
$routeGuard->attachToMessageBus($messageBus);

$this->messageBus->dispatch('test_event');
$messageBus->dispatch('test_event');
}

/**
Expand All @@ -54,7 +56,7 @@ public function it_allows_when_authorization_service_grants_access_without_defer
public function it_allows_when_authorization_service_grants_access_with_deferred(): void
{
$authorizationService = $this->prophesize(AuthorizationService::class);
$authorizationService->isGranted('test_event', 'result')->willReturn(true);
$authorizationService->isGranted('test_event', 'result')->willReturn(true)->shouldBeCalled();

$routeGuard = new FinalizeGuard($authorizationService->reveal());
$routeGuard->attachToMessageBus($this->messageBus);
Expand Down
34 changes: 24 additions & 10 deletions tests/Plugin/MessageFactoryPluginTest.php
Expand Up @@ -40,6 +40,20 @@ public function it_turns_a_message_given_as_array_into_a_message_object_using_a_
$factoryPlugin = new MessageFactoryPlugin($messageFactory->reveal());
$factoryPlugin->attachToMessageBus($commandBus);

$handled = false;

$handler = function (DoSomething $command) use (&$handled): void {
$handled = true;
};

$commandBus->attach(
CommandBus::EVENT_DISPATCH,
function (ActionEvent $event) use (&$handler): void {
$event->setParam(CommandBus::EVENT_PARAM_MESSAGE_HANDLER, $handler);
},
CommandBus::PRIORITY_ROUTE
);

$commandBus->attach(
CommandBus::EVENT_FINALIZE,
function (ActionEvent $actionEvent): void {
Expand All @@ -49,20 +63,19 @@ function (ActionEvent $actionEvent): void {
}
);

try {
$commandBus->dispatch([
'message_name' => 'custom-message',
'payload' => ['some data'],
]);
} catch (MessageDispatchException $exception) {
// ignore
}
$commandBus->dispatch([
'message_name' => 'custom-message',
'payload' => ['some data'],
]);

$this->assertTrue($handled);
}

/**
* @test
* @group by
*/
public function it_will_return_eary_if_message_name_not_present_in_message(): void
public function it_will_return_early_if_message_name_not_present_in_message(): void
{
$commandBus = new CommandBus();
$messageFactory = $this->prophesize(MessageFactory::class);
Expand All @@ -79,7 +92,8 @@ function (ActionEvent $actionEvent): void {
'payload' => ['some data'],
],
$message);
}
},
1000
);

try {
Expand Down
16 changes: 15 additions & 1 deletion tests/Plugin/Router/AsyncSwitchMessageRouterTest.php
Expand Up @@ -13,6 +13,7 @@
namespace ProophTest\ServiceBus\Plugin\Router;

use PHPUnit\Framework\TestCase;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\DefaultActionEvent;
use Prooph\ServiceBus\Async\MessageProducer;
use Prooph\ServiceBus\CommandBus;
Expand All @@ -33,15 +34,28 @@ class AsyncSwitchMessageRouterTest extends TestCase
public function it_sets_message_producer_as_message_handler_on_dispatch_initialize(): void
{
$messageProducer = $this->prophesize(MessageProducer::class);
$messageProducer = $messageProducer->reveal();

$commandBus = new CommandBus();

$router = new AsyncSwitchMessageRouter(new SingleHandlerRouter(), $messageProducer->reveal());
$handler = null;

$commandBus->attach(
CommandBus::EVENT_DISPATCH,
function (ActionEvent $e) use (&$handler): void {
$handler = $e->getParam(CommandBus::EVENT_PARAM_MESSAGE_HANDLER);
},
CommandBus::PRIORITY_ROUTE - 1
);

$router = new AsyncSwitchMessageRouter(new SingleHandlerRouter(), $messageProducer);
$router->attachToMessageBus($commandBus);

$message = new AsyncCommand(['foo' => 'bar']);

$commandBus->dispatch($message);

$this->assertSame($messageProducer, $handler);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Plugin/Router/EventRouterTest.php
Expand Up @@ -90,6 +90,18 @@ public function it_fails_on_setting_a_listener_before_an_event_is_set(): void
$router->to('SomethingDoneListener');
}

/**
* @test
*/
public function it_fails_on_setting_a_listener_before_an_event_is_set_2(): void
{
$this->expectException(RuntimeException::class);

$router = new EventRouter();

$router->to(new \stdClass());
}

/**
* @test
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/Plugin/Router/RegexRouterTest.php
Expand Up @@ -163,6 +163,18 @@ public function it_fails_on_setting_a_handler_before_a_pattern_is_set(): void
$router->to('DoSomethingHandler');
}

/**
* @test
*/
public function it_fails_on_setting_a_handler_before_a_pattern_is_set_2(): void
{
$this->expectException(RuntimeException::class);

$router = new RegexRouter();

$router->to(new \stdClass());
}

/**
* @test
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/Plugin/Router/SingleHandlerRouterTest.php
Expand Up @@ -126,6 +126,18 @@ public function it_fails_on_setting_a_handler_before_a_command_is_set(): void
$router->to('DoSomethingHandler');
}

/**
* @test
*/
public function it_fails_on_setting_a_handler_before_a_command_is_set_2(): void
{
$this->expectException(RuntimeException::class);

$router = new CommandRouter();

$router->to(new \stdClass());
}

/**
* @test
*/
Expand Down
1 change: 0 additions & 1 deletion tests/Plugin/ServiceLocatorPluginTest.php
Expand Up @@ -55,7 +55,6 @@ function (ActionEvent $actionEvent): void {

/**
* @test
* @group by
*/
public function it_doesnt_override_previous_event_handlers(): void
{
Expand Down

0 comments on commit 3b95e1e

Please sign in to comment.