Repository navigation
Expand file tree
/
Copy pathtoBeSorted.php
More file actions
108 lines (90 loc) · 3.39 KB
/
Copy pathtoBeSorted.php
File metadata and controls
108 lines (90 loc) · 3.39 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
use Pest\Exceptions\InvalidExpectationValue;
use PHPUnit\Framework\ExpectationFailedException;
test('pass with no $by delegates to ascending', function () {
expect([1, 2, 3])->toBeSorted();
});
test('pass with no $by delegates to descending', function () {
expect([3, 2, 1])->toBeSorted(direction: 'desc');
});
test('pass with $by on associative arrays ascending', function () {
$users = [
['name' => 'Anna', 'age' => 20],
['name' => 'Ben', 'age' => 25],
['name' => 'Cara', 'age' => 30],
];
expect($users)->toBeSorted(by: 'age');
});
test('pass with $by on associative arrays descending', function () {
$users = [
['name' => 'Cara', 'age' => 30],
['name' => 'Ben', 'age' => 25],
['name' => 'Anna', 'age' => 20],
];
expect($users)->toBeSorted(by: 'age', direction: 'desc');
});
test('pass with $by on object properties', function () {
$users = [
(object) ['name' => 'Anna'],
(object) ['name' => 'Ben'],
(object) ['name' => 'Cara'],
];
expect($users)->toBeSorted(by: 'name');
});
test('pass with $by on DateTime values', function () {
$posts = [
['created_at' => new DateTime('2024-01-01')],
['created_at' => new DateTime('2024-06-01')],
['created_at' => new DateTime('2024-12-01')],
];
expect($posts)->toBeSorted(by: 'created_at');
});
test('pass with single element', function () {
expect([['age' => 25]])->toBeSorted(by: 'age');
});
test('pass with empty array', function () {
expect([])->toBeSorted(by: 'age');
});
test('failures with $by', function () {
$users = [
['age' => 30],
['age' => 20],
];
expect($users)->toBeSorted(by: 'age');
})->throws(ExpectationFailedException::class, 'Array is not sorted by [age] in ascending order.');
test('failures with $by descending', function () {
$users = [
['age' => 20],
['age' => 30],
];
expect($users)->toBeSorted(by: 'age', direction: 'desc');
})->throws(ExpectationFailedException::class, 'Array is not sorted by [age] in descending order.');
test('failures with custom message', function () {
expect([['age' => 30], ['age' => 20]])->toBeSorted(by: 'age', message: 'oh no!');
})->throws(ExpectationFailedException::class, 'oh no!');
test('failures with invalid direction', function () {
expect([1, 2, 3])->toBeSorted(direction: 'sideways');
})->throws(InvalidArgumentException::class, 'Direction must be "asc" or "desc", got "sideways".');
test('failures with invalid type', function () {
expect('not an array')->toBeSorted();
})->throws(InvalidExpectationValue::class, 'Invalid expectation value type. Expected [array]');
test('failures with missing array key', function () {
expect([['name' => 'Anna']])->toBeSorted(by: 'age');
})->throws(InvalidArgumentException::class, 'Array key [age] does not exist.');
test('failures with missing object property', function () {
expect([(object) ['name' => 'Anna']])->toBeSorted(by: 'age');
})->throws(InvalidArgumentException::class, 'Property [age] does not exist.');
test('not pass', function () {
$users = [
['age' => 30],
['age' => 20],
];
expect($users)->not->toBeSorted(by: 'age');
});
test('not failures', function () {
$users = [
['age' => 20],
['age' => 30],
];
expect($users)->not->toBeSorted(by: 'age');
})->throws(ExpectationFailedException::class);