Skip to content

Commit

Permalink
Create new interceptor iterator to avoid issues with multiple messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Szurovecz committed Feb 22, 2015
1 parent 52e3062 commit a788111
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/predaddy/messagehandling/InterceptableMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@

namespace predaddy\messagehandling;

use ArrayIterator;
use Closure;
use InvalidArgumentException;
use precore\lang\Object;
use precore\util\Preconditions;
use predaddy\messagehandling\util\MessageCallbackClosures;
use RuntimeException;
use SplFixedArray;

/**
* @author Janos Szurovecz <szjani@szjani.hu>
Expand All @@ -42,7 +42,7 @@ abstract class InterceptableMessageBus extends Object implements MessageBus
private static $emptyCallback;

/**
* @var SplFixedArray
* @var DispatchInterceptor[]
*/
private $interceptors;

Expand All @@ -56,7 +56,7 @@ public static function init()

public function __construct(array $interceptors = [])
{
$this->interceptors = SplFixedArray::fromArray($interceptors);
$this->interceptors = $interceptors;
}

/**
Expand Down Expand Up @@ -111,7 +111,7 @@ private function createChain($message, Closure $dispatchClosure)
{
return new DefaultInterceptorChain(
$message,
$this->interceptors,
new ArrayIterator($this->interceptors),
$dispatchClosure
);
}
Expand Down
94 changes: 94 additions & 0 deletions tests/src/predaddy/messagehandling/InterceptableMessageBusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/*
* Copyright (c) 2015 Janos Szurovecz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace predaddy\messagehandling;

use Exception;
use PHPUnit_Framework_TestCase;
use predaddy\commandhandling\CommandBus;
use predaddy\domain\EventPublisher;
use predaddy\eventhandling\EventBus;
use predaddy\fixture\BaseEvent;
use predaddy\fixture\BaseEvent2;
use predaddy\fixture\SimpleCommand;
use predaddy\messagehandling\interceptors\BlockerInterceptor;

/**
* Class BusFactoryTest
*
* @package predaddy\util
* @author Janos Szurovecz <szjani@szjani.hu>
*/
class InterceptableMessageBusTest extends PHPUnit_Framework_TestCase
{
/**
* @var EventBus
*/
private $eventBus;

/**
* @var CommandBus
*/
private $commandBus;

protected function setUp()
{
$blockerInterceptor = new BlockerInterceptor();
$this->eventBus = EventBus::builder()
->withInterceptors([$blockerInterceptor])
->build();
$blockerInterceptorManager = $blockerInterceptor->manager();
$this->commandBus = CommandBus::builder()
->withInterceptors([$blockerInterceptorManager])
->withExceptionHandler($blockerInterceptorManager)
->build();
}

/**
* @test
*/
public function shouldBlockBothEvents()
{
EventPublisher::instance()->setEventBus($this->eventBus);
$eventHandlerCalled = 0;

$eventHandler1 = function (BaseEvent $event) use (&$eventHandlerCalled) {
$eventHandlerCalled++;
};
$eventHandler2 = function (BaseEvent2 $event) use (&$eventHandlerCalled) {
$eventHandlerCalled++;
};
$this->eventBus->registerClosure($eventHandler1);
$this->eventBus->registerClosure($eventHandler2);

$commandHandler = function (SimpleCommand $command) {
EventPublisher::instance()->post(new BaseEvent());
EventPublisher::instance()->post(new BaseEvent2());
throw new Exception();
};
$this->commandBus->registerClosure($commandHandler);

$this->commandBus->post(new SimpleCommand(null, null, null));
self::assertEquals(0, $eventHandlerCalled);
}
}

0 comments on commit a788111

Please sign in to comment.