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