KeySortTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /**
  12. * @group commands
  13. * @group realm-key
  14. */
  15. class KeySortTest extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\KeySort';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'SORT';
  30. }
  31. /**
  32. * Utility method to to an LPUSH of some unordered values on a key.
  33. *
  34. * @param Predis\Client $redis Redis client instance.
  35. * @param string $key Target key
  36. * @return array
  37. */
  38. protected function lpushUnorderedList(Predis\Client $redis, $key)
  39. {
  40. $list = array(2, 100, 3, 1, 30, 10);
  41. $redis->lpush($key, $list);
  42. return $list;
  43. }
  44. /**
  45. * @group disconnected
  46. */
  47. public function testFilterArguments()
  48. {
  49. $modifiers = array(
  50. 'by' => 'by_key_*',
  51. 'limit' => array(1, 4),
  52. 'get' => array('object_*', '#'),
  53. 'sort' => 'asc',
  54. 'alpha' => true,
  55. 'store' => 'destination_key',
  56. );
  57. $arguments = array('key', $modifiers);
  58. $expected = array(
  59. 'key', 'BY', 'by_key_*', 'GET', 'object_*', 'GET', '#',
  60. 'LIMIT', 1, 4, 'ASC', 'ALPHA', 'STORE', 'destination_key'
  61. );
  62. $command = $this->getCommand();
  63. $command->setArguments($arguments);
  64. $this->assertEquals($expected, $command->getArguments());
  65. }
  66. /**
  67. * @group disconnected
  68. */
  69. public function testGetModifierCanBeString()
  70. {
  71. $arguments = array('key', array('get' => '#'));
  72. $expected = array('key', 'GET', '#');
  73. $command = $this->getCommand();
  74. $command->setArguments($arguments);
  75. $this->assertSame($expected, $command->getArguments());
  76. }
  77. /**
  78. * @group disconnected
  79. */
  80. public function testParseResponse()
  81. {
  82. $raw = array('value1', 'value2', 'value3');
  83. $expected = array('value1', 'value2', 'value3');
  84. $command = $this->getCommand();
  85. $this->assertSame($expected, $command->parseResponse($raw));
  86. }
  87. /**
  88. * @group disconnected
  89. */
  90. public function testPrefixKeys()
  91. {
  92. $modifiers = array(
  93. 'by' => 'by_key_*',
  94. 'limit' => array(1, 4),
  95. 'get' => array('object_*', '#'),
  96. 'sort' => 'asc',
  97. 'alpha' => true,
  98. 'store' => 'destination_key',
  99. );
  100. $arguments = array('key', $modifiers);
  101. $expected = array(
  102. 'prefix:key', 'BY', 'prefix:by_key_*', 'GET', 'prefix:object_*', 'GET', '#',
  103. 'LIMIT', 1, 4, 'ASC', 'ALPHA', 'STORE', 'prefix:destination_key'
  104. );
  105. $command = $this->getCommandWithArgumentsArray($arguments);
  106. $command->prefixKeys('prefix:');
  107. $this->assertSame($expected, $command->getArguments());
  108. }
  109. /**
  110. * @group disconnected
  111. */
  112. public function testPrefixKeysIgnoredOnEmptyArguments()
  113. {
  114. $command = $this->getCommand();
  115. $command->prefixKeys('prefix:');
  116. $this->assertSame(array(), $command->getArguments());
  117. }
  118. /**
  119. * @group connected
  120. */
  121. public function testBasicSort()
  122. {
  123. $redis = $this->getClient();
  124. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  125. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $redis->sort('list:unordered'));
  126. }
  127. /**
  128. * @group connected
  129. */
  130. public function testSortWithAscOrDescModifier()
  131. {
  132. $redis = $this->getClient();
  133. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  134. $this->assertEquals(
  135. array(100, 30, 10, 3, 2, 1),
  136. $redis->sort('list:unordered', array(
  137. 'sort' => 'desc',
  138. ))
  139. );
  140. $this->assertEquals(
  141. array(1, 2, 3, 10, 30, 100),
  142. $redis->sort('list:unordered', array(
  143. 'sort' => 'asc',
  144. ))
  145. );
  146. }
  147. /**
  148. * @group connected
  149. */
  150. public function testSortWithLimitModifier()
  151. {
  152. $redis = $this->getClient();
  153. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  154. $this->assertEquals(
  155. array(1, 2, 3),
  156. $redis->sort('list:unordered', array(
  157. 'limit' => array(0, 3),
  158. ))
  159. );
  160. $this->assertEquals(
  161. array(10, 30),
  162. $redis->sort('list:unordered', array(
  163. 'limit' => array(3, 2)
  164. ))
  165. );
  166. }
  167. /**
  168. * @group connected
  169. */
  170. public function testSortWithAlphaModifier()
  171. {
  172. $redis = $this->getClient();
  173. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  174. $this->assertEquals(
  175. array(1, 10, 100, 2, 3, 30),
  176. $redis->sort('list:unordered', array(
  177. 'alpha' => true
  178. ))
  179. );
  180. }
  181. /**
  182. * @group connected
  183. */
  184. public function testSortWithStoreModifier()
  185. {
  186. $redis = $this->getClient();
  187. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  188. $this->assertEquals(
  189. count($unordered),
  190. $redis->sort('list:unordered', array(
  191. 'store' => 'list:ordered'
  192. ))
  193. );
  194. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $redis->lrange('list:ordered', 0, -1));
  195. }
  196. /**
  197. * @group connected
  198. */
  199. public function testSortWithCombinedModifiers()
  200. {
  201. $redis = $this->getClient();
  202. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  203. $this->assertEquals(
  204. array(30, 10, 3, 2),
  205. $redis->sort('list:unordered', array(
  206. 'alpha' => false,
  207. 'sort' => 'desc',
  208. 'limit' => array(1, 4)
  209. ))
  210. );
  211. }
  212. /**
  213. * @group connected
  214. */
  215. public function testSortWithGetModifiers()
  216. {
  217. $redis = $this->getClient();
  218. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  219. $redis->rpush('list:uids', $uids = array(1003, 1001, 1002, 1000));
  220. $redis->mset($sortget = array(
  221. 'uid:1000' => 'foo', 'uid:1001' => 'bar',
  222. 'uid:1002' => 'hoge', 'uid:1003' => 'piyo',
  223. ));
  224. $this->assertEquals(array_values($sortget), $redis->sort('list:uids', array('get' => 'uid:*')));
  225. }
  226. /**
  227. * @group connected
  228. * @expectedException Predis\ServerException
  229. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  230. */
  231. public function testThrowsExceptionOnWrongType()
  232. {
  233. $redis = $this->getClient();
  234. $redis->set('foo', 'bar');
  235. $redis->sort('foo');
  236. }
  237. }