RedisFactoryTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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\Command;
  11. use Predis\Command\CommandInterface;
  12. use Predis\Command\Processor\ProcessorChain;
  13. use PredisTestCase;
  14. /**
  15. *
  16. */
  17. class RedisFactoryTest extends PredisTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testGetVersion()
  23. {
  24. $factory = new RedisFactory();
  25. $this->assertSame('3.2', $factory->getVersion());
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testSupportedCommands()
  31. {
  32. $factory = new RedisFactory();
  33. $expected = $this->getExpectedCommands();
  34. $commands = $this->getCommands($factory);
  35. $this->assertSame($expected, $commands);
  36. }
  37. /**
  38. * @group disconnected
  39. */
  40. public function testSupportCommand()
  41. {
  42. $factory = new RedisFactory();
  43. $this->assertTrue($factory->supportsCommand('info'));
  44. $this->assertTrue($factory->supportsCommand('INFO'));
  45. $this->assertFalse($factory->supportsCommand('unknown'));
  46. $this->assertFalse($factory->supportsCommand('UNKNOWN'));
  47. }
  48. /**
  49. * @group disconnected
  50. */
  51. public function testSupportCommands()
  52. {
  53. $factory = new RedisFactory();
  54. $this->assertTrue($factory->supportsCommands(array('get', 'set')));
  55. $this->assertTrue($factory->supportsCommands(array('GET', 'SET')));
  56. $this->assertFalse($factory->supportsCommands(array('get', 'unknown')));
  57. $this->assertFalse($factory->supportsCommands(array('unknown1', 'unknown2')));
  58. }
  59. /**
  60. * @group disconnected
  61. */
  62. public function testGetCommandClass()
  63. {
  64. $factory = new RedisFactory();
  65. $this->assertSame('Predis\Command\Redis\ConnectionPing', $factory->getCommandClass('ping'));
  66. $this->assertSame('Predis\Command\Redis\ConnectionPing', $factory->getCommandClass('PING'));
  67. $this->assertNull($factory->getCommandClass('unknown'));
  68. $this->assertNull($factory->getCommandClass('UNKNOWN'));
  69. }
  70. /**
  71. * @group disconnected
  72. */
  73. public function testDefineCommand()
  74. {
  75. $factory = new RedisFactory();
  76. $command = $this->getMock('Predis\Command\CommandInterface');
  77. $factory->defineCommand('mock', get_class($command));
  78. $this->assertTrue($factory->supportsCommand('mock'));
  79. $this->assertTrue($factory->supportsCommand('MOCK'));
  80. $this->assertSame(get_class($command), $factory->getCommandClass('mock'));
  81. }
  82. /**
  83. * @group disconnected
  84. * @expectedException \InvalidArgumentException
  85. * @expectedExceptionMessage The class 'stdClass' is not a valid command class.
  86. */
  87. public function testDefineInvalidCommand()
  88. {
  89. $factory = new RedisFactory();
  90. $factory->defineCommand('mock', 'stdClass');
  91. }
  92. /**
  93. * @group disconnected
  94. */
  95. public function testCreateCommandWithoutArguments()
  96. {
  97. $factory = new RedisFactory();
  98. $command = $factory->createCommand('info');
  99. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  100. $this->assertEquals('INFO', $command->getId());
  101. $this->assertEquals(array(), $command->getArguments());
  102. }
  103. /**
  104. * @group disconnected
  105. */
  106. public function testCreateCommandWithArguments()
  107. {
  108. $factory = new RedisFactory();
  109. $arguments = array('foo', 'bar');
  110. $command = $factory->createCommand('set', $arguments);
  111. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  112. $this->assertEquals('SET', $command->getId());
  113. $this->assertEquals($arguments, $command->getArguments());
  114. }
  115. /**
  116. * @group disconnected
  117. * @expectedException \Predis\ClientException
  118. * @expectedExceptionMessage Command 'UNKNOWN' is not a registered Redis command.
  119. */
  120. public function testCreateUndefinedCommand()
  121. {
  122. $factory = new RedisFactory();
  123. $factory->createCommand('unknown');
  124. }
  125. /**
  126. * @group disconnected
  127. */
  128. public function testGetDefaultProcessor()
  129. {
  130. $factory = new RedisFactory();
  131. $this->assertNull($factory->getProcessor());
  132. }
  133. /**
  134. * @group disconnected
  135. */
  136. public function testSetProcessor()
  137. {
  138. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  139. $factory = new RedisFactory();
  140. $factory->setProcessor($processor);
  141. $this->assertSame($processor, $factory->getProcessor());
  142. }
  143. /**
  144. * @group disconnected
  145. */
  146. public function testSetAndUnsetProcessor()
  147. {
  148. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  149. $factory = new RedisFactory();
  150. $factory->setProcessor($processor);
  151. $this->assertSame($processor, $factory->getProcessor());
  152. $factory->setProcessor(null);
  153. $this->assertNull($factory->getProcessor());
  154. }
  155. /**
  156. * @group disconnected
  157. */
  158. public function testSingleProcessor()
  159. {
  160. // Could it be that objects passed to the return callback of a mocked
  161. // method are cloned instead of being passed by reference?
  162. $argsRef = null;
  163. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  164. $processor->expects($this->once())
  165. ->method('process')
  166. ->with($this->isInstanceOf('Predis\Command\CommandInterface'))
  167. ->will($this->returnCallback(function (CommandInterface $cmd) use (&$argsRef) {
  168. $cmd->setRawArguments($argsRef = array_map('strtoupper', $cmd->getArguments()));
  169. }));
  170. $factory = new RedisFactory();
  171. $factory->setProcessor($processor);
  172. $factory->createCommand('set', array('foo', 'bar'));
  173. $this->assertSame(array('FOO', 'BAR'), $argsRef);
  174. }
  175. /**
  176. * @group disconnected
  177. */
  178. public function testChainOfProcessors()
  179. {
  180. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  181. $processor->expects($this->exactly(2))
  182. ->method('process');
  183. $chain = new ProcessorChain();
  184. $chain->add($processor);
  185. $chain->add($processor);
  186. $factory = new RedisFactory();
  187. $factory->setProcessor($chain);
  188. $factory->createCommand('info');
  189. }
  190. // ******************************************************************** //
  191. // ---- HELPER METHODS ------------------------------------------------ //
  192. // ******************************************************************** //
  193. /**
  194. * Returns the list of commands supported by the specified command factory.
  195. *
  196. * @param FactoryInterface $factory Command factory instance.
  197. *
  198. * @return array
  199. */
  200. protected function getCommands(FactoryInterface $factory)
  201. {
  202. $commands = $factory->getSupportedCommands();
  203. return array_keys($commands);
  204. }
  205. /**
  206. * Returns the expected list of commands supported by the tested factory.
  207. *
  208. * @return array List of supported commands.
  209. */
  210. protected function getExpectedCommands()
  211. {
  212. return array(
  213. 0 => 'EXISTS',
  214. 1 => 'DEL',
  215. 2 => 'TYPE',
  216. 3 => 'KEYS',
  217. 4 => 'RANDOMKEY',
  218. 5 => 'RENAME',
  219. 6 => 'RENAMENX',
  220. 7 => 'EXPIRE',
  221. 8 => 'EXPIREAT',
  222. 9 => 'TTL',
  223. 10 => 'MOVE',
  224. 11 => 'SORT',
  225. 12 => 'DUMP',
  226. 13 => 'RESTORE',
  227. 14 => 'SET',
  228. 15 => 'SETNX',
  229. 16 => 'MSET',
  230. 17 => 'MSETNX',
  231. 18 => 'GET',
  232. 19 => 'MGET',
  233. 20 => 'GETSET',
  234. 21 => 'INCR',
  235. 22 => 'INCRBY',
  236. 23 => 'DECR',
  237. 24 => 'DECRBY',
  238. 25 => 'RPUSH',
  239. 26 => 'LPUSH',
  240. 27 => 'LLEN',
  241. 28 => 'LRANGE',
  242. 29 => 'LTRIM',
  243. 30 => 'LINDEX',
  244. 31 => 'LSET',
  245. 32 => 'LREM',
  246. 33 => 'LPOP',
  247. 34 => 'RPOP',
  248. 35 => 'RPOPLPUSH',
  249. 36 => 'SADD',
  250. 37 => 'SREM',
  251. 38 => 'SPOP',
  252. 39 => 'SMOVE',
  253. 40 => 'SCARD',
  254. 41 => 'SISMEMBER',
  255. 42 => 'SINTER',
  256. 43 => 'SINTERSTORE',
  257. 44 => 'SUNION',
  258. 45 => 'SUNIONSTORE',
  259. 46 => 'SDIFF',
  260. 47 => 'SDIFFSTORE',
  261. 48 => 'SMEMBERS',
  262. 49 => 'SRANDMEMBER',
  263. 50 => 'ZADD',
  264. 51 => 'ZINCRBY',
  265. 52 => 'ZREM',
  266. 53 => 'ZRANGE',
  267. 54 => 'ZREVRANGE',
  268. 55 => 'ZRANGEBYSCORE',
  269. 56 => 'ZCARD',
  270. 57 => 'ZSCORE',
  271. 58 => 'ZREMRANGEBYSCORE',
  272. 59 => 'PING',
  273. 60 => 'AUTH',
  274. 61 => 'SELECT',
  275. 62 => 'ECHO',
  276. 63 => 'QUIT',
  277. 64 => 'INFO',
  278. 65 => 'SLAVEOF',
  279. 66 => 'MONITOR',
  280. 67 => 'DBSIZE',
  281. 68 => 'FLUSHDB',
  282. 69 => 'FLUSHALL',
  283. 70 => 'SAVE',
  284. 71 => 'BGSAVE',
  285. 72 => 'LASTSAVE',
  286. 73 => 'SHUTDOWN',
  287. 74 => 'BGREWRITEAOF',
  288. 75 => 'SETEX',
  289. 76 => 'APPEND',
  290. 77 => 'SUBSTR',
  291. 78 => 'BLPOP',
  292. 79 => 'BRPOP',
  293. 80 => 'ZUNIONSTORE',
  294. 81 => 'ZINTERSTORE',
  295. 82 => 'ZCOUNT',
  296. 83 => 'ZRANK',
  297. 84 => 'ZREVRANK',
  298. 85 => 'ZREMRANGEBYRANK',
  299. 86 => 'HSET',
  300. 87 => 'HSETNX',
  301. 88 => 'HMSET',
  302. 89 => 'HINCRBY',
  303. 90 => 'HGET',
  304. 91 => 'HMGET',
  305. 92 => 'HDEL',
  306. 93 => 'HEXISTS',
  307. 94 => 'HLEN',
  308. 95 => 'HKEYS',
  309. 96 => 'HVALS',
  310. 97 => 'HGETALL',
  311. 98 => 'MULTI',
  312. 99 => 'EXEC',
  313. 100 => 'DISCARD',
  314. 101 => 'SUBSCRIBE',
  315. 102 => 'UNSUBSCRIBE',
  316. 103 => 'PSUBSCRIBE',
  317. 104 => 'PUNSUBSCRIBE',
  318. 105 => 'PUBLISH',
  319. 106 => 'CONFIG',
  320. 107 => 'PERSIST',
  321. 108 => 'STRLEN',
  322. 109 => 'SETRANGE',
  323. 110 => 'GETRANGE',
  324. 111 => 'SETBIT',
  325. 112 => 'GETBIT',
  326. 113 => 'RPUSHX',
  327. 114 => 'LPUSHX',
  328. 115 => 'LINSERT',
  329. 116 => 'BRPOPLPUSH',
  330. 117 => 'ZREVRANGEBYSCORE',
  331. 118 => 'WATCH',
  332. 119 => 'UNWATCH',
  333. 120 => 'OBJECT',
  334. 121 => 'SLOWLOG',
  335. 122 => 'CLIENT',
  336. 123 => 'PTTL',
  337. 124 => 'PEXPIRE',
  338. 125 => 'PEXPIREAT',
  339. 126 => 'MIGRATE',
  340. 127 => 'PSETEX',
  341. 128 => 'INCRBYFLOAT',
  342. 129 => 'BITOP',
  343. 130 => 'BITCOUNT',
  344. 131 => 'HINCRBYFLOAT',
  345. 132 => 'EVAL',
  346. 133 => 'EVALSHA',
  347. 134 => 'SCRIPT',
  348. 135 => 'TIME',
  349. 136 => 'SENTINEL',
  350. 137 => 'SCAN',
  351. 138 => 'BITPOS',
  352. 139 => 'SSCAN',
  353. 140 => 'ZSCAN',
  354. 141 => 'ZLEXCOUNT',
  355. 142 => 'ZRANGEBYLEX',
  356. 143 => 'ZREMRANGEBYLEX',
  357. 144 => 'ZREVRANGEBYLEX',
  358. 145 => 'HSCAN',
  359. 146 => 'PUBSUB',
  360. 147 => 'PFADD',
  361. 148 => 'PFCOUNT',
  362. 149 => 'PFMERGE',
  363. 150 => 'COMMAND',
  364. 151 => 'HSTRLEN',
  365. 152 => 'BITFIELD',
  366. 153 => 'GEOADD',
  367. 154 => 'GEOHASH',
  368. 155 => 'GEOPOS',
  369. 156 => 'GEODIST',
  370. 157 => 'GEORADIUS',
  371. 158 => 'GEORADIUSBYMEMBER',
  372. );
  373. }
  374. }