3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\SortArray;
6 use PHPUnit\Framework\TestCase;
9 * Tests the SortArray component.
13 * @coversDefaultClass \Drupal\Component\Utility\SortArray
15 class SortArrayTest extends TestCase {
18 * Tests SortArray::sortByWeightElement() input against expected output.
20 * @dataProvider providerSortByWeightElement
21 * @covers ::sortByWeightElement
22 * @covers ::sortByKeyInt
25 * The first input array for the SortArray::sortByWeightElement() method.
27 * The second input array for the SortArray::sortByWeightElement().
28 * @param int $expected
29 * The expected output from calling the method.
31 public function testSortByWeightElement($a, $b, $expected) {
32 $result = SortArray::sortByWeightElement($a, $b);
33 $this->assertBothNegativePositiveOrZero($expected, $result);
37 * Data provider for SortArray::sortByWeightElement().
40 * An array of tests, matching the parameter inputs for
41 * testSortByWeightElement.
43 * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByWeightElement()
45 public function providerSortByWeightElement() {
48 // Weights set and equal.
55 // Weights set and $a is less (lighter) than $b.
62 // Weights set and $a is greater (heavier) than $b.
76 // Weights for $b not set.
83 // Weights for $a not set.
94 * Tests SortArray::sortByWeightProperty() input against expected output.
96 * @dataProvider providerSortByWeightProperty
97 * @covers ::sortByWeightProperty
98 * @covers ::sortByKeyInt
101 * The first input array for the SortArray::sortByWeightProperty() method.
103 * The second input array for the SortArray::sortByWeightProperty().
104 * @param int $expected
105 * The expected output from calling the method.
107 public function testSortByWeightProperty($a, $b, $expected) {
108 $result = SortArray::sortByWeightProperty($a, $b);
109 $this->assertBothNegativePositiveOrZero($expected, $result);
113 * Data provider for SortArray::sortByWeightProperty().
116 * An array of tests, matching the parameter inputs for
117 * testSortByWeightProperty.
119 * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByWeightProperty()
121 public function providerSortByWeightProperty() {
124 // Weights set and equal.
131 // Weights set and $a is less (lighter) than $b.
138 // Weights set and $a is greater (heavier) than $b.
152 // Weights for $b not set.
159 // Weights for $a not set.
170 * Tests SortArray::sortByTitleElement() input against expected output.
172 * @dataProvider providerSortByTitleElement
173 * @covers ::sortByTitleElement
174 * @covers ::sortByKeyString
177 * The first input item for comparison.
179 * The second item for comparison.
180 * @param int $expected
181 * The expected output from calling the method.
183 public function testSortByTitleElement($a, $b, $expected) {
184 $result = SortArray::sortByTitleElement($a, $b);
185 $this->assertBothNegativePositiveOrZero($expected, $result);
189 * Data provider for SortArray::sortByTitleElement().
192 * An array of tests, matching the parameter inputs for
193 * testSortByTitleElement.
195 * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleElement()
197 public function providerSortByTitleElement() {
200 // Titles set and equal.
221 // Titles set but not equal.
224 ['title' => 'testing'],
228 // Titles set but not equal.
230 ['title' => 'testing'],
239 * Tests SortArray::sortByTitleProperty() input against expected output.
241 * @dataProvider providerSortByTitleProperty
242 * @covers ::sortByTitleProperty
243 * @covers ::sortByKeyString
246 * The first input item for comparison.
248 * The second item for comparison.
249 * @param int $expected
250 * The expected output from calling the method.
252 public function testSortByTitleProperty($a, $b, $expected) {
253 $result = SortArray::sortByTitleProperty($a, $b);
254 $this->assertBothNegativePositiveOrZero($expected, $result);
258 * Data provider for SortArray::sortByTitleProperty().
261 * An array of tests, matching the parameter inputs for
262 * testSortByTitleProperty.
264 * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleProperty()
266 public function providerSortByTitleProperty() {
269 // Titles set and equal.
271 ['#title' => 'test'],
272 ['#title' => 'test'],
279 ['#title' => 'test'],
285 ['#title' => 'test'],
290 // Titles set but not equal.
292 ['#title' => 'test'],
293 ['#title' => 'testing'],
297 // Titles set but not equal.
299 ['#title' => 'testing'],
300 ['#title' => 'test'],
308 * Asserts that numbers are either both negative, both positive or both zero.
310 * The exact values returned by comparison functions differ between PHP
311 * versions and are considered an "implementation detail".
313 * @param int $expected
314 * Expected comparison function return value.
316 * Actual comparison function return value.
318 protected function assertBothNegativePositiveOrZero($expected, $result) {
319 $this->assertTrue(is_numeric($expected) && is_numeric($result), 'Parameters are numeric.');
320 $this->assertTrue(($expected < 0 && $result < 0) || ($expected > 0 && $result > 0) || ($expected === 0 && $result === 0), 'Numbers are either both negative, both positive or both zero.');