1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use ScayTrase\Api\Cruds\Tests\AbstractCrudsWebTest; |
6
|
|
|
use ScayTrase\Api\Cruds\Tests\Fixtures\Common\Entity\MyEntity; |
7
|
|
|
|
8
|
|
|
class ReadControllerTest extends AbstractCrudsWebTest |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @dataProvider getKernelClasses |
12
|
|
|
* |
13
|
|
|
* @param $kernel |
14
|
|
|
*/ |
15
|
|
|
public function testSearchAction($kernel) |
16
|
|
|
{ |
17
|
|
|
self::createAndBootKernel($kernel); |
18
|
|
|
self::configureDb(); |
19
|
|
|
|
20
|
|
|
$em = self::getEntityManager(); |
21
|
|
|
$entity = new MyEntity('my-test-secret'); |
22
|
|
|
$em->persist($entity); |
23
|
|
|
$parent = new MyEntity('non-recursing-entity'); |
24
|
|
|
$em->persist($parent); |
25
|
|
|
$entity->setParent($parent); |
26
|
|
|
$em->flush(); |
27
|
|
|
$em->clear(); |
28
|
|
|
|
29
|
|
|
$client = self::createClient(); |
30
|
|
|
$client->request( |
31
|
|
|
'GET', |
32
|
|
|
'/api/entity/my-entity/read', |
33
|
|
|
['identifier' => $entity->getId()], |
34
|
|
|
[], |
35
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json'] |
36
|
|
|
); |
37
|
|
|
$response = $client->getResponse(); |
38
|
|
|
|
39
|
|
|
self::assertTrue($response->isSuccessful()); |
40
|
|
|
$data = json_decode($response->getContent()); |
41
|
|
|
|
42
|
|
|
self::assertEquals(JSON_ERROR_NONE, json_last_error()); |
43
|
|
|
|
44
|
|
|
self::assertInstanceOf(\stdClass::class, $data); |
45
|
|
|
self::assertSame($entity->getId(), $data->id); |
46
|
|
|
self::assertSame('defaults', $data->public_api_field); |
47
|
|
|
self::assertObjectNotHasAttribute('private_field', $data); |
48
|
|
|
self::assertSame($parent->getId(), $data->parent); |
49
|
|
|
self::assertSame([], $data->children); |
50
|
|
|
|
51
|
|
|
$client->request( |
52
|
|
|
'GET', |
53
|
|
|
'/api/entity/my-entity/read', |
54
|
|
|
['identifier' => $parent->getId()], |
55
|
|
|
[], |
56
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json'] |
57
|
|
|
); |
58
|
|
|
$response = $client->getResponse(); |
59
|
|
|
|
60
|
|
|
self::assertTrue($response->isSuccessful()); |
61
|
|
|
$data = json_decode($response->getContent()); |
62
|
|
|
|
63
|
|
|
self::assertEquals(JSON_ERROR_NONE, json_last_error()); |
64
|
|
|
|
65
|
|
|
self::assertInstanceOf(\stdClass::class, $data); |
66
|
|
|
self::assertSame($parent->getId(), $data->id); |
67
|
|
|
self::assertSame('defaults', $data->public_api_field); |
68
|
|
|
self::assertObjectNotHasAttribute('private_field', $data); |
69
|
|
|
self::assertNull($data->parent); |
70
|
|
|
self::assertSame([$entity->getId()], $data->children); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|