Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[ChainNode] changes according to the latest comment

  • Loading branch information...
commit 0d414ac32abf3379095be0619669aebcab0ef7cb 1 parent eb2fb33
@vatson vatson authored
View
53 src/BadaBoom/ChainNode/CallbackChainNode.php
@@ -10,30 +10,39 @@
*/
class CallbackChainNode extends AbstractChainNode
{
- /**
- * @var Callable|Closure
- */
- protected $callback;
-
- /**
- * @param Callable|Closure $callback
- */
- public function __construct($callback)
- {
- if (false == is_callable($callback)) {
- throw new \InvalidArgumentException('Invalid callable provided');
+ /**
+ * @var Callable|Closure
+ */
+ protected $callback;
+
+ /**
+ * @var boolean
+ */
+ protected $handleNextNode;
+
+ /**
+ * @param Callable|Closure $callback
+ * @param boolean $handleNextNode
+ */
+ public function __construct($callback, $handleNextNode = true)
+ {
+ if (false == is_callable($callback)) {
+ throw new \InvalidArgumentException('Invalid callable provided');
+ }
+
+ $this->callback = $callback;
+ $this->handleNextNode = $handleNextNode;
}
- $this->callback = $callback;
- }
+ /**
+ * {@inheritdoc}
+ */
+ public function handle(\Exception $exception, DataHolderInterface $data)
+ {
+ call_user_func_array($this->callback, array($exception, $data));
- /**
- * {@inheritdoc}
- */
- public function handle(\Exception $exception, DataHolderInterface $data)
- {
- if (true === call_user_func_array($this->callback, array($exception, $data))) {
- $this->handleNextNode($exception, $data);
+ if ($this->handleNextNode) {
+ $this->handleNextNode($exception, $data);
+ }
}
- }
}
View
289 tests/BadaBoom/Tests/ChainNode/CallbackChainNodeTest.php
@@ -11,141 +11,156 @@
*/
class CallbackChainNodeTest extends \PHPUnit_Framework_TestCase
{
- /**
- * @test
- */
- public function shouldBeSubclassOfAbstractChainNode()
- {
- $rc = new \ReflectionClass('BadaBoom\ChainNode\CallbackChainNode');
- $this->assertTrue($rc->isSubclassOf('BadaBoom\ChainNode\AbstractChainNode'));
- }
-
- /**
- * @test
- *
- * @dataProvider provideValidCallbacks
- */
- public function shouldBeConstructedWithCallback($callback)
- {
- new CallbackChainNode($callback);
- }
-
- /**
- * @test
- *
- * @dataProvider provideNoCallableItems
- *
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Invalid callable provided
- */
- public function throwWhenConstructedWithInvalidCallback($invalidCallback)
- {
- new CallbackChainNode($invalidCallback);
- }
-
- /**
- * @test
- */
- public function shouldDelegateHandlingToCallback()
- {
- $is_called = false;
- $exception = new \Exception();
- $data = new DataHolder();
-
- $test = $this;
-
- $node = new CallbackChainNode(function($e, $d) use ($exception, $data, $test, &$is_called) {
- $test->assertSame($exception, $e);
- $test->assertSame($data, $d);
- $is_called = true;
- });
-
- $node->handle($exception, $data);
-
- $this->assertTrue($is_called);
- }
-
- /**
- * @test
- */
- public function shouldGiveControlToNextNodeIfCallbackReturnTrue()
- {
- $exception = new \Exception();
- $data = new DataHolder();
-
- $nextNode = $this->createChainNodeMock();
-
- $node = new CallbackChainNode(function() {
- return true;
- });
- $node->nextNode($nextNode);
-
- $nextNode->expects($this->once())
- ->method('handle')
- ->with($exception, $data)
- ;
-
- $node->handle($exception, $data);
- }
-
- /**
- * @test
- */
- public function shouldNotGiveControlToNextNodeIfCallbackReturnFalse()
- {
- $nextNode = $this->createChainNodeMock();
-
- $node = new CallbackChainNode(function() {
- return false;
- });
- $node->nextNode($nextNode);
-
- $node->handle(new \Exception(), new DataHolder());
- }
-
- /**
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- public function createChainNodeMock()
- {
- return $this->getMock('BadaBoom\ChainNode\ChainNodeInterface');
- }
-
- /**
- * @return array
- */
- public static function provideValidCallbacks()
- {
- $staticMethod = array(__NAMESPACE__.'\StubMethodCall', 'staticMethod');
- $objectMethod = array(new StubMethodCall(), 'objectMethod');
- $closure = function() {};
- $function = 'is_callable';
-
- return array(
- array($staticMethod),
- array($objectMethod),
- array($closure),
- array($function),
- );
- }
-
- /**
- * @return array
- */
- public static function provideNoCallableItems()
- {
- return array(
- array('string'),
- array(1),
- array(12.2),
- array(array()),
- array(false),
- array(null),
- array(new \stdClass()),
- array(array(new \stdClass(), 'no_exist_method')),
- array(array('stdClass', 'no_exist_method')),
- );
- }
+ /**
+ * @test
+ */
+ public function shouldBeSubclassOfAbstractChainNode()
+ {
+ $rc = new \ReflectionClass('BadaBoom\ChainNode\CallbackChainNode');
+ $this->assertTrue($rc->isSubclassOf('BadaBoom\ChainNode\AbstractChainNode'));
+ }
+
+ /**
+ * @test
+ *
+ * @dataProvider provideValidCallbacks
+ */
+ public function shouldBeConstructedWithCallback($callback)
+ {
+ new CallbackChainNode($callback);
+ }
+
+ /**
+ * @test
+ */
+ public function couldBeConstructedWithFlagForProcessingNextNode()
+ {
+ new CallbackChainNode(function(){}, $handleNextNode = false);
+
+ new CallbackChainNode(function(){}, $handleNextNode = true);
+ }
+
+ /**
+ * @test
+ *
+ * @dataProvider provideNoCallableItems
+ *
+ * @expectedException \InvalidArgumentException
+ * @expectedExceptionMessage Invalid callable provided
+ */
+ public function throwWhenConstructedWithInvalidCallback($invalidCallback)
+ {
+ new CallbackChainNode($invalidCallback);
+ }
+
+ /**
+ * @test
+ */
+ public function shouldDelegateHandlingToCallback()
+ {
+ $exception = new \Exception();
+ $data = new DataHolder();
+
+ $callback = $this->getMock('BadaBoom\ChainNode\ChainNodeInterface');
+ $callback
+ ->expects($this->once())
+ ->method('handle')
+ ->with($exception, $data)
+ ;
+
+ $callbackNode = new CallbackChainNode(array($callback, 'handle'));
+ $callbackNode->handle($exception, $data);
+ }
+
+ /**
+ * @test
+ */
+ public function shouldGiveControlToNextNode()
+ {
+ $exception = new \Exception();
+ $data = new DataHolder();
+
+ $nextNode = $this->createChainNodeMock();
+
+ $node = new CallbackChainNode(function() {
+ return true;
@makasim Owner
makasim added a note

is this return make any sense?

@vatson Owner
vatson added a note

Nope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ });
+ $node->nextNode($nextNode);
+
+ $nextNode
+ ->expects($this->once())
+ ->method('handle')
+ ->with($exception, $data)
+ ;
+
+ $node->handle($exception, $data);
+ }
+
+ /**
+ * @test
+ */
+ public function shouldNotGiveControlToNextNode()
+ {
+ $nextNode = $this->createChainNodeMock();
+ $nextNode
+ ->expects($this->never())
+ ->method('handle')
+ ;
+
+ $node = new CallbackChainNode(function() {
+ return false;
+ },
+ $handleNextNode = false
+ );
+
+ $node->nextNode($nextNode);
+
+ $node->handle(new \Exception(), new DataHolder());
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject
+ */
+ public function createChainNodeMock()
+ {
+ return $this->getMock('BadaBoom\ChainNode\ChainNodeInterface');
+ }
+
+ /**
+ * @return array
+ */
+ public static function provideValidCallbacks()
+ {
+ $staticMethod = array(__NAMESPACE__ . '\StubMethodCall', 'staticMethod');
+ $objectMethod = array(new StubMethodCall(), 'objectMethod');
+ $closure = function(){};
+ $function = 'is_callable';
+
+ return array(
+ array($staticMethod),
+ array($objectMethod),
+ array($closure),
+ array($function),
+ );
+ }
+
+ /**
+ * @return array
+ */
+ public static function provideNoCallableItems()
+ {
+ return array(
+ array('string'),
+ array(1),
+ array(12.2),
+ array(array()),
+ array(false),
+ array(null),
+ array(new \stdClass()),
+ array(array(new \stdClass(), 'no_exist_method')),
+ array(array('stdClass', 'no_exist_method')),
+ );
+ }
}
/**
@@ -153,7 +168,7 @@ public static function provideNoCallableItems()
*/
class StubMethodCall
{
- public static function staticMethod() {}
+ public static function staticMethod(){}
- public function objectMethod() {}
+ public function objectMethod(){}
}
Please sign in to comment.
Something went wrong with that request. Please try again.