|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Data\Tests\Reader; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Data\Reader\Sort; |
|
8
|
|
|
use Yiisoft\Data\Tests\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
final class SortTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
public function testInvalidConfigWithoutFieldName(): void |
|
13
|
|
|
{ |
|
14
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
15
|
|
|
new Sort([ |
|
16
|
|
|
1 => [], |
|
17
|
|
|
]); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function testInvalidConfigWithoutConfig(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
23
|
|
|
new Sort([ |
|
24
|
|
|
'field' => 'whatever', |
|
25
|
|
|
]); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testConfig(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$sort = new Sort([ |
|
31
|
|
|
'a', |
|
32
|
|
|
'b' => [ |
|
33
|
|
|
'default' => 'desc', |
|
34
|
|
|
], |
|
35
|
|
|
'name' => [ |
|
36
|
|
|
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC], |
|
37
|
|
|
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC], |
|
38
|
|
|
'default' => 'desc', |
|
39
|
|
|
'label' => 'Name', |
|
40
|
|
|
], |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
$expected = [ |
|
44
|
|
|
'a' => [ |
|
45
|
|
|
'asc' => [ |
|
46
|
|
|
'a' => SORT_ASC, |
|
47
|
|
|
], |
|
48
|
|
|
'desc' => [ |
|
49
|
|
|
'a' => SORT_DESC, |
|
50
|
|
|
], |
|
51
|
|
|
'default' => 'asc', |
|
52
|
|
|
'label' => 'a', |
|
53
|
|
|
], |
|
54
|
|
|
'b' => [ |
|
55
|
|
|
'asc' => [ |
|
56
|
|
|
'b' => SORT_ASC, |
|
57
|
|
|
], |
|
58
|
|
|
'desc' => [ |
|
59
|
|
|
'b' => SORT_DESC, |
|
60
|
|
|
], |
|
61
|
|
|
'default' => 'desc', |
|
62
|
|
|
'label' => 'b', |
|
63
|
|
|
], |
|
64
|
|
|
'name' => [ |
|
65
|
|
|
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC], |
|
66
|
|
|
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC], |
|
67
|
|
|
'default' => 'desc', |
|
68
|
|
|
'label' => 'Name', |
|
69
|
|
|
], |
|
70
|
|
|
]; |
|
71
|
|
|
$this->assertSame($expected, $this->getInaccessibleProperty($sort, 'config')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testWithOrderStringIsImmutable(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$sort = new Sort([]); |
|
77
|
|
|
$this->assertNotSame($sort, $sort->withOrderString('a')); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testWithOrderIsImmutable(): void |
|
81
|
|
|
{ |
|
82
|
|
|
$sort = new Sort([]); |
|
83
|
|
|
$this->assertNotSame($sort, $sort->withOrder([])); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testWithOrderString(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$sort = (new Sort([])) |
|
89
|
|
|
->withOrderString(' -a, b'); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertSame([ |
|
92
|
|
|
'a' => 'desc', |
|
93
|
|
|
'b' => 'asc', |
|
94
|
|
|
], $sort->getOrder()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testGetOrderAsString(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$sort = (new Sort([])) |
|
100
|
|
|
->withOrder([ |
|
101
|
|
|
'a' => 'desc', |
|
102
|
|
|
'b' => 'asc', |
|
103
|
|
|
]); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertSame('-a,b', $sort->getOrderAsString()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testGetCriteriaWithEmptyConfig(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$sort = (new Sort([])) |
|
111
|
|
|
->withOrder([ |
|
112
|
|
|
'a' => 'desc', |
|
113
|
|
|
'b' => 'asc', |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertSame([], $sort->getCriteria()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testGetCriteria(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$sort = (new Sort([ |
|
122
|
|
|
'b' => [ |
|
123
|
|
|
'asc' => ['bee' => SORT_ASC], |
|
124
|
|
|
'desc' => ['bee' => SORT_DESC], |
|
125
|
|
|
'default' => 'asc', |
|
126
|
|
|
'label' => 'B', |
|
127
|
|
|
] |
|
128
|
|
|
])) |
|
129
|
|
|
->withOrder([ |
|
130
|
|
|
'a' => 'desc', |
|
131
|
|
|
'b' => 'asc', |
|
132
|
|
|
]); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertSame([ |
|
135
|
|
|
'bee' => SORT_ASC, |
|
136
|
|
|
], $sort->getCriteria()); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|