1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Tests\Bridge\Doctrine\ORM\DataProvider; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\ORM\AbstractQuery; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Doctrine\ORM\EntityRepository; |
9
|
|
|
use Doctrine\ORM\QueryBuilder; |
10
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
11
|
|
|
use LAG\AdminBundle\Bridge\Doctrine\ORM\DataProvider\ORMDataProvider; |
12
|
|
|
use LAG\AdminBundle\Configuration\AdminConfiguration; |
13
|
|
|
use LAG\AdminBundle\Event\AdminEvents; |
14
|
|
|
use LAG\AdminBundle\Event\DoctrineOrmFilterEvent; |
15
|
|
|
use LAG\AdminBundle\Exception\Exception; |
16
|
|
|
use LAG\AdminBundle\Tests\AdminTestBase; |
17
|
|
|
use LAG\AdminBundle\Tests\Fixtures\EntityFixture; |
18
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
21
|
|
|
|
22
|
|
|
class ORMDataProviderTest extends AdminTestBase |
23
|
|
|
{ |
24
|
|
|
public function testGetCollection() |
25
|
|
|
{ |
26
|
|
|
// Create an admin configuration to test the collection method |
27
|
|
|
$configuration = $this->createMock(AdminConfiguration::class); |
28
|
|
|
$configuration |
29
|
|
|
->expects($this->atLeastOnce()) |
30
|
|
|
->method('getParameter') |
31
|
|
|
->willReturnMap([ |
32
|
|
|
['entity', 'MyClass'], |
33
|
|
|
['pager', null], |
34
|
|
|
['page_parameter', 'page'], |
35
|
|
|
]) |
36
|
|
|
; |
37
|
|
|
|
38
|
|
|
/** @var AdminInterface|MockObject $admin */ |
39
|
|
|
$admin = $this->createMock(AdminInterface::class); |
40
|
|
|
$admin |
|
|
|
|
41
|
|
|
->expects($this->atLeastOnce()) |
42
|
|
|
->method('getConfiguration') |
43
|
|
|
->willReturn($configuration) |
44
|
|
|
; |
45
|
|
|
|
46
|
|
|
// Create fake entities to return |
47
|
|
|
$entities = [ |
48
|
|
|
new EntityFixture(uniqid()), |
49
|
|
|
new EntityFixture(uniqid()), |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
// The query should return entities |
53
|
|
|
$query = $this->createMock(AbstractQuery::class); |
54
|
|
|
$query |
55
|
|
|
->expects($this->once()) |
56
|
|
|
->method('getResult') |
57
|
|
|
->willReturn($entities) |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
// The query builder should call the getQuery() method |
61
|
|
|
$queryBuilder = $this->createMock(QueryBuilder::class); |
62
|
|
|
$queryBuilder |
63
|
|
|
->expects($this->once()) |
64
|
|
|
->method('getQuery') |
65
|
|
|
->willReturn($query) |
66
|
|
|
; |
67
|
|
|
|
68
|
|
|
// The repository should return a query builder |
69
|
|
|
$repository = $this->createMock(EntityRepository::class); |
70
|
|
|
$repository |
71
|
|
|
->expects($this->once()) |
72
|
|
|
->method('createQueryBuilder') |
73
|
|
|
->with('entity') |
74
|
|
|
->willReturn($queryBuilder) |
75
|
|
|
; |
76
|
|
|
|
77
|
|
|
// The entity manager should return a repository |
78
|
|
|
/** @var EntityManagerInterface|MockObject $entityManager */ |
79
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
80
|
|
|
$entityManager |
|
|
|
|
81
|
|
|
->expects($this->once()) |
82
|
|
|
->method('getRepository') |
83
|
|
|
->willReturn($repository) |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
// The event dispatcher should dispatched once a ORM_FILTER event |
87
|
|
|
/** @var EventDispatcherInterface|MockObject $eventDispatcher */ |
88
|
|
|
$eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
89
|
|
|
$eventDispatcher |
|
|
|
|
90
|
|
|
->expects($this->once()) |
91
|
|
|
->method('dispatch') |
92
|
|
|
->willReturnCallback(function ($eventName, $event) use ($queryBuilder, $admin) { |
93
|
|
|
$this->assertEquals(AdminEvents::DOCTRINE_ORM_FILTER, $eventName); |
94
|
|
|
/** @var DoctrineOrmFilterEvent $event */ |
95
|
|
|
$this->assertInstanceOf(DoctrineOrmFilterEvent::class, $event); |
96
|
|
|
$this->assertEquals($queryBuilder, $event->getQueryBuilder()); |
97
|
|
|
$this->assertEquals($admin, $event->getAdmin()); |
98
|
|
|
$this->assertEquals([], $event->getFilters()); |
99
|
|
|
}) |
100
|
|
|
; |
101
|
|
|
/** @var RequestStack|MockObject $requestStack */ |
102
|
|
|
$requestStack = $this->createMock(RequestStack::class); |
103
|
|
|
|
104
|
|
|
$provider = new ORMDataProvider( |
105
|
|
|
$entityManager, |
|
|
|
|
106
|
|
|
$eventDispatcher, |
|
|
|
|
107
|
|
|
$requestStack |
|
|
|
|
108
|
|
|
); |
109
|
|
|
$returnedEntities = $provider->getCollection($admin); |
|
|
|
|
110
|
|
|
|
111
|
|
|
// The returned entities should be the same as those provided by the repository |
112
|
|
|
$this->assertEquals($entities, $returnedEntities); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGetItem() |
116
|
|
|
{ |
117
|
|
|
/** @var EventDispatcherInterface|MockObject $eventDispatcher */ |
118
|
|
|
$eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
119
|
|
|
|
120
|
|
|
/** @var RequestStack|MockObject $requestStack */ |
121
|
|
|
$requestStack = $this->createMock(RequestStack::class); |
122
|
|
|
|
123
|
|
|
$repository = $this->createMock(EntityRepository::class); |
124
|
|
|
$repository |
125
|
|
|
->expects($this->atLeastOnce()) |
126
|
|
|
->method('find') |
127
|
|
|
->with(42) |
128
|
|
|
->willReturn(new EntityFixture(42)) |
129
|
|
|
; |
130
|
|
|
|
131
|
|
|
/** @var EntityManagerInterface|MockObject $entityManager */ |
132
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
133
|
|
|
$entityManager |
|
|
|
|
134
|
|
|
->expects($this->atLeastOnce()) |
135
|
|
|
->method('getRepository') |
136
|
|
|
->with('MyClass') |
137
|
|
|
->willReturn($repository) |
138
|
|
|
; |
139
|
|
|
|
140
|
|
|
$configuration = $this->createMock(AdminConfiguration::class); |
141
|
|
|
$configuration |
142
|
|
|
->expects($this->atLeastOnce()) |
143
|
|
|
->method('getParameter') |
144
|
|
|
->willReturnMap([ |
145
|
|
|
['entity', 'MyClass'], |
146
|
|
|
['pager', null], |
147
|
|
|
['page_parameter', 'page'], |
148
|
|
|
]) |
149
|
|
|
; |
150
|
|
|
|
151
|
|
|
/** @var AdminInterface|MockObject $admin */ |
152
|
|
|
$admin = $this->createMock(AdminInterface::class); |
153
|
|
|
$admin |
|
|
|
|
154
|
|
|
->expects($this->atLeastOnce()) |
155
|
|
|
->method('getConfiguration') |
156
|
|
|
->willReturn($configuration) |
157
|
|
|
; |
158
|
|
|
|
159
|
|
|
$provider = new ORMDataProvider( |
160
|
|
|
$entityManager, |
|
|
|
|
161
|
|
|
$eventDispatcher, |
|
|
|
|
162
|
|
|
$requestStack |
|
|
|
|
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$item = $provider->getItem($admin, 42); |
|
|
|
|
166
|
|
|
|
167
|
|
|
$this->assertEquals(42, $item->getId()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testGetItemWithException() |
171
|
|
|
{ |
172
|
|
|
/** @var EventDispatcherInterface|MockObject $eventDispatcher */ |
173
|
|
|
$eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
174
|
|
|
|
175
|
|
|
/** @var RequestStack|MockObject $requestStack */ |
176
|
|
|
$requestStack = $this->createMock(RequestStack::class); |
177
|
|
|
|
178
|
|
|
$repository = $this->createMock(EntityRepository::class); |
179
|
|
|
$repository |
180
|
|
|
->expects($this->atLeastOnce()) |
181
|
|
|
->method('find') |
182
|
|
|
->with(42) |
183
|
|
|
->willReturn(null) |
184
|
|
|
; |
185
|
|
|
|
186
|
|
|
/** @var EntityManagerInterface|MockObject $entityManager */ |
187
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
188
|
|
|
$entityManager |
|
|
|
|
189
|
|
|
->expects($this->atLeastOnce()) |
190
|
|
|
->method('getRepository') |
191
|
|
|
->with('MyClass') |
192
|
|
|
->willReturn($repository) |
193
|
|
|
; |
194
|
|
|
|
195
|
|
|
$configuration = $this->createMock(AdminConfiguration::class); |
196
|
|
|
$configuration |
197
|
|
|
->expects($this->atLeastOnce()) |
198
|
|
|
->method('getParameter') |
199
|
|
|
->willReturnMap([ |
200
|
|
|
['entity', 'MyClass'], |
201
|
|
|
['pager', null], |
202
|
|
|
['page_parameter', 'page'], |
203
|
|
|
]) |
204
|
|
|
; |
205
|
|
|
|
206
|
|
|
/** @var AdminInterface|MockObject $admin */ |
207
|
|
|
$admin = $this->createMock(AdminInterface::class); |
208
|
|
|
$admin |
|
|
|
|
209
|
|
|
->expects($this->atLeastOnce()) |
210
|
|
|
->method('getConfiguration') |
211
|
|
|
->willReturn($configuration) |
212
|
|
|
; |
213
|
|
|
|
214
|
|
|
$provider = new ORMDataProvider( |
215
|
|
|
$entityManager, |
|
|
|
|
216
|
|
|
$eventDispatcher, |
|
|
|
|
217
|
|
|
$requestStack |
|
|
|
|
218
|
|
|
); |
219
|
|
|
|
220
|
|
|
$this->assertExceptionRaised(Exception::class, function () use ($provider, $admin) { |
221
|
|
|
$provider->getItem($admin, 42); |
|
|
|
|
222
|
|
|
}); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testSaveItem() |
226
|
|
|
{ |
227
|
|
|
/** @var EventDispatcherInterface|MockObject $eventDispatcher */ |
228
|
|
|
$eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
229
|
|
|
|
230
|
|
|
/** @var RequestStack|MockObject $requestStack */ |
231
|
|
|
$requestStack = $this->createMock(RequestStack::class); |
232
|
|
|
|
233
|
|
|
$entity =new EntityFixture(42); |
234
|
|
|
|
235
|
|
|
/** @var EntityManagerInterface|MockObject $entityManager */ |
236
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
237
|
|
|
$entityManager |
|
|
|
|
238
|
|
|
->expects($this->atLeastOnce()) |
239
|
|
|
->method('persist') |
240
|
|
|
->with($entity) |
241
|
|
|
; |
242
|
|
|
$entityManager |
243
|
|
|
->expects($this->atLeastOnce()) |
244
|
|
|
->method('flush') |
245
|
|
|
; |
246
|
|
|
|
247
|
|
|
/** @var AdminInterface|MockObject $admin */ |
248
|
|
|
$admin = $this->createMock(AdminInterface::class); |
249
|
|
|
$admin |
|
|
|
|
250
|
|
|
->expects($this->atLeastOnce()) |
251
|
|
|
->method('getEntities') |
252
|
|
|
->willReturn(new ArrayCollection([ |
253
|
|
|
$entity, |
254
|
|
|
])) |
255
|
|
|
; |
256
|
|
|
|
257
|
|
|
$provider = new ORMDataProvider( |
258
|
|
|
$entityManager, |
|
|
|
|
259
|
|
|
$eventDispatcher, |
|
|
|
|
260
|
|
|
$requestStack |
|
|
|
|
261
|
|
|
); |
262
|
|
|
|
263
|
|
|
$provider->saveItem($admin); |
|
|
|
|
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
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: