Composite Command updates

Unit tested Composite
Corrected interface to be E_STRICT compliant
Refactored enqueue method
This commit is contained in:
Chris Boden 2012-04-28 18:25:57 -04:00
parent a4f9da8815
commit 228d0b8627
2 changed files with 75 additions and 5 deletions

View File

@ -6,9 +6,17 @@ class Composite extends \SplQueue implements CommandInterface {
/** /**
* Add another Command to the stack * Add another Command to the stack
* Unlike a true composite the enqueue flattens a composite parameter into leafs * 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) { if ($command instanceof self) {
foreach ($command as $cmd) { foreach ($command as $cmd) {
$this->enqueue($cmd); $this->enqueue($cmd);
@ -17,9 +25,7 @@ class Composite extends \SplQueue implements CommandInterface {
return; return;
} }
if (null !== $command) { parent::enqueue($command);
parent::enqueue($command);
}
} }
/** /**

View File

@ -0,0 +1,64 @@
<?php
namespace Ratchet\Tests\Resource\Command;
use Ratchet\Resource\Command\Composite;
use Ratchet\Resource\Connection;
use Ratchet\Tests\Mock\FakeSocket;
use Ratchet\Resource\Command\Action\Null as NullAction;
/**
* @covers Ratchet\Resource\Command\Composite
*/
class CompositeTestnope extends \PHPUnit_Framework_TestCase {
protected $_comp;
public function setUp() {
$this->_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());
}
}