Skip to content

Commit 7ef3eb1

Browse files
authored
Merge pull request #62 from OpenClassrooms/handle_404_example
2 parents ab566c6 + d9d77f2 commit 7ef3eb1

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

src/Definition/Operation.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,12 @@ public function setParametersIn(Parameters $parameters, string $in): self
419419
return $this;
420420
}
421421

422-
public function getExample(?string $name = null): OperationExample
422+
public function getExample(?string $name = null, mixed $default = null): OperationExample
423423
{
424424
$examples = $this->getExamples();
425425
if ($name !== null) {
426426
return $examples
427-
->get($name)
427+
->get($name, $default)
428428
;
429429
}
430430

src/Preparator/Error404Preparator.php

+5-15
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,13 @@ private function prepareTestCase(DefinitionResponse $response): array
4040

4141
$testcases = [];
4242

43-
if ($operation->getRequestBodies()->count() === 0) {
44-
$testcases[] = $this->buildTestCase(
45-
OperationExample::create('RandomPath', $operation)
46-
->setForceRandom()
47-
->setResponse(
48-
ResponseExample::create()
49-
->setStatusCode($this->config->response->getStatusCode() ?? '404')
50-
->setHeaders($this->config->response->headers ?? [])
51-
->setContent($this->config->response->body ?? $response->getDescription())
52-
)
43+
foreach (range(1, $operation->getRequestBodies()->count() ?: 1) as $ignored) {
44+
$notFoundExample = $operation->getExample(
45+
'404',
46+
OperationExample::create('RandomPath', $operation)->setForceRandom()
5347
);
54-
}
55-
56-
foreach ($operation->getRequestBodies() as $ignored) {
5748
$testcases[] = $this->buildTestCase(
58-
OperationExample::create('RandomPath', $operation)
59-
->setForceRandom()
49+
$notFoundExample
6050
->setResponse(
6151
ResponseExample::create()
6252
->setStatusCode($this->config->response->getStatusCode() ?? '404')

0 commit comments

Comments
 (0)