1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace App\Bundle\Example\Tests\Service; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Import classes |
7
|
|
|
*/ |
8
|
|
|
use App\Exception\InvalidEntityException; |
9
|
|
|
use App\Tests\ContainerAwareTrait; |
10
|
|
|
use App\Tests\DatabaseSchemaToolTrait; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Sunrise\Http\Router\Exception\BadRequestException; |
13
|
|
|
use Sunrise\Http\Router\OpenApi\Test\OpenApiAssertKitTrait; |
14
|
|
|
use Sunrise\Http\ServerRequest\ServerRequestFactory; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* EntryCreateControllerTest |
18
|
|
|
*/ |
19
|
|
|
class EntryCreateControllerTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
use ContainerAwareTrait; |
22
|
|
|
use DatabaseSchemaToolTrait; |
23
|
|
|
use OpenApiAssertKitTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private const ROUTE_NAME = 'api_v1_entry_create'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return void |
32
|
|
|
* |
33
|
|
|
* @runInSeparateProcess |
34
|
|
|
*/ |
35
|
|
|
public function testCreate() : void |
36
|
|
|
{ |
37
|
|
|
$container = $this->getContainer(); |
38
|
|
|
$doctrine = $container->get('doctrine'); |
39
|
|
|
|
40
|
|
|
$entityManager = $doctrine->getManager('master'); |
41
|
|
|
$this->createDatabaseSchema($entityManager); |
42
|
|
|
|
43
|
|
|
$entryManager = $container->get('entryManager'); |
44
|
|
|
$this->assertSame(0, $entryManager->countAll()); |
45
|
|
|
|
46
|
|
|
$request = (new ServerRequestFactory) |
47
|
|
|
->createServerRequest('POST', '/api/v1/entry') |
48
|
|
|
->withHeader('Content-Type', 'application/json') |
49
|
|
|
->withParsedBody([ |
50
|
|
|
'name' => 'foo', |
51
|
|
|
'slug' => 'bar', |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
$route = $container->get('router')->getRoute(self::ROUTE_NAME); |
55
|
|
|
$response = $route->handle($request); |
56
|
|
|
|
57
|
|
|
$this->assertSame(201, $response->getStatusCode()); |
58
|
|
|
$this->assertResponseBodyMatchesDescription($route, $response); |
59
|
|
|
$this->assertSame(1, $entryManager->countAll()); |
60
|
|
|
|
61
|
|
|
$entries = $entryManager->getList(null, null); |
62
|
|
|
$this->assertSame('foo', $entries[0]->getName()); |
63
|
|
|
$this->assertSame('bar', $entries[0]->getSlug()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return void |
68
|
|
|
* |
69
|
|
|
* @runInSeparateProcess |
70
|
|
|
*/ |
71
|
|
|
public function testCreateWithEmptyName() : void |
72
|
|
|
{ |
73
|
|
|
$container = $this->getContainer(); |
74
|
|
|
$doctrine = $container->get('doctrine'); |
75
|
|
|
|
76
|
|
|
$entityManager = $doctrine->getManager('master'); |
77
|
|
|
$this->createDatabaseSchema($entityManager); |
78
|
|
|
|
79
|
|
|
$entryManager = $container->get('entryManager'); |
80
|
|
|
$this->assertSame(0, $entryManager->countAll()); |
81
|
|
|
|
82
|
|
|
$request = (new ServerRequestFactory) |
83
|
|
|
->createServerRequest('POST', '/api/v1/entry') |
84
|
|
|
->withHeader('Content-Type', 'application/json') |
85
|
|
|
->withParsedBody([ |
86
|
|
|
'name' => '', |
87
|
|
|
'slug' => 'bar', |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
$this->expectException(BadRequestException::class); |
91
|
|
|
|
92
|
|
|
$container->get('router') |
93
|
|
|
->getRoute(self::ROUTE_NAME) |
94
|
|
|
->handle($request); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return void |
99
|
|
|
* |
100
|
|
|
* @runInSeparateProcess |
101
|
|
|
*/ |
102
|
|
|
public function testCreateWithEmptySlug() : void |
103
|
|
|
{ |
104
|
|
|
$container = $this->getContainer(); |
105
|
|
|
$doctrine = $container->get('doctrine'); |
106
|
|
|
|
107
|
|
|
$entityManager = $doctrine->getManager('master'); |
108
|
|
|
$this->createDatabaseSchema($entityManager); |
109
|
|
|
|
110
|
|
|
$entryManager = $container->get('entryManager'); |
111
|
|
|
$this->assertSame(0, $entryManager->countAll()); |
112
|
|
|
|
113
|
|
|
$request = (new ServerRequestFactory) |
114
|
|
|
->createServerRequest('POST', '/api/v1/entry') |
115
|
|
|
->withHeader('Content-Type', 'application/json') |
116
|
|
|
->withParsedBody([ |
117
|
|
|
'name' => 'foo', |
118
|
|
|
'slug' => '', |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
$this->expectException(BadRequestException::class); |
122
|
|
|
|
123
|
|
|
$container->get('router') |
124
|
|
|
->getRoute(self::ROUTE_NAME) |
125
|
|
|
->handle($request); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return void |
130
|
|
|
* |
131
|
|
|
* @runInSeparateProcess |
132
|
|
|
*/ |
133
|
|
|
public function testCreateWithNotUniqueSlug() : void |
134
|
|
|
{ |
135
|
|
|
$container = $this->getContainer(); |
136
|
|
|
$doctrine = $container->get('doctrine'); |
137
|
|
|
|
138
|
|
|
$entityManager = $doctrine->getManager('master'); |
139
|
|
|
$this->createDatabaseSchema($entityManager); |
140
|
|
|
|
141
|
|
|
$entryManager = $container->get('entryManager'); |
142
|
|
|
$this->assertSame(0, $entryManager->countAll()); |
143
|
|
|
|
144
|
|
|
// reserving the `foo` slug... |
145
|
|
|
$entryManager->create(['name' => 'foo', 'slug' => 'foo']); |
146
|
|
|
|
147
|
|
|
$request = (new ServerRequestFactory) |
148
|
|
|
->createServerRequest('POST', '/api/v1/entry') |
149
|
|
|
->withHeader('Content-Type', 'application/json') |
150
|
|
|
->withParsedBody([ |
151
|
|
|
'name' => 'foo', |
152
|
|
|
'slug' => 'foo', |
153
|
|
|
]); |
154
|
|
|
|
155
|
|
|
$this->expectException(InvalidEntityException::class); |
156
|
|
|
|
157
|
|
|
$container->get('router') |
158
|
|
|
->getRoute(self::ROUTE_NAME) |
159
|
|
|
->handle($request); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return void |
164
|
|
|
* |
165
|
|
|
* @runInSeparateProcess |
166
|
|
|
*/ |
167
|
|
|
public function testCreateWithInvalidPayload() : void |
168
|
|
|
{ |
169
|
|
|
$container = $this->getContainer(); |
170
|
|
|
|
171
|
|
|
$request = (new ServerRequestFactory) |
172
|
|
|
->createServerRequest('POST', '/api/v1/entry') |
173
|
|
|
->withHeader('Content-Type', 'application/json') |
174
|
|
|
->withParsedBody([null]); |
175
|
|
|
|
176
|
|
|
$this->expectException(BadRequestException::class); |
177
|
|
|
|
178
|
|
|
$container->get('router') |
179
|
|
|
->getRoute(self::ROUTE_NAME) |
180
|
|
|
->handle($request); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|