ServerProfileTest.php 8.6 KB

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