PredisProfileTestCase.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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\Profile;
  11. use PredisTestCase;
  12. use Predis\Command\CommandInterface;
  13. use Predis\Command\Processor\ProcessorChain;
  14. /**
  15. *
  16. */
  17. abstract class PredisProfileTestCase extends PredisTestCase
  18. {
  19. /**
  20. * Returns a new instance of the tested profile.
  21. *
  22. * @param string $version Version of Redis.
  23. * @return ProfileInterface
  24. */
  25. protected function getProfile($version = null)
  26. {
  27. $this->markTestIncomplete("Server profile must be defined in ".get_class($this));
  28. }
  29. /**
  30. * Returns the expected version string for the tested profile.
  31. *
  32. * @return string Version string.
  33. */
  34. abstract protected function getExpectedVersion();
  35. /**
  36. * Returns the expected list of commands supported by the tested profile.
  37. *
  38. * @return array List of supported commands.
  39. */
  40. abstract protected function getExpectedCommands();
  41. /**
  42. * Returns the list of commands supported by the current
  43. * server profile.
  44. *
  45. * @param ProfileInterface $profile Server profile instance.
  46. * @return array
  47. */
  48. protected function getCommands(ProfileInterface $profile)
  49. {
  50. $commands = $profile->getSupportedCommands();
  51. return array_keys($commands);
  52. }
  53. /**
  54. * @group disconnected
  55. */
  56. public function testGetVersion()
  57. {
  58. $profile = $this->getProfile();
  59. $this->assertEquals($this->getExpectedVersion(), $profile->getVersion());
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testSupportedCommands()
  65. {
  66. $profile = $this->getProfile();
  67. $expected = $this->getExpectedCommands();
  68. $commands = $this->getCommands($profile);
  69. $this->assertSame($expected, $commands);
  70. }
  71. /**
  72. * @group disconnected
  73. */
  74. public function testToString()
  75. {
  76. $this->assertEquals($this->getExpectedVersion(), $this->getProfile());
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testSupportCommand()
  82. {
  83. $profile = $this->getProfile();
  84. $this->assertTrue($profile->supportsCommand('info'));
  85. $this->assertTrue($profile->supportsCommand('INFO'));
  86. $this->assertFalse($profile->supportsCommand('unknown'));
  87. $this->assertFalse($profile->supportsCommand('UNKNOWN'));
  88. }
  89. /**
  90. * @group disconnected
  91. */
  92. public function testSupportCommands()
  93. {
  94. $profile = $this->getProfile();
  95. $this->assertTrue($profile->supportsCommands(array('get', 'set')));
  96. $this->assertTrue($profile->supportsCommands(array('GET', 'SET')));
  97. $this->assertFalse($profile->supportsCommands(array('get', 'unknown')));
  98. $this->assertFalse($profile->supportsCommands(array('unknown1', 'unknown2')));
  99. }
  100. /**
  101. * @group disconnected
  102. */
  103. public function testGetCommandClass()
  104. {
  105. $profile = $this->getProfile();
  106. $this->assertSame('Predis\Command\ConnectionPing', $profile->getCommandClass('ping'));
  107. $this->assertSame('Predis\Command\ConnectionPing', $profile->getCommandClass('PING'));
  108. $this->assertNull($profile->getCommandClass('unknown'));
  109. $this->assertNull($profile->getCommandClass('UNKNOWN'));
  110. }
  111. /**
  112. * @group disconnected
  113. */
  114. public function testDefineCommand()
  115. {
  116. $profile = $this->getProfile();
  117. $command = $this->getMock('Predis\Command\CommandInterface');
  118. $profile->defineCommand('mock', get_class($command));
  119. $this->assertTrue($profile->supportsCommand('mock'));
  120. $this->assertTrue($profile->supportsCommand('MOCK'));
  121. $this->assertSame(get_class($command), $profile->getCommandClass('mock'));
  122. }
  123. /**
  124. * @group disconnected
  125. * @expectedException \InvalidArgumentException
  126. * @expectedExceptionMessage The class 'stdClass' is not a valid command class.
  127. */
  128. public function testDefineInvalidCommand()
  129. {
  130. $profile = $this->getProfile();
  131. $profile->defineCommand('mock', 'stdClass');
  132. }
  133. /**
  134. * @group disconnected
  135. */
  136. public function testCreateCommandWithoutArguments()
  137. {
  138. $profile = $this->getProfile();
  139. $command = $profile->createCommand('info');
  140. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  141. $this->assertEquals('INFO', $command->getId());
  142. $this->assertEquals(array(), $command->getArguments());
  143. }
  144. /**
  145. * @group disconnected
  146. */
  147. public function testCreateCommandWithArguments()
  148. {
  149. $profile = $this->getProfile();
  150. $arguments = array('foo', 'bar');
  151. $command = $profile->createCommand('set', $arguments);
  152. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  153. $this->assertEquals('SET', $command->getId());
  154. $this->assertEquals($arguments, $command->getArguments());
  155. }
  156. /**
  157. * @group disconnected
  158. * @expectedException \Predis\ClientException
  159. * @expectedExceptionMessage Command 'UNKNOWN' is not a registered Redis command.
  160. */
  161. public function testCreateUndefinedCommand()
  162. {
  163. $profile = $this->getProfile();
  164. $profile->createCommand('unknown');
  165. }
  166. /**
  167. * @group disconnected
  168. */
  169. public function testGetDefaultProcessor()
  170. {
  171. $profile = $this->getProfile();
  172. $this->assertNull($profile->getProcessor());
  173. }
  174. /**
  175. * @group disconnected
  176. */
  177. public function testSetProcessor()
  178. {
  179. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  180. $profile = $this->getProfile();
  181. $profile->setProcessor($processor);
  182. $this->assertSame($processor, $profile->getProcessor());
  183. }
  184. /**
  185. * @group disconnected
  186. */
  187. public function testSetAndUnsetProcessor()
  188. {
  189. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  190. $profile = $this->getProfile();
  191. $profile->setProcessor($processor);
  192. $this->assertSame($processor, $profile->getProcessor());
  193. $profile->setProcessor(null);
  194. $this->assertNull($profile->getProcessor());
  195. }
  196. /**
  197. * @group disconnected
  198. * @todo Could it be that objects passed to the return callback of a mocked
  199. * method are cloned instead of being passed by reference?
  200. */
  201. public function testSingleProcessor()
  202. {
  203. $argsRef = null;
  204. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  205. $processor->expects($this->once())
  206. ->method('process')
  207. ->with($this->isInstanceOf('Predis\Command\CommandInterface'))
  208. ->will($this->returnCallback(function (CommandInterface $cmd) use (&$argsRef) {
  209. $cmd->setRawArguments($argsRef = array_map('strtoupper', $cmd->getArguments()));
  210. }));
  211. $profile = $this->getProfile();
  212. $profile->setProcessor($processor);
  213. $profile->createCommand('set', array('foo', 'bar'));
  214. $this->assertSame(array('FOO', 'BAR'), $argsRef);
  215. }
  216. /**
  217. * @group disconnected
  218. */
  219. public function testChainOfProcessors()
  220. {
  221. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  222. $processor->expects($this->exactly(2))
  223. ->method('process');
  224. $chain = new ProcessorChain();
  225. $chain->add($processor);
  226. $chain->add($processor);
  227. $profile = $this->getProfile();
  228. $profile->setProcessor($chain);
  229. $profile->createCommand('info');
  230. }
  231. }