ServerProfileTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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\Profiles;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Commands\Processors\ProcessorChain;
  13. /**
  14. *
  15. */
  16. class ServerProfileTest extends StandardTestCase
  17. {
  18. const DEFAULT_PROFILE_VERSION = '2.4';
  19. const DEVELOPMENT_PROFILE_VERSION = '2.6';
  20. /**
  21. * @group disconnected
  22. */
  23. public function testGetVersion()
  24. {
  25. $profile = ServerProfile::get('2.0');
  26. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $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\Profiles\IServerProfile', $profile1);
  38. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $profile2);
  39. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $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\Profiles\IServerProfile', $profile1);
  51. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $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\Profiles\IServerProfile'));
  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\Commands\ConnectionPing', $profile->getCommandClass('ping'));
  117. $this->assertSame('Predis\Commands\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 testCreateCommandWithoutArguments()
  125. {
  126. $profile = ServerProfile::getDefault();
  127. $command = $profile->createCommand('info');
  128. $this->assertInstanceOf('Predis\Commands\ICommand', $command);
  129. $this->assertEquals('INFO', $command->getId());
  130. $this->assertEquals(array(), $command->getArguments());
  131. }
  132. /**
  133. * @group disconnected
  134. */
  135. public function testCreateCommandWithArguments()
  136. {
  137. $profile = ServerProfile::getDefault();
  138. $arguments = array('foo', 'bar');
  139. $command = $profile->createCommand('set', $arguments);
  140. $this->assertInstanceOf('Predis\Commands\ICommand', $command);
  141. $this->assertEquals('SET', $command->getId());
  142. $this->assertEquals($arguments, $command->getArguments());
  143. }
  144. /**
  145. * @group disconnected
  146. * @expectedException Predis\ClientException
  147. * @expectedExceptionMessage 'unknown' is not a registered Redis command
  148. */
  149. public function testCreateUndefinedCommand()
  150. {
  151. $profile = ServerProfile::getDefault();
  152. $profile->createCommand('unknown');
  153. }
  154. /**
  155. * @group disconnected
  156. */
  157. public function testGetDefaultProcessor()
  158. {
  159. $profile = ServerProfile::getDefault();
  160. $this->assertNull($profile->getProcessor());
  161. }
  162. /**
  163. * @group disconnected
  164. */
  165. public function testSetProcessor()
  166. {
  167. $processor = $this->getMock('Predis\Commands\Processors\ICommandProcessor');
  168. $profile = ServerProfile::getDefault();
  169. $profile->setProcessor($processor);
  170. $this->assertSame($processor, $profile->getProcessor());
  171. }
  172. /**
  173. * @group disconnected
  174. */
  175. public function testSetAndUnsetProcessor()
  176. {
  177. $processor = $this->getMock('Predis\Commands\Processors\ICommandProcessor');
  178. $profile = ServerProfile::getDefault();
  179. $profile->setProcessor($processor);
  180. $this->assertSame($processor, $profile->getProcessor());
  181. $profile->setProcessor(null);
  182. $this->assertNull($profile->getProcessor());
  183. }
  184. /**
  185. * @group disconnected
  186. * @todo Could it be that objects passed to the return callback of a mocked
  187. * method are cloned instead of being passed by reference?
  188. */
  189. public function testSingleProcessor()
  190. {
  191. $argsRef = null;
  192. $processor = $this->getMock('Predis\Commands\Processors\ICommandProcessor');
  193. $processor->expects($this->once())
  194. ->method('process')
  195. ->with($this->isInstanceOf('Predis\Commands\ICommand'))
  196. ->will($this->returnCallback(function($cmd) use(&$argsRef) {
  197. $cmd->setRawArguments($argsRef = array_map('strtoupper', $cmd->getArguments()));
  198. }));
  199. $profile = ServerProfile::getDefault();
  200. $profile->setProcessor($processor);
  201. $command = $profile->createCommand('set', array('foo', 'bar'));
  202. $this->assertSame(array('FOO', 'BAR'), $argsRef);
  203. }
  204. /**
  205. * @group disconnected
  206. */
  207. public function testChainOfProcessors()
  208. {
  209. $processor = $this->getMock('Predis\Commands\Processors\ICommandProcessor');
  210. $processor->expects($this->exactly(2))
  211. ->method('process');
  212. $chain = new ProcessorChain();
  213. $chain->add($processor);
  214. $chain->add($processor);
  215. $profile = ServerProfile::getDefault();
  216. $profile->setProcessor($chain);
  217. $profile->createCommand('info');
  218. }
  219. }