ProcessorChainTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\Command\Processor;
  11. use PredisTestCase;
  12. /**
  13. *
  14. */
  15. class ProcessorChainTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructor()
  21. {
  22. $chain = new ProcessorChain();
  23. $this->assertInstanceOf('Predis\Command\Processor\ProcessorInterface', $chain);
  24. $this->assertEmpty($chain->getProcessors());
  25. }
  26. /**
  27. * @group disconnected
  28. */
  29. public function testConstructorWithProcessorsArray()
  30. {
  31. $processors = array(
  32. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  33. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  34. );
  35. $chain = new ProcessorChain($processors);
  36. $this->assertSame($processors, $chain->getProcessors());
  37. }
  38. /**
  39. * @group disconnected
  40. */
  41. public function testCountProcessors()
  42. {
  43. $processors = array(
  44. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  45. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  46. );
  47. $chain = new ProcessorChain($processors);
  48. $this->assertEquals(2, $chain->count());
  49. }
  50. /**
  51. * @group disconnected
  52. */
  53. public function testAddProcessors()
  54. {
  55. $processors = array(
  56. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  57. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  58. );
  59. $chain = new ProcessorChain();
  60. $chain->add($processors[0]);
  61. $chain->add($processors[1]);
  62. $this->assertSame($processors, $chain->getProcessors());
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testAddMoreProcessors()
  68. {
  69. $processors1 = array(
  70. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  71. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  72. );
  73. $processors2 = array(
  74. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  75. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  76. );
  77. $chain = new ProcessorChain($processors1);
  78. $chain->add($processors2[0]);
  79. $chain->add($processors2[1]);
  80. $this->assertSame(array_merge($processors1, $processors2), $chain->getProcessors());
  81. }
  82. /**
  83. * @group disconnected
  84. */
  85. public function testRemoveProcessors()
  86. {
  87. $processors = array(
  88. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  89. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  90. );
  91. $chain = new ProcessorChain($processors);
  92. $chain->remove($processors[0]);
  93. $this->assertSame(array($processors[1]), $chain->getProcessors());
  94. $chain->remove($processors[1]);
  95. $this->assertEmpty($chain->getProcessors());
  96. }
  97. /**
  98. * @group disconnected
  99. */
  100. public function testRemoveProcessorNotInChain()
  101. {
  102. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  103. $processors = array(
  104. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  105. $this->getMock('Predis\Command\Processor\ProcessorInterface'),
  106. );
  107. $chain = new ProcessorChain($processors);
  108. $chain->remove($processor);
  109. $this->assertSame($processors, $chain->getProcessors());
  110. }
  111. /**
  112. * @group disconnected
  113. */
  114. public function testRemoveProcessorFromEmptyChain()
  115. {
  116. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  117. $chain = new ProcessorChain();
  118. $this->assertEmpty($chain->getProcessors());
  119. $chain->remove($processor);
  120. $this->assertEmpty($chain->getProcessors());
  121. }
  122. /**
  123. * @group disconnected
  124. */
  125. public function testProcessChain()
  126. {
  127. $command = $this->getMock('Predis\Command\CommandInterface');
  128. $processor1 = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  129. $processor1->expects($this->once())->method('process')->with($command);
  130. $processor2 = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  131. $processor2->expects($this->once())->method('process')->with($command);
  132. $processors = array($processor1, $processor2);
  133. $chain = new ProcessorChain($processors);
  134. $chain->process($command);
  135. }
  136. }