PredisProfileTestCase.php 7.6 KB

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