ProcessorChainTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Commands\Processors;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. *
  14. */
  15. class ProcessorChainTest extends StandardTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructor()
  21. {
  22. $chain = new ProcessorChain();
  23. $this->assertInstanceOf('Predis\Commands\Processors\ICommandProcessor', $chain);
  24. $this->assertInstanceOf('Predis\Commands\Processors\ICommandProcessorChain', $chain);
  25. $this->assertEmpty($chain->getProcessors());
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testConstructorWithProcessorsArray()
  31. {
  32. $processors = array(
  33. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  34. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  35. );
  36. $chain = new ProcessorChain($processors);
  37. $this->assertSame($processors, $chain->getProcessors());
  38. }
  39. /**
  40. * @group disconnected
  41. */
  42. public function testCountProcessors()
  43. {
  44. $processors = array(
  45. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  46. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  47. );
  48. $chain = new ProcessorChain($processors);
  49. $this->assertEquals(2, $chain->count());
  50. }
  51. /**
  52. * @group disconnected
  53. */
  54. public function testAddProcessors()
  55. {
  56. $processors = array(
  57. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  58. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  59. );
  60. $chain = new ProcessorChain();
  61. $chain->add($processors[0]);
  62. $chain->add($processors[1]);
  63. $this->assertSame($processors, $chain->getProcessors());
  64. }
  65. /**
  66. * @group disconnected
  67. */
  68. public function testAddMoreProcessors()
  69. {
  70. $processors1 = array(
  71. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  72. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  73. );
  74. $processors2 = array(
  75. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  76. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  77. );
  78. $chain = new ProcessorChain($processors1);
  79. $chain->add($processors2[0]);
  80. $chain->add($processors2[1]);
  81. $this->assertSame(array_merge($processors1, $processors2), $chain->getProcessors());
  82. }
  83. /**
  84. * @group disconnected
  85. */
  86. public function testRemoveProcessors()
  87. {
  88. $processors = array(
  89. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  90. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  91. );
  92. $chain = new ProcessorChain($processors);
  93. $chain->remove($processors[0]);
  94. $this->assertSame(array($processors[1]), $chain->getProcessors());
  95. $chain->remove($processors[1]);
  96. $this->assertEmpty($chain->getProcessors());
  97. }
  98. /**
  99. * @group disconnected
  100. */
  101. public function testRemoveProcessorNotInChain()
  102. {
  103. $processor = $this->getMock('Predis\Commands\Processors\ICommandProcessor');
  104. $processors = array(
  105. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  106. $this->getMock('Predis\Commands\Processors\ICommandProcessor'),
  107. );
  108. $chain = new ProcessorChain($processors);
  109. $chain->remove($processor);
  110. $this->assertSame($processors, $chain->getProcessors());
  111. }
  112. /**
  113. * @group disconnected
  114. */
  115. public function testRemoveProcessorFromEmptyChain()
  116. {
  117. $processor = $this->getMock('Predis\Commands\Processors\ICommandProcessor');
  118. $chain = new ProcessorChain();
  119. $this->assertEmpty($chain->getProcessors());
  120. $chain->remove($processor);
  121. $this->assertEmpty($chain->getProcessors());
  122. }
  123. /**
  124. * @group disconnected
  125. */
  126. public function testProcessChain()
  127. {
  128. $command = $this->getMock('Predis\Commands\ICommand');
  129. $processor1 = $this->getMock('Predis\Commands\Processors\ICommandProcessor');
  130. $processor1->expects($this->once())->method('process')->with($command);
  131. $processor2 = $this->getMock('Predis\Commands\Processors\ICommandProcessor');
  132. $processor2->expects($this->once())->method('process')->with($command);
  133. $processors = array($processor1, $processor2);
  134. $chain = new ProcessorChain($processors);
  135. $chain->process($command);
  136. }
  137. }