SortArrayTest.php 7.4 KB

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