|
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\MongoDB; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\MongoDB\Iterator; |
|
15
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
|
16
|
|
|
use Doctrine\ODM\MongoDB\Query\Query; |
|
17
|
|
|
use Lug\Component\Grid\DataSource\ArrayDataSource; |
|
18
|
|
|
use Lug\Component\Grid\DataSource\DataSourceBuilderInterface; |
|
19
|
|
|
use Lug\Component\Grid\DataSource\Doctrine\MongoDB\DataSourceBuilder; |
|
20
|
|
|
use Lug\Component\Grid\DataSource\Doctrine\MongoDB\ExpressionBuilder; |
|
21
|
|
|
use Lug\Component\Grid\DataSource\PagerfantaDataSource; |
|
22
|
|
|
use Lug\Component\Resource\Repository\Doctrine\MongoDB\Repository; |
|
23
|
|
|
use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter; |
|
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|Builder |
|
42
|
|
|
*/ |
|
43
|
|
|
private $queryBuilder; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function setUp() |
|
49
|
|
|
{ |
|
50
|
|
|
if (!class_exists(Builder::class)) { |
|
51
|
|
|
$this->markTestSkipped(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->repository = $this->createRepositoryMock(); |
|
55
|
|
|
$this->queryBuilder = $this->createQueryBuilderMock(); |
|
56
|
|
|
|
|
57
|
|
|
$this->repository |
|
|
|
|
|
|
58
|
|
|
->expects($this->once()) |
|
59
|
|
|
->method('createQueryBuilderForCollection') |
|
60
|
|
|
->will($this->returnValue($this->queryBuilder)); |
|
61
|
|
|
|
|
62
|
|
|
$this->builder = new DataSourceBuilder($this->repository); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testInheritance() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->assertInstanceOf(DataSourceBuilderInterface::class, $this->builder); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testSelect() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->assertSame($this->builder, $this->builder->select('select')); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testInnerJoin() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->queryBuilder |
|
|
|
|
|
|
78
|
|
|
->expects($this->once()) |
|
79
|
|
|
->method('field') |
|
80
|
|
|
->with($this->identicalTo($join = 'property')) |
|
81
|
|
|
->will($this->returnSelf()); |
|
82
|
|
|
|
|
83
|
|
|
$this->queryBuilder |
|
84
|
|
|
->expects($this->once()) |
|
85
|
|
|
->method('notEqual') |
|
86
|
|
|
->with($this->isNull()) |
|
87
|
|
|
->will($this->returnSelf()); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertSame($this->builder, $this->builder->innerJoin($join, 'alias')); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testLeftJoin() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->assertSame($this->builder, $this->builder->leftJoin('property', 'alias')); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testAndWhere() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->queryBuilder |
|
|
|
|
|
|
100
|
|
|
->expects($this->once()) |
|
101
|
|
|
->method('addAnd') |
|
102
|
|
|
->with($this->identicalTo($where = 'expression')); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertSame($this->builder, $this->builder->andWhere($where)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testOrWhere() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->queryBuilder |
|
|
|
|
|
|
110
|
|
|
->expects($this->once()) |
|
111
|
|
|
->method('addOr') |
|
112
|
|
|
->with($this->identicalTo($where = 'expression')); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertSame($this->builder, $this->builder->orWhere($where)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
public function testOrderBy() |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$this->queryBuilder |
|
|
|
|
|
|
120
|
|
|
->expects($this->once()) |
|
121
|
|
|
->method('sort') |
|
122
|
|
|
->with( |
|
123
|
|
|
$this->identicalTo($sort = 'expression'), |
|
124
|
|
|
$this->identicalTo('ASC') |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertSame($this->builder, $this->builder->orderBy($sort)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testOrderByWithOrder() |
|
131
|
|
|
{ |
|
132
|
|
|
$this->queryBuilder |
|
|
|
|
|
|
133
|
|
|
->expects($this->once()) |
|
134
|
|
|
->method('sort') |
|
135
|
|
|
->with( |
|
136
|
|
|
$this->identicalTo($sort = 'expression'), |
|
137
|
|
|
$this->identicalTo($order = 'DESC') |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertSame($this->builder, $this->builder->orderBy($sort, $order)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testParameter() |
|
144
|
|
|
{ |
|
145
|
|
|
$this->assertSame($this->builder, $this->builder->setParameter('parameter', 'value')); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function testPlaceholder() |
|
149
|
|
|
{ |
|
150
|
|
|
$this->assertSame($value = 'foo', $this->builder->createPlaceholder('parameter', $value)); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testProperty() |
|
154
|
|
|
{ |
|
155
|
|
|
$this->repository |
|
|
|
|
|
|
156
|
|
|
->expects($this->once()) |
|
157
|
|
|
->method('getProperty') |
|
158
|
|
|
->with( |
|
159
|
|
|
$this->identicalTo($field = 'field'), |
|
160
|
|
|
$this->identicalTo($this->queryBuilder) |
|
161
|
|
|
) |
|
162
|
|
|
->will($this->returnValue($property = 'property')); |
|
163
|
|
|
|
|
164
|
|
|
$this->assertSame($property, $this->builder->getProperty($field)); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function testAliases() |
|
168
|
|
|
{ |
|
169
|
|
|
$this->assertEmpty($this->builder->getAliases()); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function testExpressionBuilder() |
|
173
|
|
|
{ |
|
174
|
|
|
$this->assertInstanceOf(ExpressionBuilder::class, $this->builder->getExpressionBuilder()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
View Code Duplication |
public function testCreateDataSource() |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
$dataSource = $this->builder->createDataSource(); |
|
180
|
|
|
|
|
181
|
|
|
$this->assertInstanceOf(PagerfantaDataSource::class, $dataSource); |
|
182
|
|
|
$this->assertInstanceOf(DoctrineODMMongoDBAdapter::class, $dataSource->getAdapter()); |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
$this->assertSame(10, $dataSource->getMaxPerPage()); |
|
|
|
|
|
|
185
|
|
|
$this->assertSame(1, $dataSource->getCurrentPage()); |
|
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function testCreateDataSourceWithAllOption() |
|
189
|
|
|
{ |
|
190
|
|
|
$this->queryBuilder |
|
|
|
|
|
|
191
|
|
|
->expects($this->once()) |
|
192
|
|
|
->method('getQuery') |
|
193
|
|
|
->will($this->returnValue($query = $this->createQueryMock())); |
|
194
|
|
|
|
|
195
|
|
|
$query |
|
|
|
|
|
|
196
|
|
|
->expects($this->once()) |
|
197
|
|
|
->method('getIterator') |
|
198
|
|
|
->will($this->returnValue($iterator = $this->createIteratorMock())); |
|
199
|
|
|
|
|
200
|
|
|
$iterator |
|
|
|
|
|
|
201
|
|
|
->expects($this->once()) |
|
202
|
|
|
->method('toArray') |
|
203
|
|
|
->will($this->returnValue($values = [new \stdClass()])); |
|
204
|
|
|
|
|
205
|
|
|
$dataSource = $this->builder->createDataSource(['all' => true]); |
|
206
|
|
|
|
|
207
|
|
|
$this->assertInstanceOf(ArrayDataSource::class, $dataSource); |
|
208
|
|
|
$this->assertSame($values, iterator_to_array($dataSource)); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function testLimit() |
|
212
|
|
|
{ |
|
213
|
|
|
$this->assertSame($this->builder, $this->builder->setLimit($limit = 20)); |
|
214
|
|
|
$this->assertSame($limit, $this->builder->createDataSource()->getMaxPerPage()); |
|
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function testPage() |
|
218
|
|
|
{ |
|
219
|
|
|
$this->assertSame($this->builder, $this->builder->setPage($page = 1)); |
|
220
|
|
|
$this->assertSame($page, $this->builder->createDataSource()->getCurrentPage()); |
|
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Repository |
|
225
|
|
|
*/ |
|
226
|
|
|
private function createRepositoryMock() |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->getMockBuilder(Repository::class) |
|
229
|
|
|
->disableOriginalConstructor() |
|
230
|
|
|
->getMock(); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Builder |
|
235
|
|
|
*/ |
|
236
|
|
|
private function createQueryBuilderMock() |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->getMockBuilder(Builder::class) |
|
239
|
|
|
->disableOriginalConstructor() |
|
240
|
|
|
->disableOriginalClone() |
|
241
|
|
|
->getMock(); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Query |
|
246
|
|
|
*/ |
|
247
|
|
|
private function createQueryMock() |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->getMockBuilder(Query::class) |
|
250
|
|
|
->disableOriginalConstructor() |
|
251
|
|
|
->getMock(); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Iterator |
|
256
|
|
|
*/ |
|
257
|
|
|
private function createIteratorMock() |
|
258
|
|
|
{ |
|
259
|
|
|
return $this->getMock(Iterator::class); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
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: