PredisProfileTestCase.php 7.7 KB

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