-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.php
More file actions
50 lines (44 loc) · 1.16 KB
/
index.php
File metadata and controls
50 lines (44 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/**
* Function to be tested
*
* Includes a number of features that require PHP 7.0+, including:
* - Scalar type hints
* - Return types
* - The "spaceship" operator
*
*/
function sortArrayOfObjects(string $field, array $objects): array
{
usort($objects, function ($a, $b) use ($field) {
return $a->{$field} <=> $b->{$field};
});
return $objects;
}
/**
* PHPUnit test code
*
* Normally this would be in another file, but we're keeping it simple.
* This test simply verifies that the sorting works, but will throw
* a syntax error in PHP < 7.0.
*/
use PHPUnit\Framework\TestCase;
class SortArrayOfObjectsTest extends TestCase
{
function testItSortsWhenFieldExists()
{
$objects = [
(object) [
'sort_order' => '2',
'name' => 'The first shall be last',
],
(object) [
'sort_order' => '1',
'name' => 'The last shall be first',
],
];
$result = sortArrayOfObjects('sort_order', $objects);
$this->assertEquals($objects[0], $result[1]);
$this->assertEquals($objects[1], $result[0]);
}
}