SortArrayTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace Drupal\Tests\Component\Utility;
  3. use Drupal\Component\Utility\SortArray;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * Tests the SortArray component.
  7. *
  8. * @group Utility
  9. *
  10. * @coversDefaultClass \Drupal\Component\Utility\SortArray
  11. */
  12. class SortArrayTest extends TestCase {
  13. /**
  14. * Tests SortArray::sortByWeightElement() input against expected output.
  15. *
  16. * @dataProvider providerSortByWeightElement
  17. * @covers ::sortByWeightElement
  18. * @covers ::sortByKeyInt
  19. *
  20. * @param array $a
  21. * The first input array for the SortArray::sortByWeightElement() method.
  22. * @param array $b
  23. * The second input array for the SortArray::sortByWeightElement().
  24. * @param int $expected
  25. * The expected output from calling the method.
  26. */
  27. public function testSortByWeightElement($a, $b, $expected) {
  28. $result = SortArray::sortByWeightElement($a, $b);
  29. $this->assertBothNegativePositiveOrZero($expected, $result);
  30. }
  31. /**
  32. * Data provider for SortArray::sortByWeightElement().
  33. *
  34. * @return array
  35. * An array of tests, matching the parameter inputs for
  36. * testSortByWeightElement.
  37. *
  38. * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByWeightElement()
  39. */
  40. public function providerSortByWeightElement() {
  41. $tests = [];
  42. // Weights set and equal.
  43. $tests[] = [
  44. ['weight' => 1],
  45. ['weight' => 1],
  46. 0,
  47. ];
  48. // Weights set and $a is less (lighter) than $b.
  49. $tests[] = [
  50. ['weight' => 1],
  51. ['weight' => 2],
  52. -1,
  53. ];
  54. // Weights set and $a is greater (heavier) than $b.
  55. $tests[] = [
  56. ['weight' => 2],
  57. ['weight' => 1],
  58. 1,
  59. ];
  60. // Weights not set.
  61. $tests[] = [
  62. [],
  63. [],
  64. 0,
  65. ];
  66. // Weights for $b not set.
  67. $tests[] = [
  68. ['weight' => 1],
  69. [],
  70. 1,
  71. ];
  72. // Weights for $a not set.
  73. $tests[] = [
  74. [],
  75. ['weight' => 1],
  76. -1,
  77. ];
  78. return $tests;
  79. }
  80. /**
  81. * Tests SortArray::sortByWeightProperty() input against expected output.
  82. *
  83. * @dataProvider providerSortByWeightProperty
  84. * @covers ::sortByWeightProperty
  85. * @covers ::sortByKeyInt
  86. *
  87. * @param array $a
  88. * The first input array for the SortArray::sortByWeightProperty() method.
  89. * @param array $b
  90. * The second input array for the SortArray::sortByWeightProperty().
  91. * @param int $expected
  92. * The expected output from calling the method.
  93. */
  94. public function testSortByWeightProperty($a, $b, $expected) {
  95. $result = SortArray::sortByWeightProperty($a, $b);
  96. $this->assertBothNegativePositiveOrZero($expected, $result);
  97. }
  98. /**
  99. * Data provider for SortArray::sortByWeightProperty().
  100. *
  101. * @return array
  102. * An array of tests, matching the parameter inputs for
  103. * testSortByWeightProperty.
  104. *
  105. * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByWeightProperty()
  106. */
  107. public function providerSortByWeightProperty() {
  108. $tests = [];
  109. // Weights set and equal.
  110. $tests[] = [
  111. ['#weight' => 1],
  112. ['#weight' => 1],
  113. 0,
  114. ];
  115. // Weights set and $a is less (lighter) than $b.
  116. $tests[] = [
  117. ['#weight' => 1],
  118. ['#weight' => 2],
  119. -1,
  120. ];
  121. // Weights set and $a is greater (heavier) than $b.
  122. $tests[] = [
  123. ['#weight' => 2],
  124. ['#weight' => 1],
  125. 1,
  126. ];
  127. // Weights not set.
  128. $tests[] = [
  129. [],
  130. [],
  131. 0,
  132. ];
  133. // Weights for $b not set.
  134. $tests[] = [
  135. ['#weight' => 1],
  136. [],
  137. 1,
  138. ];
  139. // Weights for $a not set.
  140. $tests[] = [
  141. [],
  142. ['#weight' => 1],
  143. -1,
  144. ];
  145. return $tests;
  146. }
  147. /**
  148. * Tests SortArray::sortByTitleElement() input against expected output.
  149. *
  150. * @dataProvider providerSortByTitleElement
  151. * @covers ::sortByTitleElement
  152. * @covers ::sortByKeyString
  153. *
  154. * @param array $a
  155. * The first input item for comparison.
  156. * @param array $b
  157. * The second item for comparison.
  158. * @param int $expected
  159. * The expected output from calling the method.
  160. */
  161. public function testSortByTitleElement($a, $b, $expected) {
  162. $result = SortArray::sortByTitleElement($a, $b);
  163. $this->assertBothNegativePositiveOrZero($expected, $result);
  164. }
  165. /**
  166. * Data provider for SortArray::sortByTitleElement().
  167. *
  168. * @return array
  169. * An array of tests, matching the parameter inputs for
  170. * testSortByTitleElement.
  171. *
  172. * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleElement()
  173. */
  174. public function providerSortByTitleElement() {
  175. $tests = [];
  176. // Titles set and equal.
  177. $tests[] = [
  178. ['title' => 'test'],
  179. ['title' => 'test'],
  180. 0,
  181. ];
  182. // Title $a not set.
  183. $tests[] = [
  184. [],
  185. ['title' => 'test'],
  186. -4,
  187. ];
  188. // Title $b not set.
  189. $tests[] = [
  190. ['title' => 'test'],
  191. [],
  192. 4,
  193. ];
  194. // Titles set but not equal.
  195. $tests[] = [
  196. ['title' => 'test'],
  197. ['title' => 'testing'],
  198. -1,
  199. ];
  200. // Titles set but not equal.
  201. $tests[] = [
  202. ['title' => 'testing'],
  203. ['title' => 'test'],
  204. 1,
  205. ];
  206. return $tests;
  207. }
  208. /**
  209. * Tests SortArray::sortByTitleProperty() input against expected output.
  210. *
  211. * @dataProvider providerSortByTitleProperty
  212. * @covers ::sortByTitleProperty
  213. * @covers ::sortByKeyString
  214. *
  215. * @param array $a
  216. * The first input item for comparison.
  217. * @param array $b
  218. * The second item for comparison.
  219. * @param int $expected
  220. * The expected output from calling the method.
  221. */
  222. public function testSortByTitleProperty($a, $b, $expected) {
  223. $result = SortArray::sortByTitleProperty($a, $b);
  224. $this->assertBothNegativePositiveOrZero($expected, $result);
  225. }
  226. /**
  227. * Data provider for SortArray::sortByTitleProperty().
  228. *
  229. * @return array
  230. * An array of tests, matching the parameter inputs for
  231. * testSortByTitleProperty.
  232. *
  233. * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleProperty()
  234. */
  235. public function providerSortByTitleProperty() {
  236. $tests = [];
  237. // Titles set and equal.
  238. $tests[] = [
  239. ['#title' => 'test'],
  240. ['#title' => 'test'],
  241. 0,
  242. ];
  243. // Title $a not set.
  244. $tests[] = [
  245. [],
  246. ['#title' => 'test'],
  247. -4,
  248. ];
  249. // Title $b not set.
  250. $tests[] = [
  251. ['#title' => 'test'],
  252. [],
  253. 4,
  254. ];
  255. // Titles set but not equal.
  256. $tests[] = [
  257. ['#title' => 'test'],
  258. ['#title' => 'testing'],
  259. -1,
  260. ];
  261. // Titles set but not equal.
  262. $tests[] = [
  263. ['#title' => 'testing'],
  264. ['#title' => 'test'],
  265. 1,
  266. ];
  267. return $tests;
  268. }
  269. /**
  270. * Asserts that numbers are either both negative, both positive or both zero.
  271. *
  272. * The exact values returned by comparison functions differ between PHP
  273. * versions and are considered an "implementation detail".
  274. *
  275. * @param int $expected
  276. * Expected comparison function return value.
  277. * @param int $result
  278. * Actual comparison function return value.
  279. */
  280. protected function assertBothNegativePositiveOrZero($expected, $result) {
  281. $this->assertTrue(is_numeric($expected) && is_numeric($result), 'Parameters are numeric.');
  282. $this->assertTrue(($expected < 0 && $result < 0) || ($expected > 0 && $result > 0) || ($expected === 0 && $result === 0), 'Numbers are either both negative, both positive or both zero.');
  283. }
  284. }