Skip to content

Commit 58ef8f1

Browse files
committed
feat(api tester): exclude 400 test cases for openapi endpoints
1 parent 2f54edb commit 58ef8f1

8 files changed

+116
-2
lines changed

src/Definition/Loader/OpenApiDefinitionLoader.php

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ private function getOperations(array $paths, array $securitySchemes, array $filt
133133
->setTags($this->getTags($operation->tags))
134134
->setSecurities($this->getSecurities($securitySchemes, $requirements))
135135
->setExamples($this->getExamples($operation, $parameters))
136+
->setExtensions($operation->getExtensions())
136137
);
137138
}
138139
}

src/Definition/Operation.php

+23
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ final class Operation implements Filterable
4343

4444
private OperationExamples $examples;
4545

46+
/**
47+
* @var array<string, mixed>
48+
*/
49+
private array $extensions;
50+
4651
public function __construct(
4752
private readonly string $id,
4853
private readonly string $path,
@@ -200,6 +205,14 @@ public function setResponses(Responses $responses): self
200205
return $this;
201206
}
202207

208+
/**
209+
* @return array<string, mixed> $extensions
210+
*/
211+
public function getExtensions(): array
212+
{
213+
return $this->extensions;
214+
}
215+
203216
public function getResponse(int $status): ?Response
204217
{
205218
return $this->responses->get($status);
@@ -277,6 +290,16 @@ public function setExamples(OperationExamples $examples): self
277290
return $this;
278291
}
279292

293+
/**
294+
* @param array<string, mixed> $extensions
295+
*/
296+
public function setExtensions(array $extensions): self
297+
{
298+
$this->extensions = $extensions;
299+
300+
return $this;
301+
}
302+
280303
/**
281304
* @param array<int|string, string|int> $params
282305
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace APITester\Preparator\Config;
6+
7+
final class Error400BadFormatsPreparatorConfig extends Error400PreparatorConfig
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace APITester\Preparator\Config;
6+
7+
final class Error400BadTypesPreparatorConfig extends Error400PreparatorConfig
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace APITester\Preparator\Config;
6+
7+
final class Error400MissingRequiredFieldsPreparatorConfig extends Error400PreparatorConfig
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace APITester\Preparator\Config;
6+
7+
abstract class Error400PreparatorConfig extends PreparatorConfig
8+
{
9+
public bool $excludeOpenApiEndpoints = false;
10+
}

src/Preparator/Error400Preparator.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
use APITester\Definition\Example\ResponseExample;
1313
use APITester\Definition\Operation;
1414
use APITester\Definition\Parameter;
15+
use APITester\Preparator\Config\Error400PreparatorConfig;
1516
use APITester\Test\TestCase;
1617

18+
/**
19+
* @property Error400PreparatorConfig $config
20+
*/
1721
abstract class Error400Preparator extends TestCasesPreparator
1822
{
1923
/**
@@ -23,7 +27,7 @@ protected function prepare(Operations $operations): iterable
2327
{
2428
/** @var iterable<array-key, TestCase> */
2529
return $operations
26-
->map(fn (Operation $op) => $this->prepareTestCases($op))
30+
->map(fn (Operation $operation) => $this->prepareTestCases($operation))
2731
->flatten()
2832
;
2933
}
@@ -118,6 +122,10 @@ protected function getStatusCode(): string
118122
*/
119123
private function prepareTestCases(Operation $operation): array
120124
{
125+
if ($this->config->excludeOpenApiEndpoints
126+
&& isset($operation->getExtensions()['x-usecase'])) {
127+
return [];
128+
}
121129
$requiredParams = $operation->getParameters(true);
122130

123131
return array_merge(

tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php

+46-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ final class Error400MissingRequiredFieldsPreparatorTest extends \PHPUnit\Framewo
2121
/**
2222
* @dataProvider getData
2323
*
24+
* @param array<string, array<mixed>> $config
2425
* @param TestCase[] $expected
2526
*/
26-
public function test(Api $api, array $expected): void
27+
public function test(Api $api, array $expected, array $config = []): void
2728
{
2829
$preparator = new Error400MissingRequiredFieldsPreparator();
30+
$preparator->configure($config);
2931
Assert::objectsEqual(
3032
$expected,
3133
$preparator->doPrepare($api->getOperations()),
@@ -163,5 +165,48 @@ public function getData(): iterable
163165
),
164166
],
165167
];
168+
169+
yield 'openapi endpoint is ignored' => [
170+
Api::create()
171+
->addOperation(
172+
Operation::create(
173+
'test',
174+
'/test',
175+
'POST'
176+
)
177+
->addRequestBody(
178+
(new Body(
179+
new Schema([
180+
'type' => 'object',
181+
'properties' => [
182+
'foo' => [
183+
'type' => 'string',
184+
],
185+
'bar' => [
186+
'type' => 'string',
187+
],
188+
],
189+
'required' => ['foo'],
190+
]),
191+
'application/json'
192+
))
193+
)
194+
->addExample(
195+
OperationExample::create('foo')
196+
->setBodyContent([
197+
'foo' => 'foo_body1',
198+
'bar' => 'bar_body1',
199+
])
200+
->setQueryParameter('foo_query', 'foo1')
201+
)->setExtensions([
202+
'x-usecase' => 'UseCaseName',
203+
])
204+
),
205+
[
206+
],
207+
[
208+
'excludeOpenApiEndpoints' => true,
209+
],
210+
];
166211
}
167212
}

0 commit comments

Comments
 (0)