SortIteratorTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Collection\Iterator;
  17. use ArrayObject;
  18. use Cake\Chronos\Chronos;
  19. use Cake\Chronos\ChronosDate;
  20. use Cake\Collection\Iterator\SortIterator;
  21. use Cake\TestSuite\TestCase;
  22. use DateInterval;
  23. use DateTime;
  24. use DateTimeImmutable;
  25. use const SORT_ASC;
  26. use const SORT_DESC;
  27. use const SORT_NUMERIC;
  28. /**
  29. * SortIterator Test
  30. */
  31. class SortIteratorTest extends TestCase
  32. {
  33. /**
  34. * Tests sorting numbers with an identity callbacks
  35. */
  36. public function testSortNumbersIdentity(): void
  37. {
  38. $items = new ArrayObject([3, 5, 1, 2, 4]);
  39. $identity = function ($a) {
  40. return $a;
  41. };
  42. $sorted = new SortIterator($items, $identity);
  43. $expected = range(5, 1);
  44. $this->assertEquals($expected, $sorted->toList());
  45. $sorted = new SortIterator($items, $identity, SORT_ASC);
  46. $expected = range(1, 5);
  47. $this->assertEquals($expected, $sorted->toList());
  48. }
  49. /**
  50. * Tests sorting numbers with custom callback
  51. */
  52. public function testSortNumbersCustom(): void
  53. {
  54. $items = new ArrayObject([3, 5, 1, 2, 4]);
  55. $callback = function ($a) {
  56. return $a * -1;
  57. };
  58. $sorted = new SortIterator($items, $callback);
  59. $expected = range(1, 5);
  60. $this->assertEquals($expected, $sorted->toList());
  61. $sorted = new SortIterator($items, $callback, SORT_ASC);
  62. $expected = range(5, 1);
  63. $this->assertEquals($expected, $sorted->toList());
  64. }
  65. /**
  66. * Tests sorting a complex structure with numeric sort
  67. */
  68. public function testSortComplexNumeric(): void
  69. {
  70. $items = new ArrayObject([
  71. ['foo' => 1, 'bar' => 'a'],
  72. ['foo' => 10, 'bar' => 'a'],
  73. ['foo' => 2, 'bar' => 'a'],
  74. ['foo' => 13, 'bar' => 'a'],
  75. ]);
  76. $callback = function ($a) {
  77. return $a['foo'];
  78. };
  79. $sorted = new SortIterator($items, $callback, SORT_DESC, SORT_NUMERIC);
  80. $expected = [
  81. ['foo' => 13, 'bar' => 'a'],
  82. ['foo' => 10, 'bar' => 'a'],
  83. ['foo' => 2, 'bar' => 'a'],
  84. ['foo' => 1, 'bar' => 'a'],
  85. ];
  86. $this->assertEquals($expected, $sorted->toList());
  87. $sorted = new SortIterator($items, $callback, SORT_ASC, SORT_NUMERIC);
  88. $expected = [
  89. ['foo' => 1, 'bar' => 'a'],
  90. ['foo' => 2, 'bar' => 'a'],
  91. ['foo' => 10, 'bar' => 'a'],
  92. ['foo' => 13, 'bar' => 'a'],
  93. ];
  94. $this->assertEquals($expected, $sorted->toList());
  95. }
  96. /**
  97. * Tests sorting a complex structure with natural sort
  98. */
  99. public function testSortComplexNatural(): void
  100. {
  101. $items = new ArrayObject([
  102. ['foo' => 'foo_1', 'bar' => 'a'],
  103. ['foo' => 'foo_10', 'bar' => 'a'],
  104. ['foo' => 'foo_2', 'bar' => 'a'],
  105. ['foo' => 'foo_13', 'bar' => 'a'],
  106. ]);
  107. $callback = function ($a) {
  108. return $a['foo'];
  109. };
  110. $sorted = new SortIterator($items, $callback, SORT_DESC, SORT_NATURAL);
  111. $expected = [
  112. ['foo' => 'foo_13', 'bar' => 'a'],
  113. ['foo' => 'foo_10', 'bar' => 'a'],
  114. ['foo' => 'foo_2', 'bar' => 'a'],
  115. ['foo' => 'foo_1', 'bar' => 'a'],
  116. ];
  117. $this->assertEquals($expected, $sorted->toList());
  118. $sorted = new SortIterator($items, $callback, SORT_ASC, SORT_NATURAL);
  119. $expected = [
  120. ['foo' => 'foo_1', 'bar' => 'a'],
  121. ['foo' => 'foo_2', 'bar' => 'a'],
  122. ['foo' => 'foo_10', 'bar' => 'a'],
  123. ['foo' => 'foo_13', 'bar' => 'a'],
  124. ];
  125. $this->assertEquals($expected, $sorted->toList());
  126. $this->assertEquals($expected, $sorted->toList(), 'Iterator should rewind');
  127. }
  128. /**
  129. * Tests sorting a complex structure with natural sort with string callback
  130. */
  131. public function testSortComplexNaturalWithPath(): void
  132. {
  133. $items = new ArrayObject([
  134. ['foo' => 'foo_1', 'bar' => 'a'],
  135. ['foo' => 'foo_10', 'bar' => 'a'],
  136. ['foo' => 'foo_2', 'bar' => 'a'],
  137. ['foo' => 'foo_13', 'bar' => 'a'],
  138. ]);
  139. $sorted = new SortIterator($items, 'foo', SORT_DESC, SORT_NATURAL);
  140. $expected = [
  141. ['foo' => 'foo_13', 'bar' => 'a'],
  142. ['foo' => 'foo_10', 'bar' => 'a'],
  143. ['foo' => 'foo_2', 'bar' => 'a'],
  144. ['foo' => 'foo_1', 'bar' => 'a'],
  145. ];
  146. $this->assertEquals($expected, $sorted->toList());
  147. $sorted = new SortIterator($items, 'foo', SORT_ASC, SORT_NATURAL);
  148. $expected = [
  149. ['foo' => 'foo_1', 'bar' => 'a'],
  150. ['foo' => 'foo_2', 'bar' => 'a'],
  151. ['foo' => 'foo_10', 'bar' => 'a'],
  152. ['foo' => 'foo_13', 'bar' => 'a'],
  153. ];
  154. $this->assertEquals($expected, $sorted->toList());
  155. $this->assertEquals($expected, $sorted->toList(), 'Iterator should rewind');
  156. }
  157. /**
  158. * Tests sorting a complex structure with a deep path
  159. */
  160. public function testSortComplexDeepPath(): void
  161. {
  162. $items = new ArrayObject([
  163. ['foo' => ['bar' => 1], 'bar' => 'a'],
  164. ['foo' => ['bar' => 12], 'bar' => 'a'],
  165. ['foo' => ['bar' => 10], 'bar' => 'a'],
  166. ['foo' => ['bar' => 2], 'bar' => 'a'],
  167. ]);
  168. $sorted = new SortIterator($items, 'foo.bar', SORT_ASC, SORT_NUMERIC);
  169. $expected = [
  170. ['foo' => ['bar' => 1], 'bar' => 'a'],
  171. ['foo' => ['bar' => 2], 'bar' => 'a'],
  172. ['foo' => ['bar' => 10], 'bar' => 'a'],
  173. ['foo' => ['bar' => 12], 'bar' => 'a'],
  174. ];
  175. $this->assertEquals($expected, $sorted->toList());
  176. }
  177. /**
  178. * Tests sorting datetime
  179. */
  180. public function testSortDateTime(): void
  181. {
  182. $items = new ArrayObject([
  183. new DateTime('2014-07-21'),
  184. new DateTime('2015-06-30'),
  185. new DateTimeImmutable('2013-08-12'),
  186. ]);
  187. $callback = function ($a) {
  188. return $a->add(new DateInterval('P1Y'));
  189. };
  190. $sorted = new SortIterator($items, $callback);
  191. $expected = [
  192. new DateTime('2016-06-30'),
  193. new DateTime('2015-07-21'),
  194. new DateTimeImmutable('2013-08-12'),
  195. ];
  196. $this->assertEquals($expected, $sorted->toList());
  197. $items = new ArrayObject([
  198. new DateTime('2014-07-21'),
  199. new DateTime('2015-06-30'),
  200. new DateTimeImmutable('2013-08-12'),
  201. ]);
  202. $sorted = new SortIterator($items, $callback, SORT_ASC);
  203. $expected = [
  204. new DateTimeImmutable('2013-08-12'),
  205. new DateTime('2015-07-21'),
  206. new DateTime('2016-06-30'),
  207. ];
  208. $this->assertEquals($expected, $sorted->toList());
  209. }
  210. /**
  211. * Tests sorting with Chronos datetime
  212. */
  213. public function testSortWithChronosDateTime(): void
  214. {
  215. $items = new ArrayObject([
  216. new Chronos('2014-07-21'),
  217. new ChronosDate('2015-06-30'),
  218. new DateTimeImmutable('2013-08-12'),
  219. ]);
  220. $callback = fn ($d) => $d;
  221. $sorted = new SortIterator($items, $callback);
  222. $expected = [
  223. new ChronosDate('2015-06-30'),
  224. new Chronos('2014-07-21'),
  225. new DateTimeImmutable('2013-08-12'),
  226. ];
  227. $this->assertEquals($expected, $sorted->toList());
  228. $items = new ArrayObject([
  229. new Chronos('2014-07-21'),
  230. new ChronosDate('2015-06-30'),
  231. new DateTimeImmutable('2013-08-12'),
  232. ]);
  233. $sorted = new SortIterator($items, $callback, SORT_ASC);
  234. $expected = [
  235. new DateTimeImmutable('2013-08-12'),
  236. new Chronos('2014-07-21'),
  237. new ChronosDate('2015-06-30'),
  238. ];
  239. $this->assertEquals($expected, $sorted->toList());
  240. }
  241. }