RedisFactoryTest.php 12 KB

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