KeySortTest.php 7.1 KB

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