RedisProfileTestCase.php 7.7 KB

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