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