SORT_Test.php 6.0 KB

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