KeySortTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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\Client;
  12. /**
  13. * @group commands
  14. * @group realm-key
  15. */
  16. class KeySortTest extends PredisCommandTestCase
  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 Client $redis Redis client instance.
  36. * @param string $key Target key
  37. * @return array
  38. */
  39. protected function lpushUnorderedList(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 connected
  90. */
  91. public function testBasicSort()
  92. {
  93. $redis = $this->getClient();
  94. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  95. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $redis->sort('list:unordered'));
  96. }
  97. /**
  98. * @group connected
  99. */
  100. public function testSortWithAscOrDescModifier()
  101. {
  102. $redis = $this->getClient();
  103. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  104. $this->assertEquals(
  105. array(100, 30, 10, 3, 2, 1),
  106. $redis->sort('list:unordered', array(
  107. 'sort' => 'desc',
  108. ))
  109. );
  110. $this->assertEquals(
  111. array(1, 2, 3, 10, 30, 100),
  112. $redis->sort('list:unordered', array(
  113. 'sort' => 'asc',
  114. ))
  115. );
  116. }
  117. /**
  118. * @group connected
  119. */
  120. public function testSortWithLimitModifier()
  121. {
  122. $redis = $this->getClient();
  123. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  124. $this->assertEquals(
  125. array(1, 2, 3),
  126. $redis->sort('list:unordered', array(
  127. 'limit' => array(0, 3),
  128. ))
  129. );
  130. $this->assertEquals(
  131. array(10, 30),
  132. $redis->sort('list:unordered', array(
  133. 'limit' => array(3, 2)
  134. ))
  135. );
  136. }
  137. /**
  138. * @group connected
  139. */
  140. public function testSortWithAlphaModifier()
  141. {
  142. $redis = $this->getClient();
  143. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  144. $this->assertEquals(
  145. array(1, 10, 100, 2, 3, 30),
  146. $redis->sort('list:unordered', array(
  147. 'alpha' => true
  148. ))
  149. );
  150. }
  151. /**
  152. * @group connected
  153. */
  154. public function testSortWithStoreModifier()
  155. {
  156. $redis = $this->getClient();
  157. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  158. $this->assertEquals(
  159. count($unordered),
  160. $redis->sort('list:unordered', array(
  161. 'store' => 'list:ordered'
  162. ))
  163. );
  164. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $redis->lrange('list:ordered', 0, -1));
  165. }
  166. /**
  167. * @group connected
  168. */
  169. public function testSortWithCombinedModifiers()
  170. {
  171. $redis = $this->getClient();
  172. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  173. $this->assertEquals(
  174. array(30, 10, 3, 2),
  175. $redis->sort('list:unordered', array(
  176. 'alpha' => false,
  177. 'sort' => 'desc',
  178. 'limit' => array(1, 4)
  179. ))
  180. );
  181. }
  182. /**
  183. * @group connected
  184. */
  185. public function testSortWithGetModifiers()
  186. {
  187. $redis = $this->getClient();
  188. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  189. $redis->rpush('list:uids', $uids = array(1003, 1001, 1002, 1000));
  190. $redis->mset($sortget = array(
  191. 'uid:1000' => 'foo', 'uid:1001' => 'bar',
  192. 'uid:1002' => 'hoge', 'uid:1003' => 'piyo',
  193. ));
  194. $this->assertEquals(array_values($sortget), $redis->sort('list:uids', array('get' => 'uid:*')));
  195. }
  196. /**
  197. * @group connected
  198. * @expectedException \Predis\Response\ServerException
  199. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  200. */
  201. public function testThrowsExceptionOnWrongType()
  202. {
  203. $redis = $this->getClient();
  204. $redis->set('foo', 'bar');
  205. $redis->sort('foo');
  206. }
  207. }