diff --git a/src/Ratchet/Resource/Command/Composite.php b/src/Ratchet/Resource/Command/Composite.php index 1a0f8d3..5e7a056 100644 --- a/src/Ratchet/Resource/Command/Composite.php +++ b/src/Ratchet/Resource/Command/Composite.php @@ -6,9 +6,17 @@ class Composite extends \SplQueue implements CommandInterface { /** * Add another Command to the stack * Unlike a true composite the enqueue flattens a composite parameter into leafs - * @param CommandInterface + * @param CommandInterface|null */ - public function enqueue(CommandInterface $command = null) { + public function enqueue($command) { + if (null === $command) { + return; + } + + if (!($command instanceof CommandInterface)) { + throw new \InvalidArgumentException("Parameter MUST implement Ratchet.Component.CommandInterface"); + } + if ($command instanceof self) { foreach ($command as $cmd) { $this->enqueue($cmd); @@ -17,9 +25,7 @@ class Composite extends \SplQueue implements CommandInterface { return; } - if (null !== $command) { - parent::enqueue($command); - } + parent::enqueue($command); } /** diff --git a/tests/Ratchet/Tests/Resource/Command/CompositeTest.php b/tests/Ratchet/Tests/Resource/Command/CompositeTest.php new file mode 100644 index 0000000..d6e255b --- /dev/null +++ b/tests/Ratchet/Tests/Resource/Command/CompositeTest.php @@ -0,0 +1,64 @@ +_comp = new Composite; + } + + protected function newNull() { + return new NullAction(new Connection(new FakeSocket)); + } + + public function testCanEnqueueNull() { + $count = $this->_comp->count(); + + $this->_comp->enqueue(null); + + $this->assertEquals($count, $this->_comp->count()); + } + + public function testEnqueueCommand() { + $count = $this->_comp->count(); + + $this->_comp->enqueue($this->newNull()); + + $this->assertEquals($count + 1, $this->_comp->count()); + } + + public function badEnqueueProviders() { + return array( + array(array()) + , array('string') + ); + } + + /** + * @dataProvider badEnqueueProviders + */ + public function testCanNotPassOtherThings($object) { + $this->setExpectedException('InvalidArgumentException'); + + $this->_comp->enqueue($object); + } + + public function testCompositeComposite() { + $compTwo = new Composite; + $compTwo->enqueue($this->newNull()); + $compTwo->enqueue($this->newNull()); + + $this->_comp->enqueue($this->newNull()); + $this->_comp->enqueue($compTwo); + + $this->assertEquals(3, $this->_comp->count()); + } +} \ No newline at end of file