Skip to content

Commit

Permalink
Merge pull request #68 from OpenClassrooms/deactivate_some_preparators
Browse files Browse the repository at this point in the history
feat(api tester): exclude 400 test cases for openapi endpoints
  • Loading branch information
Dzov authored Nov 27, 2024
2 parents 2f54edb + 58ef8f1 commit 699dcda
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Definition/Loader/OpenApiDefinitionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private function getOperations(array $paths, array $securitySchemes, array $filt
->setTags($this->getTags($operation->tags))
->setSecurities($this->getSecurities($securitySchemes, $requirements))
->setExamples($this->getExamples($operation, $parameters))
->setExtensions($operation->getExtensions())
);
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/Definition/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ final class Operation implements Filterable

private OperationExamples $examples;

/**
* @var array<string, mixed>
*/
private array $extensions;

public function __construct(
private readonly string $id,
private readonly string $path,
Expand Down Expand Up @@ -200,6 +205,14 @@ public function setResponses(Responses $responses): self
return $this;
}

/**
* @return array<string, mixed> $extensions
*/
public function getExtensions(): array
{
return $this->extensions;
}

public function getResponse(int $status): ?Response
{
return $this->responses->get($status);
Expand Down Expand Up @@ -277,6 +290,16 @@ public function setExamples(OperationExamples $examples): self
return $this;
}

/**
* @param array<string, mixed> $extensions
*/
public function setExtensions(array $extensions): self
{
$this->extensions = $extensions;

return $this;
}

/**
* @param array<int|string, string|int> $params
*
Expand Down
9 changes: 9 additions & 0 deletions src/Preparator/Config/Error400BadFormatsPreparatorConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace APITester\Preparator\Config;

final class Error400BadFormatsPreparatorConfig extends Error400PreparatorConfig
{
}
9 changes: 9 additions & 0 deletions src/Preparator/Config/Error400BadTypesPreparatorConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace APITester\Preparator\Config;

final class Error400BadTypesPreparatorConfig extends Error400PreparatorConfig
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace APITester\Preparator\Config;

final class Error400MissingRequiredFieldsPreparatorConfig extends Error400PreparatorConfig
{
}
10 changes: 10 additions & 0 deletions src/Preparator/Config/Error400PreparatorConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace APITester\Preparator\Config;

abstract class Error400PreparatorConfig extends PreparatorConfig
{
public bool $excludeOpenApiEndpoints = false;
}
10 changes: 9 additions & 1 deletion src/Preparator/Error400Preparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
use APITester\Definition\Example\ResponseExample;
use APITester\Definition\Operation;
use APITester\Definition\Parameter;
use APITester\Preparator\Config\Error400PreparatorConfig;
use APITester\Test\TestCase;

/**
* @property Error400PreparatorConfig $config
*/
abstract class Error400Preparator extends TestCasesPreparator
{
/**
Expand All @@ -23,7 +27,7 @@ protected function prepare(Operations $operations): iterable
{
/** @var iterable<array-key, TestCase> */
return $operations
->map(fn (Operation $op) => $this->prepareTestCases($op))
->map(fn (Operation $operation) => $this->prepareTestCases($operation))
->flatten()
;
}
Expand Down Expand Up @@ -118,6 +122,10 @@ protected function getStatusCode(): string
*/
private function prepareTestCases(Operation $operation): array
{
if ($this->config->excludeOpenApiEndpoints
&& isset($operation->getExtensions()['x-usecase'])) {
return [];
}
$requiredParams = $operation->getParameters(true);

return array_merge(
Expand Down
47 changes: 46 additions & 1 deletion tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ final class Error400MissingRequiredFieldsPreparatorTest extends \PHPUnit\Framewo
/**
* @dataProvider getData
*
* @param array<string, array<mixed>> $config
* @param TestCase[] $expected
*/
public function test(Api $api, array $expected): void
public function test(Api $api, array $expected, array $config = []): void
{
$preparator = new Error400MissingRequiredFieldsPreparator();
$preparator->configure($config);
Assert::objectsEqual(
$expected,
$preparator->doPrepare($api->getOperations()),
Expand Down Expand Up @@ -163,5 +165,48 @@ public function getData(): iterable
),
],
];

yield 'openapi endpoint is ignored' => [
Api::create()
->addOperation(
Operation::create(
'test',
'/test',
'POST'
)
->addRequestBody(
(new Body(
new Schema([
'type' => 'object',
'properties' => [
'foo' => [
'type' => 'string',
],
'bar' => [
'type' => 'string',
],
],
'required' => ['foo'],
]),
'application/json'
))
)
->addExample(
OperationExample::create('foo')
->setBodyContent([
'foo' => 'foo_body1',
'bar' => 'bar_body1',
])
->setQueryParameter('foo_query', 'foo1')
)->setExtensions([
'x-usecase' => 'UseCaseName',
])
),
[
],
[
'excludeOpenApiEndpoints' => true,
],
];
}
}

0 comments on commit 699dcda

Please sign in to comment.