1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Component\Grid\Tests\DataSource\Doctrine\ORM; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Query; |
15
|
|
|
use Doctrine\ORM\Query\Expr; |
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
17
|
|
|
use Lug\Component\Grid\DataSource\ArrayDataSource; |
18
|
|
|
use Lug\Component\Grid\DataSource\DataSourceBuilderInterface; |
19
|
|
|
use Lug\Component\Grid\DataSource\Doctrine\ORM\DataSourceBuilder; |
20
|
|
|
use Lug\Component\Grid\DataSource\Doctrine\ORM\ExpressionBuilder; |
21
|
|
|
use Lug\Component\Grid\DataSource\PagerfantaDataSource; |
22
|
|
|
use Lug\Component\Resource\Repository\Doctrine\ORM\Repository; |
23
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author GeLo <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class DataSourceBuilderTest extends \PHPUnit_Framework_TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var DataSourceBuilder |
32
|
|
|
*/ |
33
|
|
|
private $builder; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|Repository |
37
|
|
|
*/ |
38
|
|
|
private $repository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|QueryBuilder |
42
|
|
|
*/ |
43
|
|
|
private $queryBuilder; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected function setUp() |
49
|
|
|
{ |
50
|
|
|
$this->repository = $this->createRepositoryMock(); |
51
|
|
|
$this->queryBuilder = $this->createQueryBuilderMock(); |
52
|
|
|
|
53
|
|
|
$this->repository |
|
|
|
|
54
|
|
|
->expects($this->once()) |
55
|
|
|
->method('createQueryBuilderForCollection') |
56
|
|
|
->will($this->returnValue($this->queryBuilder)); |
57
|
|
|
|
58
|
|
|
$this->builder = new DataSourceBuilder($this->repository); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testInheritance() |
62
|
|
|
{ |
63
|
|
|
$this->assertInstanceOf(DataSourceBuilderInterface::class, $this->builder); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function testSelect() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$this->queryBuilder |
|
|
|
|
69
|
|
|
->expects($this->once()) |
70
|
|
|
->method('addSelect') |
71
|
|
|
->with($this->identicalTo($select = 'alias')); |
72
|
|
|
|
73
|
|
|
$this->assertSame($this->builder, $this->builder->select($select)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testInnerJoin() |
77
|
|
|
{ |
78
|
|
|
$this->queryBuilder |
|
|
|
|
79
|
|
|
->expects($this->once()) |
80
|
|
|
->method('innerJoin') |
81
|
|
|
->with( |
82
|
|
|
$this->identicalTo($join = 'property'), |
83
|
|
|
$this->identicalTo($alias = 'alias') |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$this->assertSame($this->builder, $this->builder->innerJoin($join, $alias)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testLeftJoin() |
90
|
|
|
{ |
91
|
|
|
$this->queryBuilder |
|
|
|
|
92
|
|
|
->expects($this->once()) |
93
|
|
|
->method('leftJoin') |
94
|
|
|
->with( |
95
|
|
|
$this->identicalTo($join = 'property'), |
96
|
|
|
$this->identicalTo($alias = 'alias') |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->assertSame($this->builder, $this->builder->leftJoin($join, $alias)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function testAndWhere() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$this->queryBuilder |
|
|
|
|
105
|
|
|
->expects($this->once()) |
106
|
|
|
->method('andWhere') |
107
|
|
|
->with($this->identicalTo($where = 'expression')); |
108
|
|
|
|
109
|
|
|
$this->assertSame($this->builder, $this->builder->andWhere($where)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function testOrWhere() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$this->queryBuilder |
|
|
|
|
115
|
|
|
->expects($this->once()) |
116
|
|
|
->method('orWhere') |
117
|
|
|
->with($this->identicalTo($where = 'expression')); |
118
|
|
|
|
119
|
|
|
$this->assertSame($this->builder, $this->builder->orWhere($where)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
public function testOrderBy() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$this->queryBuilder |
|
|
|
|
125
|
|
|
->expects($this->once()) |
126
|
|
|
->method('addOrderBy') |
127
|
|
|
->with( |
128
|
|
|
$this->identicalTo($sort = 'expression'), |
129
|
|
|
$this->identicalTo('ASC') |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$this->assertSame($this->builder, $this->builder->orderBy($sort)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testOrderByWithOrder() |
136
|
|
|
{ |
137
|
|
|
$this->queryBuilder |
|
|
|
|
138
|
|
|
->expects($this->once()) |
139
|
|
|
->method('addOrderBy') |
140
|
|
|
->with( |
141
|
|
|
$this->identicalTo($sort = 'expression'), |
142
|
|
|
$this->identicalTo($order = 'DESC') |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$this->assertSame($this->builder, $this->builder->orderBy($sort, $order)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testParameter() |
149
|
|
|
{ |
150
|
|
|
$this->queryBuilder |
|
|
|
|
151
|
|
|
->expects($this->once()) |
152
|
|
|
->method('setParameter') |
153
|
|
|
->with( |
154
|
|
|
$this->identicalTo($parameter = 'parameter'), |
155
|
|
|
$this->identicalTo($value = 'value') |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$this->assertSame($this->builder, $this->builder->setParameter($parameter, $value)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testParameterWithType() |
162
|
|
|
{ |
163
|
|
|
$this->queryBuilder |
|
|
|
|
164
|
|
|
->expects($this->once()) |
165
|
|
|
->method('setParameter') |
166
|
|
|
->with( |
167
|
|
|
$this->identicalTo($parameter = 'parameter'), |
168
|
|
|
$this->identicalTo($value = 'value'), |
169
|
|
|
$this->identicalTo($type = 'type') |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$this->assertSame($this->builder, $this->builder->setParameter($parameter, $value, $type)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testPlaceholder() |
176
|
|
|
{ |
177
|
|
|
$this->queryBuilder |
|
|
|
|
178
|
|
|
->expects($this->once()) |
179
|
|
|
->method('setParameter') |
180
|
|
|
->with( |
181
|
|
|
$this->matchesRegularExpression('/'.($pattern = 'foo_bar_[a-z0-9]{22}').'/'), |
182
|
|
|
$this->identicalTo($value = 'value') |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$this->assertRegExp('/\:'.$pattern.'/', $this->builder->createPlaceholder('foo.bar', $value)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testPlaceholderUnicity() |
189
|
|
|
{ |
190
|
|
|
$this->queryBuilder |
|
|
|
|
191
|
|
|
->expects($this->exactly(2)) |
192
|
|
|
->method('setParameter') |
193
|
|
|
->with( |
194
|
|
|
$this->matchesRegularExpression('/'.($pattern = 'foo_bar_[a-z0-9]{22}').'/'), |
195
|
|
|
$this->identicalTo($value = 'value') |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$this->assertRegExp( |
199
|
|
|
$resultPattern = '/\:'.$pattern.'/', |
200
|
|
|
$placeholder1 = $this->builder->createPlaceholder('foo.bar', $value) |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
$this->assertRegExp($resultPattern, $placeholder2 = $this->builder->createPlaceholder('foo.bar', $value)); |
204
|
|
|
$this->assertNotSame($placeholder1, $placeholder2); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testProperty() |
208
|
|
|
{ |
209
|
|
|
$this->repository |
|
|
|
|
210
|
|
|
->expects($this->once()) |
211
|
|
|
->method('getProperty') |
212
|
|
|
->with( |
213
|
|
|
$this->identicalTo($field = 'field'), |
214
|
|
|
$this->identicalTo($this->queryBuilder) |
215
|
|
|
) |
216
|
|
|
->will($this->returnValue($property = 'property')); |
217
|
|
|
|
218
|
|
|
$this->assertSame($property, $this->builder->getProperty($field)); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
View Code Duplication |
public function testAliases() |
|
|
|
|
222
|
|
|
{ |
223
|
|
|
$this->queryBuilder |
|
|
|
|
224
|
|
|
->expects($this->once()) |
225
|
|
|
->method('getAllAliases') |
226
|
|
|
->will($this->returnValue($aliases = ['foo'])); |
227
|
|
|
|
228
|
|
|
$this->assertSame($aliases, $this->builder->getAliases()); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testExpressionBuilder() |
232
|
|
|
{ |
233
|
|
|
$this->queryBuilder |
|
|
|
|
234
|
|
|
->expects($this->once()) |
235
|
|
|
->method('expr') |
236
|
|
|
->will($this->returnValue($this->createExprMock())); |
237
|
|
|
|
238
|
|
|
$this->assertInstanceOf(ExpressionBuilder::class, $this->builder->getExpressionBuilder()); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
View Code Duplication |
public function testCreateDataSource() |
|
|
|
|
242
|
|
|
{ |
243
|
|
|
$dataSource = $this->builder->createDataSource(); |
244
|
|
|
|
245
|
|
|
$this->assertInstanceOf(PagerfantaDataSource::class, $dataSource); |
246
|
|
|
$this->assertInstanceOf(DoctrineORMAdapter::class, $dataSource->getAdapter()); |
|
|
|
|
247
|
|
|
|
248
|
|
|
$this->assertSame(10, $dataSource->getMaxPerPage()); |
|
|
|
|
249
|
|
|
$this->assertSame(1, $dataSource->getCurrentPage()); |
|
|
|
|
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testCreateDataSourceWithAllOption() |
253
|
|
|
{ |
254
|
|
|
$this->queryBuilder |
|
|
|
|
255
|
|
|
->expects($this->once()) |
256
|
|
|
->method('getQuery') |
257
|
|
|
->will($this->returnValue($query = $this->createQueryMock())); |
258
|
|
|
|
259
|
|
|
$query |
|
|
|
|
260
|
|
|
->expects($this->once()) |
261
|
|
|
->method('getResult') |
262
|
|
|
->will($this->returnValue($values = [new \stdClass()])); |
263
|
|
|
|
264
|
|
|
$dataSource = $this->builder->createDataSource(['all' => true]); |
265
|
|
|
|
266
|
|
|
$this->assertInstanceOf(ArrayDataSource::class, $dataSource); |
267
|
|
|
$this->assertSame($values, iterator_to_array($dataSource)); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function testLimit() |
271
|
|
|
{ |
272
|
|
|
$this->assertSame($this->builder, $this->builder->setLimit($limit = 20)); |
273
|
|
|
$this->assertSame($limit, $this->builder->createDataSource()->getMaxPerPage()); |
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function testPage() |
277
|
|
|
{ |
278
|
|
|
$this->assertSame($this->builder, $this->builder->setPage($page = 1)); |
279
|
|
|
$this->assertSame($page, $this->builder->createDataSource()->getCurrentPage()); |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Repository |
284
|
|
|
*/ |
285
|
|
|
private function createRepositoryMock() |
286
|
|
|
{ |
287
|
|
|
return $this->getMockBuilder(Repository::class) |
288
|
|
|
->disableOriginalConstructor() |
289
|
|
|
->getMock(); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|QueryBuilder |
294
|
|
|
*/ |
295
|
|
|
private function createQueryBuilderMock() |
296
|
|
|
{ |
297
|
|
|
return $this->getMockBuilder(QueryBuilder::class) |
298
|
|
|
->disableOriginalConstructor() |
299
|
|
|
->disableOriginalClone() |
300
|
|
|
->getMock(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Query |
305
|
|
|
*/ |
306
|
|
|
private function createQueryMock() |
307
|
|
|
{ |
308
|
|
|
return $this->getMock(\stdClass::class, ['getResult']); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Expr |
313
|
|
|
*/ |
314
|
|
|
private function createExprMock() |
315
|
|
|
{ |
316
|
|
|
return $this->getMock(Expr::class); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: