From 989516b7dad18769c0b04fd323cf1d6f6e34f098 Mon Sep 17 00:00:00 2001 From: "amelie.haladjian" Date: Thu, 28 Nov 2024 18:29:40 +0100 Subject: [PATCH 1/2] feat(api tester): use filters to exclude openapi endpoints --- src/Config/Filters.php | 14 ++++++++++++++ .../Config/Error400BadFormatsPreparatorConfig.php | 9 --------- .../Config/Error400BadTypesPreparatorConfig.php | 9 --------- ...ror400MissingRequiredFieldsPreparatorConfig.php | 9 --------- src/Preparator/Config/Error400PreparatorConfig.php | 10 ---------- src/Preparator/Config/PreparatorConfig.php | 5 +++++ src/Preparator/Error400Preparator.php | 8 ++------ src/Util/Traits/FilterableTrait.php | 7 +++++++ ...Error400MissingRequiredFieldsPreparatorTest.php | 6 +++++- 9 files changed, 33 insertions(+), 44 deletions(-) delete mode 100644 src/Preparator/Config/Error400BadFormatsPreparatorConfig.php delete mode 100644 src/Preparator/Config/Error400BadTypesPreparatorConfig.php delete mode 100644 src/Preparator/Config/Error400MissingRequiredFieldsPreparatorConfig.php delete mode 100644 src/Preparator/Config/Error400PreparatorConfig.php diff --git a/src/Config/Filters.php b/src/Config/Filters.php index 5d7cc3d..54e92a8 100644 --- a/src/Config/Filters.php +++ b/src/Config/Filters.php @@ -143,6 +143,20 @@ public function includes(Filterable $object): bool return $include; } + public function excludes(Filterable $object): bool + { + foreach ($this->getExclude() as $item) { + foreach ($item as $key => $value) { + [$operator, $value] = $this->handleTags($value); + if ($object->has($key, $value, $operator)) { + return true; + } + } + } + + return false; + } + /** * @return array{'exclude': ?array>} */ diff --git a/src/Preparator/Config/Error400BadFormatsPreparatorConfig.php b/src/Preparator/Config/Error400BadFormatsPreparatorConfig.php deleted file mode 100644 index 18e17d0..0000000 --- a/src/Preparator/Config/Error400BadFormatsPreparatorConfig.php +++ /dev/null @@ -1,9 +0,0 @@ -response = new ResponseConfig(); + $this->filters = new Filters(); } } diff --git a/src/Preparator/Error400Preparator.php b/src/Preparator/Error400Preparator.php index 1dac5be..d78db39 100644 --- a/src/Preparator/Error400Preparator.php +++ b/src/Preparator/Error400Preparator.php @@ -12,12 +12,8 @@ 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 { /** @@ -122,10 +118,10 @@ protected function getStatusCode(): string */ private function prepareTestCases(Operation $operation): array { - if ($this->config->excludeOpenApiEndpoints - && isset($operation->getExtensions()['x-usecase'])) { + if ($this->config->filters->excludes($operation)) { return []; } + $requiredParams = $operation->getParameters(true); return array_merge( diff --git a/src/Util/Traits/FilterableTrait.php b/src/Util/Traits/FilterableTrait.php index 6e229c4..1334c99 100644 --- a/src/Util/Traits/FilterableTrait.php +++ b/src/Util/Traits/FilterableTrait.php @@ -9,6 +9,13 @@ trait FilterableTrait public function has(string $prop, $value, string $operator = '='): bool { $self = collect([$this]); + + $object = $self->whereNotNull($prop) + ->first(); + if (is_array($object->{$prop}) && array_key_exists((string) $value, $object->{$prop})) { + return true; + } + if (str_contains($prop, '*')) { $operator = 'contains'; } diff --git a/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php b/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php index b762e1f..2455f13 100644 --- a/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php +++ b/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php @@ -205,7 +205,11 @@ public function getData(): iterable [ ], [ - 'excludeOpenApiEndpoints' => true, + 'filters' => [ + 'exclude' => [[ + 'extensions' => 'x-usecase', + ]], + ], ], ]; } From 101e3375f4b212c5cd4041fd4ee69c8be04caf41 Mon Sep 17 00:00:00 2001 From: "amelie.haladjian" Date: Fri, 29 Nov 2024 10:34:00 +0100 Subject: [PATCH 2/2] feat(api tester): use IN tag to filter --- src/Config/Filters.php | 22 ++------- src/Preparator/Config/PreparatorConfig.php | 5 -- src/Preparator/Error400Preparator.php | 4 -- src/Util/Traits/FilterableTrait.php | 6 --- ...400MissingRequiredFieldsPreparatorTest.php | 49 +------------------ 5 files changed, 6 insertions(+), 80 deletions(-) diff --git a/src/Config/Filters.php b/src/Config/Filters.php index 54e92a8..772e45b 100644 --- a/src/Config/Filters.php +++ b/src/Config/Filters.php @@ -143,20 +143,6 @@ public function includes(Filterable $object): bool return $include; } - public function excludes(Filterable $object): bool - { - foreach ($this->getExclude() as $item) { - foreach ($item as $key => $value) { - [$operator, $value] = $this->handleTags($value); - if ($object->has($key, $value, $operator)) { - return true; - } - } - } - - return false; - } - /** * @return array{'exclude': ?array>} */ @@ -174,9 +160,11 @@ private function handleTags(string|int|TaggedValue $value): array $operator = '='; if ($value instanceof TaggedValue) { - if ($value->getTag() === 'NOT') { - $operator = '!='; - } + match ($value->getTag()) { + 'NOT' => $operator = '!=', + 'IN' => $operator = 'contains', + default => $operator, + }; $value = (string) $value->getValue(); } diff --git a/src/Preparator/Config/PreparatorConfig.php b/src/Preparator/Config/PreparatorConfig.php index 34341eb..0b5786d 100644 --- a/src/Preparator/Config/PreparatorConfig.php +++ b/src/Preparator/Config/PreparatorConfig.php @@ -4,8 +4,6 @@ namespace APITester\Preparator\Config; -use APITester\Config\Filters; - class PreparatorConfig { /** @@ -17,11 +15,8 @@ class PreparatorConfig public ResponseConfig $response; - public Filters $filters; - public function __construct() { $this->response = new ResponseConfig(); - $this->filters = new Filters(); } } diff --git a/src/Preparator/Error400Preparator.php b/src/Preparator/Error400Preparator.php index d78db39..03b54d9 100644 --- a/src/Preparator/Error400Preparator.php +++ b/src/Preparator/Error400Preparator.php @@ -118,10 +118,6 @@ protected function getStatusCode(): string */ private function prepareTestCases(Operation $operation): array { - if ($this->config->filters->excludes($operation)) { - return []; - } - $requiredParams = $operation->getParameters(true); return array_merge( diff --git a/src/Util/Traits/FilterableTrait.php b/src/Util/Traits/FilterableTrait.php index 1334c99..7ef8b7c 100644 --- a/src/Util/Traits/FilterableTrait.php +++ b/src/Util/Traits/FilterableTrait.php @@ -10,12 +10,6 @@ public function has(string $prop, $value, string $operator = '='): bool { $self = collect([$this]); - $object = $self->whereNotNull($prop) - ->first(); - if (is_array($object->{$prop}) && array_key_exists((string) $value, $object->{$prop})) { - return true; - } - if (str_contains($prop, '*')) { $operator = 'contains'; } diff --git a/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php b/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php index 2455f13..a74d2b5 100644 --- a/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php +++ b/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php @@ -22,7 +22,7 @@ final class Error400MissingRequiredFieldsPreparatorTest extends \PHPUnit\Framewo * @dataProvider getData * * @param array> $config - * @param TestCase[] $expected + * @param TestCase[] $expected */ public function test(Api $api, array $expected, array $config = []): void { @@ -165,52 +165,5 @@ 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', - ]) - ), - [ - ], - [ - 'filters' => [ - 'exclude' => [[ - 'extensions' => 'x-usecase', - ]], - ], - ], - ]; } }