1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace App\Bundle\Example\Tests\Service; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Import classes |
7
|
|
|
*/ |
8
|
|
|
use App\Exception\EntityNotFoundException; |
9
|
|
|
use App\Exception\InvalidEntityException; |
10
|
|
|
use App\Tests\ContainerAwareTrait; |
11
|
|
|
use App\Tests\DatabaseSchemaToolTrait; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Sunrise\Http\Router\Exception\BadRequestException; |
14
|
|
|
use Sunrise\Http\Router\OpenApi\Test\OpenApiAssertKitTrait; |
15
|
|
|
use Sunrise\Http\ServerRequest\ServerRequestFactory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* EntryUpdateControllerTest |
19
|
|
|
*/ |
20
|
|
|
class EntryUpdateControllerTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
use ContainerAwareTrait; |
23
|
|
|
use DatabaseSchemaToolTrait; |
24
|
|
|
use OpenApiAssertKitTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private const ROUTE_NAME = 'api_v1_entry_update'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return void |
33
|
|
|
* |
34
|
|
|
* @runInSeparateProcess |
35
|
|
|
*/ |
36
|
|
|
public function testUpdate() : void |
37
|
|
|
{ |
38
|
|
|
$container = $this->getContainer(); |
39
|
|
|
$doctrine = $container->get('doctrine'); |
40
|
|
|
|
41
|
|
|
$entityManager = $doctrine->getManager('master'); |
42
|
|
|
$this->createDatabaseSchema($entityManager); |
43
|
|
|
|
44
|
|
|
$entryManager = $container->get('entryManager'); |
45
|
|
|
$this->assertSame(0, $entryManager->countAll()); |
46
|
|
|
|
47
|
|
|
$entry = $entryManager->create(['name' => 'foo', 'slug' => 'foo']); |
48
|
|
|
|
49
|
|
|
$request = (new ServerRequestFactory) |
50
|
|
|
->createServerRequest('PATCH', '/api/v1/entry/' . $entry->getId()->toString()) |
51
|
|
|
->withAttribute('entryId', $entry->getId()->toString()) |
52
|
|
|
->withHeader('Content-Type', 'application/json') |
53
|
|
|
->withParsedBody([ |
54
|
|
|
'name' => 'bar', |
55
|
|
|
'slug' => 'baz', |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$route = $container->get('router')->getRoute(self::ROUTE_NAME); |
59
|
|
|
$response = $route->handle($request); |
60
|
|
|
|
61
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
62
|
|
|
$this->assertResponseBodyMatchesDescription($route, $response); |
63
|
|
|
|
64
|
|
|
// re-loading the entry... |
65
|
|
|
$entry = $entryManager->findById($entry->getId()->toString()); |
66
|
|
|
|
67
|
|
|
$this->assertSame('bar', $entry->getName()); |
68
|
|
|
$this->assertSame('baz', $entry->getSlug()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return void |
73
|
|
|
* |
74
|
|
|
* @runInSeparateProcess |
75
|
|
|
*/ |
76
|
|
|
public function testUpdateNonExistentEntry() : void |
77
|
|
|
{ |
78
|
|
|
$container = $this->getContainer(); |
79
|
|
|
$doctrine = $container->get('doctrine'); |
80
|
|
|
|
81
|
|
|
$entityManager = $doctrine->getManager('master'); |
82
|
|
|
$this->createDatabaseSchema($entityManager); |
83
|
|
|
|
84
|
|
|
$entryManager = $container->get('entryManager'); |
85
|
|
|
$this->assertSame(0, $entryManager->countAll()); |
86
|
|
|
|
87
|
|
|
$request = (new ServerRequestFactory) |
88
|
|
|
->createServerRequest('PATCH', '/api/v1/entry/e3f4f8bd-d455-4e67-86d5-ab6c9683bdd7') |
89
|
|
|
->withAttribute('entryId', 'e3f4f8bd-d455-4e67-86d5-ab6c9683bdd7') |
90
|
|
|
->withHeader('Content-Type', 'application/json') |
91
|
|
|
->withParsedBody([ |
92
|
|
|
'name' => 'bar', |
93
|
|
|
'slug' => 'bar', |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$this->expectException(EntityNotFoundException::class); |
97
|
|
|
|
98
|
|
|
$container->get('router') |
99
|
|
|
->getRoute(self::ROUTE_NAME) |
100
|
|
|
->handle($request); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return void |
105
|
|
|
* |
106
|
|
|
* @runInSeparateProcess |
107
|
|
|
*/ |
108
|
|
|
public function testUpdateWithEmptyName() : void |
109
|
|
|
{ |
110
|
|
|
$container = $this->getContainer(); |
111
|
|
|
$doctrine = $container->get('doctrine'); |
112
|
|
|
|
113
|
|
|
$entityManager = $doctrine->getManager('master'); |
114
|
|
|
$this->createDatabaseSchema($entityManager); |
115
|
|
|
|
116
|
|
|
$entryManager = $container->get('entryManager'); |
117
|
|
|
$this->assertSame(0, $entryManager->countAll()); |
118
|
|
|
|
119
|
|
|
$entry = $entryManager->create(['name' => 'foo', 'slug' => 'foo']); |
120
|
|
|
|
121
|
|
|
$request = (new ServerRequestFactory) |
122
|
|
|
->createServerRequest('PATCH', '/api/v1/entry/' . $entry->getId()->toString()) |
123
|
|
|
->withAttribute('entryId', $entry->getId()->toString()) |
124
|
|
|
->withHeader('Content-Type', 'application/json') |
125
|
|
|
->withParsedBody([ |
126
|
|
|
'name' => '', |
127
|
|
|
'slug' => 'baz', |
128
|
|
|
]); |
129
|
|
|
|
130
|
|
|
$this->expectException(BadRequestException::class); |
131
|
|
|
|
132
|
|
|
$container->get('router') |
133
|
|
|
->getRoute(self::ROUTE_NAME) |
134
|
|
|
->handle($request); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return void |
139
|
|
|
* |
140
|
|
|
* @runInSeparateProcess |
141
|
|
|
*/ |
142
|
|
|
public function testUpdateWithEmptySlug() : void |
143
|
|
|
{ |
144
|
|
|
$container = $this->getContainer(); |
145
|
|
|
$doctrine = $container->get('doctrine'); |
146
|
|
|
|
147
|
|
|
$entityManager = $doctrine->getManager('master'); |
148
|
|
|
$this->createDatabaseSchema($entityManager); |
149
|
|
|
|
150
|
|
|
$entryManager = $container->get('entryManager'); |
151
|
|
|
$this->assertSame(0, $entryManager->countAll()); |
152
|
|
|
|
153
|
|
|
$entry = $entryManager->create(['name' => 'foo', 'slug' => 'foo']); |
154
|
|
|
|
155
|
|
|
$request = (new ServerRequestFactory) |
156
|
|
|
->createServerRequest('PATCH', '/api/v1/entry/' . $entry->getId()->toString()) |
157
|
|
|
->withAttribute('entryId', $entry->getId()->toString()) |
158
|
|
|
->withHeader('Content-Type', 'application/json') |
159
|
|
|
->withParsedBody([ |
160
|
|
|
'name' => 'bar', |
161
|
|
|
'slug' => '', |
162
|
|
|
]); |
163
|
|
|
|
164
|
|
|
$this->expectException(BadRequestException::class); |
165
|
|
|
|
166
|
|
|
$container->get('router') |
167
|
|
|
->getRoute(self::ROUTE_NAME) |
168
|
|
|
->handle($request); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return void |
173
|
|
|
* |
174
|
|
|
* @runInSeparateProcess |
175
|
|
|
*/ |
176
|
|
|
public function testUpdateWithNotUniqueSlug() : void |
177
|
|
|
{ |
178
|
|
|
$container = $this->getContainer(); |
179
|
|
|
$doctrine = $container->get('doctrine'); |
180
|
|
|
|
181
|
|
|
$entityManager = $doctrine->getManager('master'); |
182
|
|
|
$this->createDatabaseSchema($entityManager); |
183
|
|
|
|
184
|
|
|
$entryManager = $container->get('entryManager'); |
185
|
|
|
$this->assertSame(0, $entryManager->countAll()); |
186
|
|
|
|
187
|
|
|
$entry = $entryManager->create(['name' => 'foo', 'slug' => 'foo']); |
188
|
|
|
$entryManager->create(['name' => 'bar', 'slug' => 'bar']); |
189
|
|
|
|
190
|
|
|
$request = (new ServerRequestFactory) |
191
|
|
|
->createServerRequest('PATCH', '/api/v1/entry/' . $entry->getId()->toString()) |
192
|
|
|
->withAttribute('entryId', $entry->getId()->toString()) |
193
|
|
|
->withHeader('Content-Type', 'application/json') |
194
|
|
|
->withParsedBody([ |
195
|
|
|
'name' => 'bar', |
196
|
|
|
'slug' => 'bar', |
197
|
|
|
]); |
198
|
|
|
|
199
|
|
|
$this->expectException(InvalidEntityException::class); |
200
|
|
|
|
201
|
|
|
$container->get('router') |
202
|
|
|
->getRoute(self::ROUTE_NAME) |
203
|
|
|
->handle($request); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return void |
208
|
|
|
* |
209
|
|
|
* @runInSeparateProcess |
210
|
|
|
*/ |
211
|
|
|
public function testUpdateWithInvalidPayload() : void |
212
|
|
|
{ |
213
|
|
|
$container = $this->getContainer(); |
214
|
|
|
|
215
|
|
|
$request = (new ServerRequestFactory) |
216
|
|
|
->createServerRequest('PATCH', '/api/v1/entry/e3f4f8bd-d455-4e67-86d5-ab6c9683bdd7') |
217
|
|
|
->withAttribute('entryId', 'e3f4f8bd-d455-4e67-86d5-ab6c9683bdd7') |
218
|
|
|
->withHeader('Content-Type', 'application/json') |
219
|
|
|
->withParsedBody([null]); |
220
|
|
|
|
221
|
|
|
$this->expectException(BadRequestException::class); |
222
|
|
|
|
223
|
|
|
$container->get('router') |
224
|
|
|
->getRoute(self::ROUTE_NAME) |
225
|
|
|
->handle($request); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|