KeySortTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\Commands;
  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\Commands\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 connected
  112. */
  113. public function testBasicSort()
  114. {
  115. $redis = $this->getClient();
  116. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  117. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $redis->sort('list:unordered'));
  118. }
  119. /**
  120. * @group connected
  121. */
  122. public function testSortWithAscOrDescModifier()
  123. {
  124. $redis = $this->getClient();
  125. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  126. $this->assertEquals(
  127. array(100, 30, 10, 3, 2, 1),
  128. $redis->sort('list:unordered', array(
  129. 'sort' => 'desc',
  130. ))
  131. );
  132. $this->assertEquals(
  133. array(1, 2, 3, 10, 30, 100),
  134. $redis->sort('list:unordered', array(
  135. 'sort' => 'asc',
  136. ))
  137. );
  138. }
  139. /**
  140. * @group connected
  141. */
  142. public function testSortWithLimitModifier()
  143. {
  144. $redis = $this->getClient();
  145. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  146. $this->assertEquals(
  147. array(1, 2, 3),
  148. $redis->sort('list:unordered', array(
  149. 'limit' => array(0, 3),
  150. ))
  151. );
  152. $this->assertEquals(
  153. array(10, 30),
  154. $redis->sort('list:unordered', array(
  155. 'limit' => array(3, 2)
  156. ))
  157. );
  158. }
  159. /**
  160. * @group connected
  161. */
  162. public function testSortWithAlphaModifier()
  163. {
  164. $redis = $this->getClient();
  165. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  166. $this->assertEquals(
  167. array(1, 10, 100, 2, 3, 30),
  168. $redis->sort('list:unordered', array(
  169. 'alpha' => true
  170. ))
  171. );
  172. }
  173. /**
  174. * @group connected
  175. */
  176. public function testSortWithStoreModifier()
  177. {
  178. $redis = $this->getClient();
  179. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  180. $this->assertEquals(
  181. count($unordered),
  182. $redis->sort('list:unordered', array(
  183. 'store' => 'list:ordered'
  184. ))
  185. );
  186. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $redis->lrange('list:ordered', 0, -1));
  187. }
  188. /**
  189. * @group connected
  190. */
  191. public function testSortWithCombinedModifiers()
  192. {
  193. $redis = $this->getClient();
  194. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  195. $this->assertEquals(
  196. array(30, 10, 3, 2),
  197. $redis->sort('list:unordered', array(
  198. 'alpha' => false,
  199. 'sort' => 'desc',
  200. 'limit' => array(1, 4)
  201. ))
  202. );
  203. }
  204. /**
  205. * @group connected
  206. */
  207. public function testSortWithGetModifiers()
  208. {
  209. $redis = $this->getClient();
  210. $redis->lpush('list:unordered', $unordered = array(2, 100, 3, 1, 30, 10));
  211. $redis->rpush('list:uids', $uids = array(1003, 1001, 1002, 1000));
  212. $redis->mset($sortget = array(
  213. 'uid:1000' => 'foo', 'uid:1001' => 'bar',
  214. 'uid:1002' => 'hoge', 'uid:1003' => 'piyo',
  215. ));
  216. $this->assertEquals(array_values($sortget), $redis->sort('list:uids', array('get' => 'uid:*')));
  217. }
  218. /**
  219. * @group connected
  220. * @expectedException Predis\ServerException
  221. * @expectedExceptionMessage ERR Operation against a key holding the wrong kind of value
  222. */
  223. public function testThrowsExceptionOnWrongType()
  224. {
  225. $redis = $this->getClient();
  226. $redis->set('foo', 'bar');
  227. $redis->sort('foo');
  228. }
  229. }