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 CreateControllerTest extends AbstractCrudsWebTest |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @dataProvider getKernelClasses |
12
|
|
|
* |
13
|
|
|
* @param $kernel |
14
|
|
|
*/ |
15
|
|
|
public function testCreateAction($kernel) |
16
|
|
|
{ |
17
|
|
|
self::createAndBootKernel($kernel); |
18
|
|
|
self::configureDb(); |
19
|
|
|
|
20
|
|
|
$client = self::createClient(); |
21
|
|
|
$client->request( |
22
|
|
|
'POST', |
23
|
|
|
'/api/entity/my-entity/create', |
24
|
|
|
[ |
25
|
|
|
'data' => [ |
26
|
|
|
'public_api_field' => 'my-data', |
27
|
|
|
], |
28
|
|
|
], |
29
|
|
|
[], |
30
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json'] |
31
|
|
|
); |
32
|
|
|
$response = $client->getResponse(); |
33
|
|
|
|
34
|
|
|
self::assertTrue($response->isSuccessful()); |
35
|
|
|
|
36
|
|
|
$em = self::getEntityManager(); |
37
|
|
|
$parent = $em->getRepository(MyEntity::class)->findOneBy(['publicApiField' => 'my-data']); |
38
|
|
|
self::assertNotNull($parent); |
39
|
|
|
|
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($parent->getId(), $data->id); |
46
|
|
|
self::assertSame('my-data', $data->public_api_field); |
47
|
|
|
self::assertObjectNotHasAttribute('private_field', $data); |
48
|
|
|
self::assertNull($data->parent); |
49
|
|
|
self::assertSame([], $data->children); |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
$client->request( |
53
|
|
|
'POST', |
54
|
|
|
'/api/entity/my-entity/create', |
55
|
|
|
[ |
56
|
|
|
'data' => [ |
57
|
|
|
'public_api_field' => 'my-child-data', |
58
|
|
|
'parent' => $parent->getId(), |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
[], |
62
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json'] |
63
|
|
|
); |
64
|
|
|
$response = $client->getResponse(); |
65
|
|
|
|
66
|
|
|
self::assertTrue($response->isSuccessful()); |
67
|
|
|
$entity = $em->getRepository(MyEntity::class)->findOneBy(['publicApiField' => 'my-child-data']); |
68
|
|
|
self::assertNotNull($parent); |
69
|
|
|
|
70
|
|
|
$data = json_decode($response->getContent()); |
71
|
|
|
|
72
|
|
|
self::assertEquals(JSON_ERROR_NONE, json_last_error()); |
73
|
|
|
|
74
|
|
|
self::assertInstanceOf(\stdClass::class, $data); |
75
|
|
|
self::assertSame($entity->getId(), $data->id); |
76
|
|
|
self::assertSame('my-child-data', $data->public_api_field); |
77
|
|
|
self::assertObjectNotHasAttribute('private_field', $data); |
78
|
|
|
self::assertSame($parent->getId(), $data->parent); |
79
|
|
|
self::assertSame([], $data->children); |
80
|
|
|
|
81
|
|
|
$em->clear(); |
82
|
|
|
$entity = $em->getRepository(MyEntity::class)->findOneBy(['publicApiField' => 'my-child-data']); |
83
|
|
|
$parent = $em->getRepository(MyEntity::class)->findOneBy(['publicApiField' => 'my-data']); |
84
|
|
|
|
85
|
|
|
self::assertSame($parent, $entity->getParent()); |
86
|
|
|
self::assertContains($entity, $parent->getChildren()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|