RedisFactoryTest.php 12 KB

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