1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace spec\Sylius\Bundle\UiBundle\Twig; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\Collection; |
15
|
|
|
use PhpSpec\ObjectBehavior; |
16
|
|
|
use Sylius\Bundle\UiBundle\Twig\SortByExtension; |
17
|
|
|
use Sylius\Bundle\UiBundle\spec\Fixtures\SampleInterface; |
18
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Jan Góralski <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class SortByExtensionSpec extends ObjectBehavior |
24
|
|
|
{ |
25
|
|
|
function it_is_initializable() |
26
|
|
|
{ |
27
|
|
|
$this->shouldHaveType(SortByExtension::class); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
function it_extends_twig_extensions() |
31
|
|
|
{ |
32
|
|
|
$this->shouldHaveType(\Twig_Extension::class); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
function it_has_name() |
36
|
|
|
{ |
37
|
|
|
$this->getName()->shouldReturn('sort_by'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function it_sorts_in_ascending_order_by_default( |
41
|
|
|
SampleInterface $firstSample, |
42
|
|
|
SampleInterface $secondSample, |
43
|
|
|
SampleInterface $thirdSample |
44
|
|
|
) { |
45
|
|
|
$firstSample->getInt()->willReturn(3); |
46
|
|
|
$secondSample->getInt()->willReturn(5); |
47
|
|
|
$thirdSample->getInt()->willReturn(1); |
48
|
|
|
|
49
|
|
|
$arrayBeforeSorting = [ |
50
|
|
|
$firstSample, |
51
|
|
|
$secondSample, |
52
|
|
|
$thirdSample, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$this->sortBy($arrayBeforeSorting, 'int')->shouldReturn( |
56
|
|
|
[ |
57
|
|
|
$thirdSample, |
58
|
|
|
$firstSample, |
59
|
|
|
$secondSample, |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
function it_sorts_an_array_of_objects_by_various_properties( |
65
|
|
|
SampleInterface $firstSample, |
66
|
|
|
SampleInterface $secondSample, |
67
|
|
|
SampleInterface $thirdSample |
68
|
|
|
) { |
69
|
|
|
$firstSample->getInt()->willReturn(3); |
70
|
|
|
$secondSample->getInt()->willReturn(5); |
71
|
|
|
$thirdSample->getInt()->willReturn(1); |
72
|
|
|
|
73
|
|
|
$firstSample->getString()->willReturn('true'); |
74
|
|
|
$secondSample->getString()->willReturn('123'); |
75
|
|
|
$thirdSample->getString()->willReturn('Alohomora'); |
76
|
|
|
|
77
|
|
|
$firstSample->getBizarrelyNamedProperty()->willReturn('banana'); |
78
|
|
|
$secondSample->getBizarrelyNamedProperty()->willReturn(123); |
79
|
|
|
$thirdSample->getBizarrelyNamedProperty()->willReturn(null); |
80
|
|
|
|
81
|
|
|
$arrayBeforeSorting = [ |
82
|
|
|
$firstSample, |
83
|
|
|
$secondSample, |
84
|
|
|
$thirdSample, |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
$this->sortBy($arrayBeforeSorting, 'int')->shouldReturn( |
88
|
|
|
[ |
89
|
|
|
$thirdSample, |
90
|
|
|
$firstSample, |
91
|
|
|
$secondSample, |
92
|
|
|
] |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$this->sortBy($arrayBeforeSorting, 'string')->shouldReturn( |
96
|
|
|
[ |
97
|
|
|
$secondSample, |
98
|
|
|
$thirdSample, |
99
|
|
|
$firstSample, |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$this->sortBy($arrayBeforeSorting, 'bizarrelyNamedProperty')->shouldReturn( |
104
|
|
|
[ |
105
|
|
|
$thirdSample, |
106
|
|
|
$secondSample, |
107
|
|
|
$firstSample, |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
function it_sorts_an_array_of_objects_in_descending_order_by_a_property( |
113
|
|
|
SampleInterface $firstSample, |
114
|
|
|
SampleInterface $secondSample, |
115
|
|
|
SampleInterface $thirdSample |
116
|
|
|
) { |
117
|
|
|
$firstSample->getInt()->willReturn(3); |
118
|
|
|
$secondSample->getInt()->willReturn(5); |
119
|
|
|
$thirdSample->getInt()->willReturn(1); |
120
|
|
|
|
121
|
|
|
$arrayBeforeSorting = [ |
122
|
|
|
$firstSample, |
123
|
|
|
$secondSample, |
124
|
|
|
$thirdSample, |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
$this->sortBy($arrayBeforeSorting, 'int', 'DESC')->shouldReturn( |
128
|
|
|
[ |
129
|
|
|
$secondSample, |
130
|
|
|
$firstSample, |
131
|
|
|
$thirdSample, |
132
|
|
|
] |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
function it_sorts_an_array_of_objects_by_a_nested_property( |
137
|
|
|
SampleInterface $firstSample, |
138
|
|
|
SampleInterface $secondSample, |
139
|
|
|
SampleInterface $thirdSample, |
140
|
|
|
SampleInterface $firstInnerSample, |
141
|
|
|
SampleInterface $secondInnerSample, |
142
|
|
|
SampleInterface $thirdInnerSample |
143
|
|
|
) { |
144
|
|
|
$firstInnerSample->getString()->willReturn('m'); |
145
|
|
|
$secondInnerSample->getString()->willReturn('Z'); |
146
|
|
|
$thirdInnerSample->getString()->willReturn('A'); |
147
|
|
|
|
148
|
|
|
$firstSample->getInnerSample()->willReturn($firstInnerSample); |
149
|
|
|
$secondSample->getInnerSample()->willReturn($secondInnerSample); |
150
|
|
|
$thirdSample->getInnerSample()->willReturn($thirdInnerSample); |
151
|
|
|
|
152
|
|
|
$arrayBeforeSorting = [ |
153
|
|
|
$firstSample, |
154
|
|
|
$secondSample, |
155
|
|
|
$thirdSample, |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$this->sortBy($arrayBeforeSorting, 'innerSample.string', 'ASC')->shouldReturn( |
159
|
|
|
[ |
160
|
|
|
$thirdSample, |
161
|
|
|
$firstSample, |
162
|
|
|
$secondSample, |
163
|
|
|
] |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
function it_throws_an_exception_if_the_property_is_not_found_on_objects( |
168
|
|
|
SampleInterface $firstSample, |
169
|
|
|
SampleInterface $secondSample, |
170
|
|
|
SampleInterface $thirdSample |
171
|
|
|
) { |
172
|
|
|
$arrayBeforeSorting = [ |
173
|
|
|
$firstSample, |
174
|
|
|
$secondSample, |
175
|
|
|
$thirdSample, |
176
|
|
|
]; |
177
|
|
|
|
178
|
|
|
$this |
179
|
|
|
->shouldThrow(NoSuchPropertyException::class) |
180
|
|
|
->during('sortBy', [$arrayBeforeSorting, 'nonExistingProperty']) |
181
|
|
|
; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
function it_return_input_array_if_there_is_only_one_object_inside(SampleInterface $sample) |
185
|
|
|
{ |
186
|
|
|
$this->sortBy([$sample], 'property')->shouldReturn([$sample]); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
function it_does_nothing_if_array_is_empty() |
190
|
|
|
{ |
191
|
|
|
$this->sortBy([], 'property')->shouldReturn([]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
function it_does_nothing_if_collection_is_empty(Collection $collection) |
195
|
|
|
{ |
196
|
|
|
$collection->toArray()->willReturn([]); |
197
|
|
|
|
198
|
|
|
$this->sortBy($collection, 'property')->shouldReturn([]); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|