1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of the Happyr Doctrine Specification package. |
6
|
|
|
* |
7
|
|
|
* (c) Tobias Nyholm <[email protected]> |
8
|
|
|
* Kacper Gunia <[email protected]> |
9
|
|
|
* Peter Gribanov <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace tests\Happyr\DoctrineSpecification\Filter; |
16
|
|
|
|
17
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
18
|
|
|
use Doctrine\ORM\QueryBuilder; |
19
|
|
|
use Happyr\DoctrineSpecification\Filter\Filter; |
20
|
|
|
use Happyr\DoctrineSpecification\Filter\Like; |
21
|
|
|
use PhpSpec\ObjectBehavior; |
22
|
|
|
use tests\Happyr\DoctrineSpecification\Player; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @mixin Like |
26
|
|
|
*/ |
27
|
|
|
final class LikeSpec extends ObjectBehavior |
28
|
|
|
{ |
29
|
|
|
private $field = 'foo'; |
30
|
|
|
|
31
|
|
|
private $value = 'bar'; |
32
|
|
|
|
33
|
|
|
public function let(): void |
34
|
|
|
{ |
35
|
|
|
$this->beConstructedWith($this->field, $this->value, Like::CONTAINS, 'context'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function it_is_an_expression(): void |
39
|
|
|
{ |
40
|
|
|
$this->shouldBeAnInstanceOf(Filter::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function it_surrounds_with_wildcards_when_using_contains( |
|
|
|
|
44
|
|
|
QueryBuilder $qb, |
45
|
|
|
ArrayCollection $parameters |
46
|
|
|
): void { |
47
|
|
|
$this->beConstructedWith($this->field, $this->value, Like::CONTAINS, 'context'); |
48
|
|
|
$qb->getParameters()->willReturn($parameters); |
49
|
|
|
$parameters->count()->willReturn(1); |
50
|
|
|
|
51
|
|
|
$qb->setParameter('comparison_1', '%bar%')->shouldBeCalled(); |
52
|
|
|
|
53
|
|
|
$this->getFilter($qb, 'a'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function it_starts_with_wildcard_when_using_ends_with(QueryBuilder $qb, ArrayCollection $parameters): void |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->beConstructedWith($this->field, $this->value, Like::ENDS_WITH, 'context'); |
59
|
|
|
$qb->getParameters()->willReturn($parameters); |
60
|
|
|
$parameters->count()->willReturn(1); |
61
|
|
|
|
62
|
|
|
$qb->setParameter('comparison_1', '%bar')->shouldBeCalled(); |
63
|
|
|
|
64
|
|
|
$this->getFilter($qb, 'a'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function it_ends_with_wildcard_when_using_starts_with(QueryBuilder $qb, ArrayCollection $parameters): void |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->beConstructedWith($this->field, $this->value, Like::STARTS_WITH, 'context'); |
70
|
|
|
$qb->getParameters()->willReturn($parameters); |
71
|
|
|
$parameters->count()->willReturn(1); |
72
|
|
|
|
73
|
|
|
$qb->setParameter('comparison_1', 'bar%')->shouldBeCalled(); |
74
|
|
|
|
75
|
|
|
$this->getFilter($qb, 'a'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function it_filter_array_collection_starts_with(): void |
79
|
|
|
{ |
80
|
|
|
$this->beConstructedWith('pseudo', 'M', Like::STARTS_WITH, null); |
81
|
|
|
|
82
|
|
|
$players = [ |
83
|
|
|
['pseudo' => 'Joe', 'gender' => 'M', 'points' => 2500], |
84
|
|
|
['pseudo' => 'Moe', 'gender' => 'M', 'points' => 1230], |
85
|
|
|
['pseudo' => 'Alice', 'gender' => 'F', 'points' => 9001], |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
$this->filterCollection($players)->shouldYield([$players[1]]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function it_filter_array_collection_ends_with(): void |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$this->beConstructedWith('pseudo', 'oe', Like::ENDS_WITH, null); |
94
|
|
|
|
95
|
|
|
$players = [ |
96
|
|
|
['pseudo' => 'Joe', 'gender' => 'M', 'points' => 2500], |
97
|
|
|
['pseudo' => 'Moe', 'gender' => 'M', 'points' => 1230], |
98
|
|
|
['pseudo' => 'Alice', 'gender' => 'F', 'points' => 9001], |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
$this->filterCollection($players)->shouldYield([$players[0], $players[1]]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function it_filter_array_collection_contains(): void |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->beConstructedWith('pseudo', 'o', Like::CONTAINS, null); |
107
|
|
|
|
108
|
|
|
$players = [ |
109
|
|
|
['pseudo' => 'Joe', 'gender' => 'M', 'points' => 2500], |
110
|
|
|
['pseudo' => 'Moe', 'gender' => 'M', 'points' => 1230], |
111
|
|
|
['pseudo' => 'Alice', 'gender' => 'F', 'points' => 9001], |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
$this->filterCollection($players)->shouldYield([$players[0], $players[1]]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function it_filter_object_collection_starts_with(): void |
118
|
|
|
{ |
119
|
|
|
$this->beConstructedWith('pseudo', 'M', Like::STARTS_WITH, null); |
120
|
|
|
|
121
|
|
|
$players = [ |
122
|
|
|
new Player('Joe', 'M', 2500), |
123
|
|
|
new Player('Moe', 'M', 1230), |
124
|
|
|
new Player('Alice', 'F', 9001), |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
$this->filterCollection($players)->shouldYield([$players[1]]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
public function it_filter_object_collection_ends_with(): void |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$this->beConstructedWith('pseudo', 'oe', Like::ENDS_WITH, null); |
133
|
|
|
|
134
|
|
|
$players = [ |
135
|
|
|
new Player('Joe', 'M', 2500), |
136
|
|
|
new Player('Moe', 'M', 1230), |
137
|
|
|
new Player('Alice', 'F', 9001), |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
$this->filterCollection($players)->shouldYield([$players[0], $players[1]]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
View Code Duplication |
public function it_filter_object_collection_contains(): void |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
$this->beConstructedWith('pseudo', 'o', Like::CONTAINS, null); |
146
|
|
|
|
147
|
|
|
$players = [ |
148
|
|
|
new Player('Joe', 'M', 2500), |
149
|
|
|
new Player('Moe', 'M', 1230), |
150
|
|
|
new Player('Alice', 'F', 9001), |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
$this->filterCollection($players)->shouldYield([$players[0], $players[1]]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function it_is_satisfied_with_array_starts_with(): void |
157
|
|
|
{ |
158
|
|
|
$this->beConstructedWith('pseudo', 'A', Like::STARTS_WITH, null); |
159
|
|
|
|
160
|
|
|
$playerA = ['pseudo' => 'Joe', 'gender' => 'M', 'points' => 2500]; |
161
|
|
|
$playerB = ['pseudo' => 'Alice', 'gender' => 'F', 'points' => 9001]; |
162
|
|
|
|
163
|
|
|
$this->isSatisfiedBy($playerA)->shouldBe(false); |
164
|
|
|
$this->isSatisfiedBy($playerB)->shouldBe(true); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function it_is_satisfied_with_array_ends_with(): void |
168
|
|
|
{ |
169
|
|
|
$this->beConstructedWith('pseudo', 'oe', Like::ENDS_WITH, null); |
170
|
|
|
|
171
|
|
|
$playerA = ['pseudo' => 'Joe', 'gender' => 'M', 'points' => 2500]; |
172
|
|
|
$playerB = ['pseudo' => 'Alice', 'gender' => 'F', 'points' => 9001]; |
173
|
|
|
|
174
|
|
|
$this->isSatisfiedBy($playerA)->shouldBe(true); |
175
|
|
|
$this->isSatisfiedBy($playerB)->shouldBe(false); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function it_is_satisfied_with_array_contains(): void |
179
|
|
|
{ |
180
|
|
|
$this->beConstructedWith('pseudo', 'oe', Like::CONTAINS, null); |
181
|
|
|
|
182
|
|
|
$playerA = ['pseudo' => 'Joe', 'gender' => 'M', 'points' => 2500]; |
183
|
|
|
$playerB = ['pseudo' => 'Alice', 'gender' => 'F', 'points' => 9001]; |
184
|
|
|
|
185
|
|
|
$this->isSatisfiedBy($playerA)->shouldBe(true); |
186
|
|
|
$this->isSatisfiedBy($playerB)->shouldBe(false); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function it_is_satisfied_with_object_starts_with(): void |
190
|
|
|
{ |
191
|
|
|
$this->beConstructedWith('pseudo', 'A', Like::STARTS_WITH, null); |
192
|
|
|
|
193
|
|
|
$playerA = new Player('Joe', 'M', 2500); |
194
|
|
|
$playerB = new Player('Alice', 'F', 9001); |
195
|
|
|
|
196
|
|
|
$this->isSatisfiedBy($playerA)->shouldBe(false); |
197
|
|
|
$this->isSatisfiedBy($playerB)->shouldBe(true); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function it_is_satisfied_with_object_ends_with(): void |
201
|
|
|
{ |
202
|
|
|
$this->beConstructedWith('pseudo', 'oe', Like::ENDS_WITH, null); |
203
|
|
|
|
204
|
|
|
$playerA = new Player('Joe', 'M', 2500); |
205
|
|
|
$playerB = new Player('Alice', 'F', 9001); |
206
|
|
|
|
207
|
|
|
$this->isSatisfiedBy($playerA)->shouldBe(true); |
208
|
|
|
$this->isSatisfiedBy($playerB)->shouldBe(false); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function it_is_satisfied_with_object_contains(): void |
212
|
|
|
{ |
213
|
|
|
$this->beConstructedWith('pseudo', 'oe', Like::CONTAINS, null); |
214
|
|
|
|
215
|
|
|
$playerA = new Player('Joe', 'M', 2500); |
216
|
|
|
$playerB = new Player('Alice', 'F', 9001); |
217
|
|
|
|
218
|
|
|
$this->isSatisfiedBy($playerA)->shouldBe(true); |
219
|
|
|
$this->isSatisfiedBy($playerB)->shouldBe(false); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.