1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2015 Michael Giesler |
4
|
|
|
* |
5
|
|
|
* This file is part of Dembelo. |
6
|
|
|
* |
7
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License 3 for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
18
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @package AdminBundle |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace AdminBundle\Tests\Controller; |
27
|
|
|
|
28
|
|
|
use DembeloMain\Document\Importfile; |
29
|
|
|
use DembeloMain\Document\Licensee; |
30
|
|
|
use DembeloMain\Document\Textnode; |
31
|
|
|
use DembeloMain\Document\User; |
32
|
|
|
use DembeloMain\Model\Repository\Doctrine\ODM\ImportfileRepository; |
33
|
|
|
use DembeloMain\Model\Repository\Doctrine\ODM\LicenseeRepository; |
34
|
|
|
use DembeloMain\Model\Repository\Doctrine\ODM\TextNodeRepository; |
35
|
|
|
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry; |
36
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository; |
37
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
38
|
|
|
use AdminBundle\Controller\DefaultController; |
39
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
40
|
|
|
use Symfony\Component\HttpFoundation\Request; |
41
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
42
|
|
|
use Symfony\Component\HttpFoundation\Response; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Class DefaultControllerTest |
46
|
|
|
*/ |
47
|
|
|
class DefaultControllerTest extends WebTestCase |
48
|
|
|
{ |
49
|
|
|
private $container; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|DocumentRepository |
53
|
|
|
*/ |
54
|
|
|
private $repository; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ManagerRegistry |
58
|
|
|
*/ |
59
|
|
|
private $service; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var DefaultController |
63
|
|
|
*/ |
64
|
|
|
private $controller; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $twineDirectory; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function setUp(): void |
75
|
|
|
{ |
76
|
|
|
$this->twineDirectory = '/tmp/twineDirectory'; |
77
|
|
|
$this->controller = new DefaultController($this->twineDirectory); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* tests the index action |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function testIndexAction(): void |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$client = static::createClient(); |
87
|
|
|
$crawler = $client->request('GET', '/admin/'); |
88
|
|
|
|
89
|
|
|
$this->assertEquals(302, $client->getResponse()->getStatusCode()); |
90
|
|
|
$this->assertTrue($crawler->filter('html:contains("login")')->count() > 0); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* tests controller's userAction with no users in db |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function testUserAction(): void |
98
|
|
|
{ |
99
|
|
|
$request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); |
100
|
|
|
$postMock = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->getMock(); |
101
|
|
|
$queryMock = $this->getMockBuilder('foobar')->setMethods(array('execute', 'getQuery'))->getMock(); |
102
|
|
|
$postArray = array(); |
103
|
|
|
$postMock->expects($this->once()) |
104
|
|
|
->method('get') |
105
|
|
|
->will($this->returnValue($postArray)); |
106
|
|
|
$request->query = $postMock; |
|
|
|
|
107
|
|
|
|
108
|
|
|
$queryMock->expects($this->once()) |
109
|
|
|
->method('getQuery') |
110
|
|
|
->will($this->returnSelf()); |
111
|
|
|
|
112
|
|
|
$queryMock->expects($this->once()) |
113
|
|
|
->method('execute') |
114
|
|
|
->will($this->returnValue(array())); |
115
|
|
|
|
116
|
|
|
$this->loadMongoContainer('user'); |
117
|
|
|
$this->repository->expects($this->once()) |
118
|
|
|
->method('createQueryBuilder') |
119
|
|
|
->will($this->returnValue($queryMock)); |
120
|
|
|
|
121
|
|
|
$this->controller->setContainer($this->container); |
122
|
|
|
|
123
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
124
|
|
|
$response = $this->controller->usersAction($request); |
125
|
|
|
$this->assertInstanceOf(Response::class, $response); |
126
|
|
|
$this->assertJsonStringEqualsJsonString('[]', $response->getContent()); |
127
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* tests controller's userAction with two users in db |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function testUserActionWithUsers(): void |
135
|
|
|
{ |
136
|
|
|
$request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); |
137
|
|
|
$postMock = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->getMock(); |
138
|
|
|
$queryMock = $this->getMockBuilder('foobar')->setMethods(['execute', 'getQuery'])->getMock(); |
139
|
|
|
$postArray = array(); |
140
|
|
|
$postMock->expects($this->once()) |
141
|
|
|
->method('get') |
142
|
|
|
->will($this->returnValue($postArray)); |
143
|
|
|
$request->query = $postMock; |
|
|
|
|
144
|
|
|
|
145
|
|
|
$queryMock->expects($this->once()) |
146
|
|
|
->method('getQuery') |
147
|
|
|
->will($this->returnSelf()); |
148
|
|
|
|
149
|
|
|
$this->loadMongoContainer('user'); |
150
|
|
|
|
151
|
|
|
$this->repository->expects($this->once()) |
152
|
|
|
->method('createQueryBuilder') |
153
|
|
|
->will($this->returnValue($queryMock)); |
154
|
|
|
|
155
|
|
|
$user1 = new User(); |
156
|
|
|
$user1->setEmail('email1'); |
157
|
|
|
$user1->setId('id1'); |
158
|
|
|
$user1->setRoles('ROLE_ADMIN'); |
159
|
|
|
$user1->setLicenseeId('lic1'); |
160
|
|
|
$user2 = new User(); |
161
|
|
|
$user2->setEmail('email2'); |
162
|
|
|
$user2->setId('id2'); |
163
|
|
|
$user2->setRoles('ROLE_USER'); |
164
|
|
|
$user2->setLicenseeId('lic2'); |
165
|
|
|
|
166
|
|
|
$userArray = array( |
167
|
|
|
$user1, |
168
|
|
|
$user2, |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
$queryMock->expects($this->once()) |
172
|
|
|
->method('execute') |
173
|
|
|
->will($this->returnValue($userArray)); |
174
|
|
|
|
175
|
|
|
$this->controller->setContainer($this->container); |
176
|
|
|
|
177
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
178
|
|
|
$response = $this->controller->usersAction($request); |
179
|
|
|
$this->assertInstanceOf(Response::class, $response); |
180
|
|
|
$this->assertJsonStringEqualsJsonString('[{"id":"id1","gender":null,"email":"email1","roles":"ROLE_ADMIN","licenseeId":"lic1","status":null,"source":null,"reason":null,"created":"'.date('Y-m-d H:i:s', 0).'","updated":"'.date('Y-m-d H:i:s', 0).'"},{"id":"id2","email":"email2","roles":"ROLE_USER","licenseeId":"lic2","status":null,"source":null,"reason":null,"gender":null,"created":"'.date('Y-m-d H:i:s', 0).'","updated":"'.date('Y-m-d H:i:s', 0).'"}]', $response->getContent()); |
181
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* tear down method |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
public function tearDown(): void |
189
|
|
|
{ |
190
|
|
|
$this->container = null; |
191
|
|
|
$this->repository = null; |
192
|
|
|
$this->service = null; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* tests the formsaveAction without parameters |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
public function testFormsaveActionWithoutParameters(): void |
200
|
|
|
{ |
201
|
|
|
$request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); |
202
|
|
|
$postMock = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->getMock(); |
203
|
|
|
$postArray = array(); |
204
|
|
|
$postMock->expects($this->once()) |
205
|
|
|
->method('all') |
206
|
|
|
->will($this->returnValue($postArray)); |
207
|
|
|
$request->request = $postMock; |
|
|
|
|
208
|
|
|
|
209
|
|
|
$this->controller->setContainer($this->container); |
210
|
|
|
|
211
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
212
|
|
|
$response = $this->controller->formsaveAction($request); |
213
|
|
|
$this->assertInstanceOf(Response::class, $response); |
214
|
|
|
$json = $response->getContent(); |
215
|
|
|
$this->assertJson($json); |
216
|
|
|
$json = json_decode($json); |
217
|
|
|
$this->assertTrue($json->error); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* tests the formsaveAction with wrong parameters |
222
|
|
|
* @return void |
223
|
|
|
*/ |
224
|
|
View Code Duplication |
public function testFormsaveActionWithWrongParameters(): void |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
$request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); |
227
|
|
|
$postMock = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->getMock(); |
228
|
|
|
$postArray = array( |
229
|
|
|
'formtype' => 'nonexistant', |
230
|
|
|
); |
231
|
|
|
$postMock->expects($this->once()) |
232
|
|
|
->method('all') |
233
|
|
|
->will($this->returnValue($postArray)); |
234
|
|
|
$request->request = $postMock; |
|
|
|
|
235
|
|
|
|
236
|
|
|
$this->controller->setContainer($this->container); |
237
|
|
|
|
238
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
239
|
|
|
$response = $this->controller->formsaveAction($request); |
240
|
|
|
$this->assertInstanceOf(Response::class, $response); |
241
|
|
|
$json = $response->getContent(); |
242
|
|
|
$this->assertJson($json); |
243
|
|
|
$json = json_decode($json); |
244
|
|
|
$this->assertTrue($json->error); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* tests the formsaveAction with an existing user |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
|
public function testFormsaveActionExistingUser(): void |
252
|
|
|
{ |
253
|
|
|
$user = new User(); |
254
|
|
|
$user->setId('someId'); |
255
|
|
|
$user->setEmail('[email protected]'); |
256
|
|
|
$user->setRoles('ROLE_USER'); |
257
|
|
|
|
258
|
|
|
$this->loadMongoContainer('user'); |
259
|
|
|
$request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); |
260
|
|
|
$postMock = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->getMock(); |
261
|
|
|
$postArray = array( |
262
|
|
|
'formtype' => 'user', |
263
|
|
|
'id' => $user->getId(), |
264
|
|
|
); |
265
|
|
|
$postMock->expects($this->once()) |
266
|
|
|
->method('all') |
267
|
|
|
->will($this->returnValue($postArray)); |
268
|
|
|
$request->request = $postMock; |
|
|
|
|
269
|
|
|
|
270
|
|
|
$this->repository->expects($this->once()) |
271
|
|
|
->method('find') |
272
|
|
|
->with($user->getId()) |
273
|
|
|
->will($this->returnValue($user)); |
274
|
|
|
|
275
|
|
|
$this->controller->setContainer($this->container); |
276
|
|
|
|
277
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
278
|
|
|
$response = $this->controller->formsaveAction($request); |
279
|
|
|
$this->assertInstanceOf(Response::class, $response); |
280
|
|
|
$json = $response->getContent(); |
281
|
|
|
$this->assertJson($json); |
282
|
|
|
$json = json_decode($json); |
283
|
|
|
$this->assertFalse($json->error); |
284
|
|
|
$this->assertEquals($user->getId(), $json->newId); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* tests the formsaveAction with a nonexisting user |
289
|
|
|
* @return void |
290
|
|
|
*/ |
291
|
|
|
public function testFormsaveActionNotExistingUser(): void |
292
|
|
|
{ |
293
|
|
|
$user = new User(); |
294
|
|
|
$user->setId('someId'); |
295
|
|
|
$user->setEmail('[email protected]'); |
296
|
|
|
$user->setRoles('ROLE_USER'); |
297
|
|
|
|
298
|
|
|
$this->loadMongoContainer('user'); |
299
|
|
|
$request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); |
300
|
|
|
$postMock = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->getMock(); |
301
|
|
|
$postArray = array( |
302
|
|
|
'formtype' => 'user', |
303
|
|
|
'id' => $user->getId(), |
304
|
|
|
); |
305
|
|
|
$postMock->expects($this->once()) |
306
|
|
|
->method('all') |
307
|
|
|
->will($this->returnValue($postArray)); |
308
|
|
|
$request->request = $postMock; |
|
|
|
|
309
|
|
|
|
310
|
|
|
$this->repository->expects($this->once()) |
311
|
|
|
->method('find') |
312
|
|
|
->with($user->getId()) |
313
|
|
|
->will($this->returnValue(null)); |
314
|
|
|
|
315
|
|
|
$this->controller->setContainer($this->container); |
316
|
|
|
|
317
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
318
|
|
|
$response = $this->controller->formsaveAction($request); |
319
|
|
|
$this->assertInstanceOf(Response::class, $response); |
320
|
|
|
$json = $response->getContent(); |
321
|
|
|
$this->assertJson($json); |
322
|
|
|
$json = json_decode($json); |
323
|
|
|
$this->assertTrue($json->error); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* tests the formsaveAction without admin permission |
328
|
|
|
* @return void |
329
|
|
|
*/ |
330
|
|
View Code Duplication |
public function testFormsaveActionWithoutAdminPermission(): void |
|
|
|
|
331
|
|
|
{ |
332
|
|
|
$client = static::createClient(); |
333
|
|
|
$crawler = $client->request('GET', '/admin/save'); |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
$this->assertEquals(302, $client->getResponse()->getStatusCode()); |
337
|
|
|
$this->assertTrue($crawler->filter('html:contains("login")')->count() > 0); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* tests the formsaveAction with missing id parameter |
342
|
|
|
* @return void |
343
|
|
|
*/ |
344
|
|
View Code Duplication |
public function testFormsaveActionWithMissingIdParameter(): void |
|
|
|
|
345
|
|
|
{ |
346
|
|
|
$request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); |
347
|
|
|
$postMock = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->getMock(); |
348
|
|
|
$postArray = array( |
349
|
|
|
'formtype' => 'licensee', |
350
|
|
|
'name' => 'someLNName', |
351
|
|
|
); |
352
|
|
|
$postMock->expects($this->once()) |
353
|
|
|
->method('all') |
354
|
|
|
->will($this->returnValue($postArray)); |
355
|
|
|
$request->request = $postMock; |
|
|
|
|
356
|
|
|
|
357
|
|
|
$this->controller->setContainer($this->container); |
358
|
|
|
|
359
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
360
|
|
|
$response = $this->controller->formsaveAction($request); |
361
|
|
|
$this->assertInstanceOf(Response::class, $response); |
362
|
|
|
$json = $response->getContent(); |
363
|
|
|
$this->assertJson($json); |
364
|
|
|
$json = json_decode($json); |
365
|
|
|
$this->assertTrue($json->error); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* tests licenseeAction with no licensees |
370
|
|
|
* @return void |
371
|
|
|
*/ |
372
|
|
|
public function testLicenseeActionWithNoLicensees(): void |
373
|
|
|
{ |
374
|
|
|
$repository = $this->getMockBuilder(LicenseeRepository::class)->disableOriginalConstructor()->setMethods(['findAll'])->getMock(); |
375
|
|
|
$repository->expects($this->once()) |
376
|
|
|
->method('findAll') |
377
|
|
|
->willReturn([]); |
378
|
|
|
|
379
|
|
|
$container = $this->getMockBuilder(ContainerInterface::class)->getMock(); |
380
|
|
|
$container->expects($this->once()) |
381
|
|
|
->method('get') |
382
|
|
|
->with('app.model_repository_licensee') |
|
|
|
|
383
|
|
|
->willReturn($repository); |
384
|
|
|
|
385
|
|
|
$this->controller->setContainer($container); |
386
|
|
|
|
387
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
388
|
|
|
$response = $this->controller->licenseesAction(); |
389
|
|
|
$this->assertInstanceOf(Response::class, $response); |
390
|
|
|
$this->assertJsonStringEqualsJsonString('[]', $response->getContent()); |
391
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* tests licenseeAction() with one licensee |
396
|
|
|
* @return void |
397
|
|
|
*/ |
398
|
|
|
public function testLicenseeActionWithOneLicensee(): void |
399
|
|
|
{ |
400
|
|
|
$licensee = new Licensee(); |
401
|
|
|
$licensee->setName('someName'); |
402
|
|
|
$licensee->setId('someId'); |
403
|
|
|
|
404
|
|
|
$repository = $this->getMockBuilder(LicenseeRepository::class)->disableOriginalConstructor()->setMethods(['findAll'])->getMock(); |
405
|
|
|
$repository->expects($this->once()) |
406
|
|
|
->method('findAll') |
407
|
|
|
->willReturn([$licensee]); |
408
|
|
|
|
409
|
|
|
$container = $this->getMockBuilder(ContainerInterface::class)->getMock(); |
410
|
|
|
$container->expects($this->once()) |
411
|
|
|
->method('get') |
412
|
|
|
->with('app.model_repository_licensee') |
|
|
|
|
413
|
|
|
->willReturn($repository); |
414
|
|
|
|
415
|
|
|
$this->controller->setContainer($container); |
416
|
|
|
|
417
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
418
|
|
|
$response = $this->controller->licenseesAction(); |
419
|
|
|
$this->assertInstanceOf(Response::class, $response); |
420
|
|
|
$this->assertJsonStringEqualsJsonString('[{"id":"someId","name":"someName"}]', $response->getContent()); |
421
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* tests textnode action |
426
|
|
|
* @return void |
427
|
|
|
*/ |
428
|
|
|
public function testTextnodesAction(): void |
429
|
|
|
{ |
430
|
|
|
$textnode = new Textnode(); |
431
|
|
|
$textnode->setId('someId'); |
432
|
|
|
$textnode->setCreated(new \DateTime('2017-01-01 12:00:00')); |
|
|
|
|
433
|
|
|
$textnode->setStatus(1); |
434
|
|
|
$textnode->setAccess(true); |
435
|
|
|
$textnode->setLicenseeId('someLicenseeId'); |
436
|
|
|
$textnode->setArbitraryId('someArbitraryId'); |
437
|
|
|
$textnode->setTwineId('someTwineId'); |
438
|
|
|
$textnode->setMetadata(['key1' => 'val1', 'key2' => 'val2']); |
439
|
|
|
|
440
|
|
|
$licensee = new Licensee(); |
441
|
|
|
$licensee->setId('someLicenseeId'); |
442
|
|
|
$licensee->setName('someLicenseeName'); |
443
|
|
|
|
444
|
|
|
$importfile = new Importfile(); |
445
|
|
|
$importfile->setId('someImportfileId'); |
446
|
|
|
$importfile->setName('someImportfileName'); |
447
|
|
|
|
448
|
|
|
$repository = $this->getMockBuilder(TextNodeRepository::class) |
449
|
|
|
->disableOriginalConstructor() |
450
|
|
|
->setMethods(['findAll']) |
451
|
|
|
->getMock(); |
452
|
|
|
|
453
|
|
|
$repository->expects($this->once()) |
454
|
|
|
->method('findAll') |
455
|
|
|
->willReturn([$textnode]); |
456
|
|
|
|
457
|
|
|
$licenseeRepository = $this->getLicenseeRepositoryMock(); |
458
|
|
|
$licenseeRepository->expects($this->once()) |
|
|
|
|
459
|
|
|
->method('findAll') |
460
|
|
|
->willReturn([$licensee]); |
461
|
|
|
|
462
|
|
|
$importfileRepository = $this->getImportfileRepositoryMock(); |
463
|
|
|
$importfileRepository->expects($this->once()) |
|
|
|
|
464
|
|
|
->method('findAll') |
465
|
|
|
->willReturn([$importfile]); |
466
|
|
|
|
467
|
|
|
|
468
|
|
|
$container = $this->getMockBuilder(ContainerInterface::class)->getMock(); |
469
|
|
|
$container->expects($this->at(0)) |
470
|
|
|
->method('get') |
471
|
|
|
->with('app.model_repository_textNode') |
|
|
|
|
472
|
|
|
->willReturn($repository); |
473
|
|
|
$container->expects($this->at(1)) |
474
|
|
|
->method('get') |
475
|
|
|
->with('app.model_repository_licensee') |
476
|
|
|
->willReturn($licenseeRepository); |
477
|
|
|
$container->expects($this->at(2)) |
478
|
|
|
->method('get') |
479
|
|
|
->with('app.model_repository_importfile') |
480
|
|
|
->willReturn($importfileRepository); |
481
|
|
|
|
482
|
|
|
$this->controller->setContainer($container); |
483
|
|
|
|
484
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
485
|
|
|
$response = $this->controller->textnodesAction(); |
486
|
|
|
$this->assertInstanceOf(Response::class, $response); |
487
|
|
|
$expectedJson = '[{"id":"someId","status":"aktiv","created":"01.01.2017, 12:00:00",'; |
488
|
|
|
$expectedJson .= '"access":"ja","licensee":"someLicenseeName","importfile":"unbekannt","beginning":"...",'; |
489
|
|
|
$expectedJson .= '"financenode":"ja","arbitraryId":"someArbitraryId","twineId":"someTwineId",'; |
490
|
|
|
$expectedJson .= '"metadata":"key1: val1\nkey2: val2\n"}]'; |
491
|
|
|
$this->assertJsonStringEqualsJsonString($expectedJson, $response->getContent()); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|LicenseeRepository |
496
|
|
|
*/ |
497
|
|
|
private function getLicenseeRepositoryMock(): LicenseeRepository |
498
|
|
|
{ |
499
|
|
|
$repository = $this->getMockBuilder(LicenseeRepository::class) |
500
|
|
|
->disableOriginalConstructor() |
501
|
|
|
->setMethods(['findAll']) |
502
|
|
|
->getMock(); |
503
|
|
|
|
504
|
|
|
return $repository; |
|
|
|
|
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ImportfileRepository |
509
|
|
|
*/ |
510
|
|
|
private function getImportfileRepositoryMock(): ImportfileRepository |
511
|
|
|
{ |
512
|
|
|
$repository = $this->getMockBuilder(ImportfileRepository::class) |
513
|
|
|
->disableOriginalConstructor() |
514
|
|
|
->setMethods(['findAll']) |
515
|
|
|
->getMock(); |
516
|
|
|
|
517
|
|
|
return $repository; |
|
|
|
|
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* load mockoed container and mocked mongodb repository |
522
|
|
|
* @return void |
523
|
|
|
*/ |
524
|
|
|
private function loadMongoContainer($repository): void |
525
|
|
|
{ |
526
|
|
|
$this->loadMockedContainer(); |
527
|
|
|
$this->loadMockedMongoRepository($repository); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* load mocked container |
532
|
|
|
* @return void |
533
|
|
|
*/ |
534
|
|
|
private function loadMockedContainer(): void |
535
|
|
|
{ |
536
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* load mocked mongodb repository |
541
|
|
|
* @return void |
542
|
|
|
*/ |
543
|
|
|
private function loadMockedMongoRepository($repository): void |
544
|
|
|
{ |
545
|
|
|
$this->service = $this->getMockBuilder(ManagerRegistry::class)->disableOriginalConstructor()->getMock(); |
546
|
|
|
$this->repository = $this->getMockBuilder(DocumentRepository::class)->disableOriginalConstructor()->getMock(); |
547
|
|
|
$this->container->expects($this->once()) |
548
|
|
|
->method('get') |
549
|
|
|
->with($this->equalTo('app.model_repository_'.$repository)) |
550
|
|
|
->will($this->returnValue($this->repository)); |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
|
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.