PredisProfileTestCase.php 7.7 KB

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