1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Tests\Objects; |
4
|
|
|
|
5
|
|
|
use Mado\QueryBundle\Queries\QueryBuilderOptions; |
6
|
|
|
use Mado\QueryBundle\Repositories\BaseRepository; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @coversDefaultClass \Mado\QueryBundle\Repositories\BaseRepository |
11
|
|
|
*/ |
12
|
|
|
class BaseRepositoryTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function setUp() |
15
|
|
|
{ |
16
|
|
|
$this->entityManager = $this |
17
|
|
|
->getMockBuilder('Doctrine\ORM\EntityManager') |
18
|
|
|
->disableOriginalConstructor() |
19
|
|
|
->getMock(); |
20
|
|
|
|
21
|
|
|
$this->classMetaData = $this |
22
|
|
|
->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata') |
23
|
|
|
->disableOriginalConstructor() |
24
|
|
|
->getMock(); |
25
|
|
|
|
26
|
|
|
$this->classMetaData->fieldMappings = [ |
27
|
|
|
'foo' => 'bar', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
$this->classMetaData->name = 'fooo'; |
31
|
|
|
|
32
|
|
|
$this->repository = new BaseRepository( |
33
|
|
|
$this->entityManager, |
34
|
|
|
$this->classMetaData |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @covers Mado\QueryBundle\Queries\AbstractQuery::__construct |
40
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::__construct |
41
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::getEntityAlias |
42
|
|
|
*/ |
43
|
|
|
public function testProvideEntityAliasByFQCN() |
44
|
|
|
{ |
45
|
|
|
$this->assertEquals( |
46
|
|
|
'classname', |
47
|
|
|
$this->repository->getEntityAlias('Mado\\QueryBundle\\Entity\\ClassName') |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @covers Mado\QueryBundle\Queries\AbstractQuery::__construct |
53
|
|
|
* @covers Mado\QueryBundle\Queries\QueryBuilderOptions::__construct |
54
|
|
|
* @covers Mado\QueryBundle\Queries\QueryBuilderOptions::fromArray |
55
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::__construct |
56
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::getQueryBuilderOptions |
57
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::getRequestAttributes |
58
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::setQueryOptionsFromRequest |
59
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::setRequest |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function testBuildOptionsViaRequest() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$this->attributeParameterBag = $this |
64
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag') |
65
|
|
|
->setMethods(['all']) |
66
|
|
|
->disableOriginalConstructor() |
67
|
|
|
->getMock(); |
68
|
|
|
$this->attributeParameterBag->expects($this->once()) |
69
|
|
|
->method('all') |
70
|
|
|
->will($this->returnValue([ |
71
|
|
|
// the collection of attributesd |
72
|
|
|
])); |
73
|
|
|
|
74
|
|
|
$this->queryParameterBag = $this |
75
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag') |
76
|
|
|
->setMethods(['get']) |
77
|
|
|
->disableOriginalConstructor() |
78
|
|
|
->getMock(); |
79
|
|
|
$this->queryParameterBag->method('get') |
80
|
|
|
->will($this->returnValue([ |
81
|
|
|
// the collection of attributesd |
82
|
|
|
])); |
83
|
|
|
|
84
|
|
|
$this->request = $this |
85
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\Request') |
86
|
|
|
->disableOriginalConstructor() |
87
|
|
|
->getMock(); |
88
|
|
|
$this->request->attributes = $this->attributeParameterBag; |
89
|
|
|
$this->request->query = $this->queryParameterBag; |
90
|
|
|
|
91
|
|
|
$this->repository->setRequest($this->request); |
92
|
|
|
|
93
|
|
|
$this->assertEquals( |
94
|
|
|
QueryBuilderOptions::fromArray([ |
95
|
|
|
'filtering' => [], |
96
|
|
|
'orFiltering' => [], |
97
|
|
|
'limit' => [], |
98
|
|
|
'page' => [], |
99
|
|
|
'filters' => [], |
100
|
|
|
'orFilters' => [], |
101
|
|
|
'sorting' => [], |
102
|
|
|
'rel' => [], |
103
|
|
|
'printing' => [], |
104
|
|
|
'select' => [], |
105
|
|
|
]), |
106
|
|
|
$this->repository->getQueryBuilderOptions() |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @covers Mado\QueryBundle\Queries\AbstractQuery::__construct |
112
|
|
|
* @covers Mado\QueryBundle\Queries\QueryBuilderOptions::__construct |
113
|
|
|
* @covers Mado\QueryBundle\Queries\QueryBuilderOptions::fromArray |
114
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::__construct |
115
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::getRequestAttributes |
116
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::setQueryOptionsFromRequest |
117
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::getQueryBuilderOptions |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function testBuildQueryOptionsFromRequest() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$this->attributeParameterBag = $this |
122
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag') |
123
|
|
|
->setMethods(['all']) |
124
|
|
|
->disableOriginalConstructor() |
125
|
|
|
->getMock(); |
126
|
|
|
$this->attributeParameterBag->expects($this->once()) |
127
|
|
|
->method('all') |
128
|
|
|
->will($this->returnValue([ |
129
|
|
|
// the collection of attributesd |
130
|
|
|
])); |
131
|
|
|
|
132
|
|
|
$this->queryParameterBag = $this |
133
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag') |
134
|
|
|
->setMethods([ |
135
|
|
|
'get', |
136
|
|
|
]) |
137
|
|
|
->disableOriginalConstructor() |
138
|
|
|
->getMock(); |
139
|
|
|
$this->queryParameterBag->method('get') |
140
|
|
|
->will($this->returnValue([ |
141
|
|
|
// the collection of attributesd |
142
|
|
|
])); |
143
|
|
|
|
144
|
|
|
$this->request = $this |
145
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\Request') |
146
|
|
|
->disableOriginalConstructor() |
147
|
|
|
->getMock(); |
148
|
|
|
$this->request->attributes = $this->attributeParameterBag; |
149
|
|
|
$this->request->query = $this->queryParameterBag; |
150
|
|
|
|
151
|
|
|
$this->repository->setQueryOptionsFromRequest($this->request); |
152
|
|
|
|
153
|
|
|
$this->assertEquals( |
154
|
|
|
QueryBuilderOptions::fromArray([ |
155
|
|
|
'filtering' => [], |
156
|
|
|
'orFiltering' => [], |
157
|
|
|
'limit' => [], |
158
|
|
|
'page' => [], |
159
|
|
|
'filters' => [], |
160
|
|
|
'orFilters' => [], |
161
|
|
|
'sorting' => [], |
162
|
|
|
'rel' => [], |
163
|
|
|
'printing' => [], |
164
|
|
|
'select' => [], |
165
|
|
|
]), |
166
|
|
|
$this->repository->getQueryBuilderOptions() |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @covers Mado\QueryBundle\Queries\AbstractQuery::__construct |
172
|
|
|
* @covers Mado\QueryBundle\Queries\QueryBuilderOptions::__construct |
173
|
|
|
* @covers Mado\QueryBundle\Queries\QueryBuilderOptions::fromArray |
174
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::__construct |
175
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::getRequestAttributes |
176
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::setQueryOptionsFromRequest |
177
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::getQueryBuilderOptions |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function testBuildQueryOptionsFromRequestWithCustomFilter() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$this->attributeParameterBag = $this |
182
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag') |
183
|
|
|
->setMethods(['all']) |
184
|
|
|
->disableOriginalConstructor() |
185
|
|
|
->getMock(); |
186
|
|
|
$this->attributeParameterBag->expects($this->once()) |
187
|
|
|
->method('all') |
188
|
|
|
->will($this->returnValue([ |
189
|
|
|
// the collection of attributesd |
190
|
|
|
])); |
191
|
|
|
|
192
|
|
|
$this->queryParameterBag = $this |
193
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag') |
194
|
|
|
->setMethods([ |
195
|
|
|
'get', |
196
|
|
|
]) |
197
|
|
|
->disableOriginalConstructor() |
198
|
|
|
->getMock(); |
199
|
|
|
$this->queryParameterBag->method('get') |
200
|
|
|
->will($this->returnValue([ |
201
|
|
|
// the collection of attributesd |
202
|
|
|
])); |
203
|
|
|
|
204
|
|
|
$this->request = $this |
205
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\Request') |
206
|
|
|
->disableOriginalConstructor() |
207
|
|
|
->getMock(); |
208
|
|
|
$this->request->attributes = $this->attributeParameterBag; |
209
|
|
|
$this->request->query = $this->queryParameterBag; |
210
|
|
|
|
211
|
|
|
$this->repository->setQueryOptionsFromRequest($this->request); |
212
|
|
|
|
213
|
|
|
$this->assertEquals( |
214
|
|
|
QueryBuilderOptions::fromArray([ |
215
|
|
|
'filtering' => [], |
216
|
|
|
'orFiltering' => [], |
217
|
|
|
'limit' => [], |
218
|
|
|
'page' => [], |
219
|
|
|
'filters' => [], |
220
|
|
|
'orFilters' => [], |
221
|
|
|
'sorting' => [], |
222
|
|
|
'rel' => [], |
223
|
|
|
'printing' => [], |
224
|
|
|
'select' => [], |
225
|
|
|
]), |
226
|
|
|
$this->repository->getQueryBuilderOptions() |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @covers Mado\QueryBundle\Queries\AbstractQuery::__construct |
232
|
|
|
* @covers Mado\QueryBundle\Queries\QueryBuilderOptions::__construct |
233
|
|
|
* @covers Mado\QueryBundle\Queries\QueryBuilderOptions::fromArray |
234
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::__construct |
235
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::getRequestAttributes |
236
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::setQueryOptionsFromRequestWithCustomOrFilter |
237
|
|
|
* @covers Mado\QueryBundle\Repositories\BaseRepository::getQueryBuilderOptions |
238
|
|
|
*/ |
239
|
|
|
public function testBuildQueryOptionsFromRequestWithCustomOrFilter() |
240
|
|
|
{ |
241
|
|
|
$this->attributeParameterBag = $this |
242
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag') |
243
|
|
|
->setMethods([ |
244
|
|
|
'all', |
245
|
|
|
'get', |
246
|
|
|
]) |
247
|
|
|
->disableOriginalConstructor() |
248
|
|
|
->getMock(); |
249
|
|
|
$this->attributeParameterBag->expects($this->once()) |
250
|
|
|
->method('all') |
251
|
|
|
->will($this->returnValue([ |
252
|
|
|
// the collection of attributesd |
253
|
|
|
])); |
254
|
|
|
$this->attributeParameterBag->method('get') |
255
|
|
|
->will($this->returnValue([ |
256
|
|
|
// the collection of attributesd |
257
|
|
|
])); |
258
|
|
|
|
259
|
|
|
$this->queryParameterBag = $this |
260
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag') |
261
|
|
|
->setMethods(['get']) |
262
|
|
|
->disableOriginalConstructor() |
263
|
|
|
->getMock(); |
264
|
|
|
$this->queryParameterBag->method('get') |
265
|
|
|
->will($this->returnValue([ |
266
|
|
|
// the collection of attributesd |
267
|
|
|
])); |
268
|
|
|
|
269
|
|
|
$this->request = $this |
270
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\Request') |
271
|
|
|
->disableOriginalConstructor() |
272
|
|
|
->getMock(); |
273
|
|
|
$this->request->attributes = $this->attributeParameterBag; |
274
|
|
|
$this->request->query = $this->queryParameterBag; |
275
|
|
|
|
276
|
|
|
$this->repository->setQueryOptionsFromRequestWithCustomOrFilter( |
277
|
|
|
$this->request, |
278
|
|
|
$orFilter = [] |
279
|
|
|
); |
280
|
|
|
|
281
|
|
|
$this->assertEquals( |
282
|
|
|
QueryBuilderOptions::fromArray([ |
283
|
|
|
'_route' => [], |
284
|
|
|
'customer_id' => [], |
285
|
|
|
'id' => [], |
286
|
|
|
'_route' => [], |
287
|
|
|
'filtering' => [], |
288
|
|
|
'limit' => [], |
289
|
|
|
'page' => [], |
290
|
|
|
'filters' => [], |
291
|
|
|
'orFilters' => [], |
292
|
|
|
'sorting' => [], |
293
|
|
|
'rel' => [], |
294
|
|
|
'printing' => [], |
295
|
|
|
'select' => [], |
296
|
|
|
]), |
297
|
|
|
$this->repository->getQueryBuilderOptions() |
298
|
|
|
); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
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.