123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- /*
- * This file is part of the Predis package.
- *
- * (c) Daniele Alessandri <suppakilla@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Predis\Command\Processor;
- use PredisTestCase;
- /**
- *
- */
- class ProcessorChainTest extends PredisTestCase
- {
- /**
- * @group disconnected
- */
- public function testConstructor()
- {
- $chain = new ProcessorChain();
- $this->assertInstanceOf('Predis\Command\Processor\ProcessorInterface', $chain);
- $this->assertEmpty($chain->getProcessors());
- }
- /**
- * @group disconnected
- */
- public function testConstructorWithProcessorsArray()
- {
- $processors = array(
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- );
- $chain = new ProcessorChain($processors);
- $this->assertSame($processors, $chain->getProcessors());
- }
- /**
- * @group disconnected
- */
- public function testCountProcessors()
- {
- $processors = array(
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- );
- $chain = new ProcessorChain($processors);
- $this->assertEquals(2, $chain->count());
- }
- /**
- * @group disconnected
- */
- public function testAddProcessors()
- {
- $processors = array(
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- );
- $chain = new ProcessorChain();
- $chain->add($processors[0]);
- $chain->add($processors[1]);
- $this->assertSame($processors, $chain->getProcessors());
- }
- /**
- * @group disconnected
- */
- public function testAddMoreProcessors()
- {
- $processors1 = array(
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- );
- $processors2 = array(
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- );
- $chain = new ProcessorChain($processors1);
- $chain->add($processors2[0]);
- $chain->add($processors2[1]);
- $this->assertSame(array_merge($processors1, $processors2), $chain->getProcessors());
- }
- /**
- * @group disconnected
- */
- public function testRemoveProcessors()
- {
- $processors = array(
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- );
- $chain = new ProcessorChain($processors);
- $chain->remove($processors[0]);
- $this->assertSame(array($processors[1]), $chain->getProcessors());
- $chain->remove($processors[1]);
- $this->assertEmpty($chain->getProcessors());
- }
- /**
- * @group disconnected
- */
- public function testRemoveProcessorNotInChain()
- {
- $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
- $processors = array(
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- );
- $chain = new ProcessorChain($processors);
- $chain->remove($processor);
- $this->assertSame($processors, $chain->getProcessors());
- }
- /**
- * @group disconnected
- */
- public function testRemoveProcessorFromEmptyChain()
- {
- $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
- $chain = new ProcessorChain();
- $this->assertEmpty($chain->getProcessors());
- $chain->remove($processor);
- $this->assertEmpty($chain->getProcessors());
- }
- /**
- * @group disconnected
- */
- public function testOffsetGet()
- {
- $processors = array(
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- );
- $chain = new ProcessorChain($processors);
- $this->assertSame($processors[0], $chain[0]);
- $this->assertSame($processors[1], $chain[1]);
- }
- /**
- * @group disconnected
- */
- public function testOffsetIsset()
- {
- $processors = array(
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- );
- $chain = new ProcessorChain($processors);
- $this->assertTrue(isset($chain[0]));
- $this->assertTrue(isset($chain[1]));
- $this->assertFalse(isset($chain[2]));
- }
- /**
- * @group disconnected
- */
- public function testOffsetSet()
- {
- $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
- $chain = new ProcessorChain();
- $chain[0] = $processor;
- $this->assertSame($processor, $chain[0]);
- }
- /**
- * @group disconnected
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Processor chain accepts only instances of `Predis\Command\Processor\ProcessorInterface`
- */
- public function testOffsetSetWithInvalidType()
- {
- $chain = new ProcessorChain();
- $chain[0] = new \stdClass();
- }
- /**
- * @group disconnected
- */
- public function testGetIterator()
- {
- $processors = array(
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- $this->getMock('Predis\Command\Processor\ProcessorInterface'),
- );
- $chain = new ProcessorChain($processors);
- $this->assertSame($processors, iterator_to_array($chain->getIterator()));
- }
- /**
- * @group disconnected
- */
- public function testProcessChain()
- {
- $command = $this->getMock('Predis\Command\CommandInterface');
- $processor1 = $this->getMock('Predis\Command\Processor\ProcessorInterface');
- $processor1->expects($this->once())->method('process')->with($command);
- $processor2 = $this->getMock('Predis\Command\Processor\ProcessorInterface');
- $processor2->expects($this->once())->method('process')->with($command);
- $processors = array($processor1, $processor2);
- $chain = new ProcessorChain($processors);
- $chain->process($command);
- }
- }
|