forked from Sylius/Sylius
/
SortByExtensionSpec.php
189 lines (162 loc) · 5.4 KB
/
SortByExtensionSpec.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace spec\Sylius\Bundle\UiBundle\Twig;
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\UiBundle\spec\Fixtures\SampleInterface;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
/**
* @author Jan Góralski <jan.goralski@lakion.com>
*/
final class SortByExtensionSpec extends ObjectBehavior
{
function it_extends_twig_extensions(): void
{
$this->shouldHaveType(\Twig_Extension::class);
}
function it_sorts_in_ascending_order_by_default(
SampleInterface $firstSample,
SampleInterface $secondSample,
SampleInterface $thirdSample
): void {
$firstSample->getInt()->willReturn(3);
$secondSample->getInt()->willReturn(5);
$thirdSample->getInt()->willReturn(1);
$arrayBeforeSorting = [
$firstSample,
$secondSample,
$thirdSample,
];
$this->sortBy($arrayBeforeSorting, 'int')->shouldReturn(
[
$thirdSample,
$firstSample,
$secondSample,
]
);
}
function it_sorts_an_array_of_objects_by_various_properties(
SampleInterface $firstSample,
SampleInterface $secondSample,
SampleInterface $thirdSample
): void {
$firstSample->getInt()->willReturn(3);
$secondSample->getInt()->willReturn(5);
$thirdSample->getInt()->willReturn(1);
$firstSample->getString()->willReturn('true');
$secondSample->getString()->willReturn('123');
$thirdSample->getString()->willReturn('Alohomora');
$firstSample->getBizarrelyNamedProperty()->willReturn('banana');
$secondSample->getBizarrelyNamedProperty()->willReturn(123);
$thirdSample->getBizarrelyNamedProperty()->willReturn(null);
$arrayBeforeSorting = [
$firstSample,
$secondSample,
$thirdSample,
];
$this->sortBy($arrayBeforeSorting, 'int')->shouldReturn(
[
$thirdSample,
$firstSample,
$secondSample,
]
);
$this->sortBy($arrayBeforeSorting, 'string')->shouldReturn(
[
$secondSample,
$thirdSample,
$firstSample,
]
);
$this->sortBy($arrayBeforeSorting, 'bizarrelyNamedProperty')->shouldReturn(
[
$thirdSample,
$secondSample,
$firstSample,
]
);
}
function it_sorts_an_array_of_objects_in_descending_order_by_a_property(
SampleInterface $firstSample,
SampleInterface $secondSample,
SampleInterface $thirdSample
): void {
$firstSample->getInt()->willReturn(3);
$secondSample->getInt()->willReturn(5);
$thirdSample->getInt()->willReturn(1);
$arrayBeforeSorting = [
$firstSample,
$secondSample,
$thirdSample,
];
$this->sortBy($arrayBeforeSorting, 'int', 'DESC')->shouldReturn(
[
$secondSample,
$firstSample,
$thirdSample,
]
);
}
function it_sorts_an_array_of_objects_by_a_nested_property(
SampleInterface $firstSample,
SampleInterface $secondSample,
SampleInterface $thirdSample,
SampleInterface $firstInnerSample,
SampleInterface $secondInnerSample,
SampleInterface $thirdInnerSample
): void {
$firstInnerSample->getString()->willReturn('m');
$secondInnerSample->getString()->willReturn('Z');
$thirdInnerSample->getString()->willReturn('A');
$firstSample->getInnerSample()->willReturn($firstInnerSample);
$secondSample->getInnerSample()->willReturn($secondInnerSample);
$thirdSample->getInnerSample()->willReturn($thirdInnerSample);
$arrayBeforeSorting = [
$firstSample,
$secondSample,
$thirdSample,
];
$this->sortBy($arrayBeforeSorting, 'innerSample.string', 'ASC')->shouldReturn(
[
$thirdSample,
$firstSample,
$secondSample,
]
);
}
function it_throws_an_exception_if_the_property_is_not_found_on_objects(
SampleInterface $firstSample,
SampleInterface $secondSample,
SampleInterface $thirdSample
): void {
$arrayBeforeSorting = [
$firstSample,
$secondSample,
$thirdSample,
];
$this
->shouldThrow(NoSuchPropertyException::class)
->during('sortBy', [$arrayBeforeSorting, 'nonExistingProperty'])
;
}
function it_return_input_array_if_there_is_only_one_object_inside(SampleInterface $sample): void
{
$this->sortBy([$sample], 'property')->shouldReturn([$sample]);
}
function it_does_nothing_if_array_is_empty(): void
{
$this->sortBy([], 'property')->shouldReturn([]);
}
function it_does_nothing_if_collection_is_empty(): void
{
$this->sortBy(new ArrayCollection(), 'property')->shouldReturn([]);
}
}