KeySortTest.php 6.0 KB

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