From d9d77f2e64f01126c5ab56e8c9e348fab398fea4 Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 18 Apr 2024 14:21:34 +0200 Subject: [PATCH 01/11] feat(404-prepartor): handle examples for 404 --- src/Definition/Operation.php | 4 ++-- src/Preparator/Error404Preparator.php | 20 +++++--------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/Definition/Operation.php b/src/Definition/Operation.php index b5f406b..8c9de33 100644 --- a/src/Definition/Operation.php +++ b/src/Definition/Operation.php @@ -419,12 +419,12 @@ public function setParametersIn(Parameters $parameters, string $in): self return $this; } - public function getExample(?string $name = null): OperationExample + public function getExample(?string $name = null, mixed $default = null): OperationExample { $examples = $this->getExamples(); if ($name !== null) { return $examples - ->get($name) + ->get($name, $default) ; } diff --git a/src/Preparator/Error404Preparator.php b/src/Preparator/Error404Preparator.php index 08302be..5a6209a 100644 --- a/src/Preparator/Error404Preparator.php +++ b/src/Preparator/Error404Preparator.php @@ -40,23 +40,13 @@ private function prepareTestCase(DefinitionResponse $response): array $testcases = []; - if ($operation->getRequestBodies()->count() === 0) { - $testcases[] = $this->buildTestCase( - OperationExample::create('RandomPath', $operation) - ->setForceRandom() - ->setResponse( - ResponseExample::create() - ->setStatusCode($this->config->response->getStatusCode() ?? '404') - ->setHeaders($this->config->response->headers ?? []) - ->setContent($this->config->response->body ?? $response->getDescription()) - ) + foreach (range(1, $operation->getRequestBodies()->count() ?: 1) as $ignored) { + $notFoundExample = $operation->getExample( + '404', + OperationExample::create('RandomPath', $operation)->setForceRandom() ); - } - - foreach ($operation->getRequestBodies() as $ignored) { $testcases[] = $this->buildTestCase( - OperationExample::create('RandomPath', $operation) - ->setForceRandom() + $notFoundExample ->setResponse( ResponseExample::create() ->setStatusCode($this->config->response->getStatusCode() ?? '404') From 2482cfca6bf7aaeb82587cb479e4dc742b266558 Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 16 May 2024 14:51:23 +0200 Subject: [PATCH 02/11] feat(examples-prepartor): exclude array bodies from auto complete --- src/Definition/Example/OperationExample.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Definition/Example/OperationExample.php b/src/Definition/Example/OperationExample.php index 18d5930..2d9145b 100644 --- a/src/Definition/Example/OperationExample.php +++ b/src/Definition/Example/OperationExample.php @@ -324,7 +324,10 @@ public function getBody(): ?BodyExample if ($this->autoComplete) { $randomBody = BodyExample::create($requestBody->getRandomContent()); if ($this->body !== null) { - $this->body->setContent(array_merge($randomBody->getContent(), $this->body->getContent())); + $content = $this->body->getContent(); + if (!isset($content[0])) { + $this->body->setContent(array_merge($randomBody->getContent(), $content)); + } } else { $this->body = $randomBody; } From a8d71454a97aebd80f3b679711047e7191c6e157 Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 16 May 2024 15:14:00 +0200 Subject: [PATCH 03/11] feat(404-prepartor): fix 404 generated path parameters --- src/Definition/Example/OperationExample.php | 8 +++++++ src/Preparator/Error404Preparator.php | 23 ++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Definition/Example/OperationExample.php b/src/Definition/Example/OperationExample.php index 2d9145b..908a386 100644 --- a/src/Definition/Example/OperationExample.php +++ b/src/Definition/Example/OperationExample.php @@ -165,6 +165,14 @@ public function setName(string $name): self return $this; } + public function withName(string $name): self + { + $clone = $this->deepCopy->copy($this); + $clone->name = $name; + + return $this; + } + /** * @return array */ diff --git a/src/Preparator/Error404Preparator.php b/src/Preparator/Error404Preparator.php index 08302be..5e8ccf1 100644 --- a/src/Preparator/Error404Preparator.php +++ b/src/Preparator/Error404Preparator.php @@ -5,13 +5,14 @@ namespace APITester\Preparator; use APITester\Definition\Collection\Operations; -use APITester\Definition\Example\OperationExample; use APITester\Definition\Example\ResponseExample; use APITester\Definition\Response as DefinitionResponse; use APITester\Test\TestCase; final class Error404Preparator extends TestCasesPreparator { + public const INT32_MAX = 2147483647; + /** * @inheritDoc */ @@ -40,13 +41,20 @@ private function prepareTestCase(DefinitionResponse $response): array $testcases = []; + $pathParameters = $operation->getPathParameters() + ->map(static fn ($parameter) => $parameter->getName()) + ->toArray() + ; + $pathParameters = array_fill_keys($pathParameters, self::INT32_MAX); + if ($operation->getRequestBodies()->count() === 0) { $testcases[] = $this->buildTestCase( - OperationExample::create('RandomPath', $operation) - ->setForceRandom() + $operation->getExample() + ->withName('RandomPath') + ->setPathParameters($pathParameters) ->setResponse( ResponseExample::create() - ->setStatusCode($this->config->response->getStatusCode() ?? '404') + ->setStatusCode('404') ->setHeaders($this->config->response->headers ?? []) ->setContent($this->config->response->body ?? $response->getDescription()) ) @@ -55,11 +63,12 @@ private function prepareTestCase(DefinitionResponse $response): array foreach ($operation->getRequestBodies() as $ignored) { $testcases[] = $this->buildTestCase( - OperationExample::create('RandomPath', $operation) - ->setForceRandom() + $operation->getExample() + ->withName('RandomPath') + ->setPathParameters($pathParameters) ->setResponse( ResponseExample::create() - ->setStatusCode($this->config->response->getStatusCode() ?? '404') + ->setStatusCode('404') ->setHeaders($this->config->response->headers ?? []) ->setContent($this->config->response->body ?? $response->getDescription()) ) From c1398e52cecfc4dd7d51eb3d7e410e827e369da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Jaume?= Date: Mon, 24 Jun 2024 11:28:34 +0200 Subject: [PATCH 04/11] fix phpstan error --- src/Definition/Example/OperationExample.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Definition/Example/OperationExample.php b/src/Definition/Example/OperationExample.php index 908a386..58a1120 100644 --- a/src/Definition/Example/OperationExample.php +++ b/src/Definition/Example/OperationExample.php @@ -167,6 +167,7 @@ public function setName(string $name): self public function withName(string $name): self { + /** @var OperationExample $clone */ $clone = $this->deepCopy->copy($this); $clone->name = $name; @@ -234,8 +235,8 @@ public function getQueryParameters(): array ->count() ; if ($this->forceRandom || ($this->autoComplete && \count( - $this->queryParameters - ) < $definitionParamsCount)) { + $this->queryParameters + ) < $definitionParamsCount)) { $randomQueryParams = $this->parent ->getQueryParameters() ->getRandomExamples() @@ -273,8 +274,8 @@ public function getHeaders(): array if ($this->parent !== null) { $definitionHeadersCount = $this->parent - ->getHeaders() - ->count() + 1 // content-type + ->getHeaders() + ->count() + 1 // content-type ; if ($this->parent->getSecurities()->count() > 0) { From cce0103db433155631fe28ea741e8d4e4b55e76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Jaume?= Date: Mon, 24 Jun 2024 15:44:44 +0200 Subject: [PATCH 05/11] fix error in withName --- src/Definition/Example/OperationExample.php | 2 +- src/Preparator/Error404Preparator.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Definition/Example/OperationExample.php b/src/Definition/Example/OperationExample.php index 58a1120..832775b 100644 --- a/src/Definition/Example/OperationExample.php +++ b/src/Definition/Example/OperationExample.php @@ -171,7 +171,7 @@ public function withName(string $name): self $clone = $this->deepCopy->copy($this); $clone->name = $name; - return $this; + return $clone; } /** diff --git a/src/Preparator/Error404Preparator.php b/src/Preparator/Error404Preparator.php index 5e8ccf1..3e2c757 100644 --- a/src/Preparator/Error404Preparator.php +++ b/src/Preparator/Error404Preparator.php @@ -43,8 +43,8 @@ private function prepareTestCase(DefinitionResponse $response): array $pathParameters = $operation->getPathParameters() ->map(static fn ($parameter) => $parameter->getName()) - ->toArray() - ; + ->toArray(); + $pathParameters = array_fill_keys($pathParameters, self::INT32_MAX); if ($operation->getRequestBodies()->count() === 0) { From 1935dbff284e6ba6bf187cfc4dd645d0a4f4ce0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Jaume?= Date: Mon, 24 Jun 2024 16:49:21 +0200 Subject: [PATCH 06/11] fix phpstan error in Error404Preparator --- src/Preparator/Error404Preparator.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Preparator/Error404Preparator.php b/src/Preparator/Error404Preparator.php index 3e2c757..e7cbcac 100644 --- a/src/Preparator/Error404Preparator.php +++ b/src/Preparator/Error404Preparator.php @@ -41,11 +41,9 @@ private function prepareTestCase(DefinitionResponse $response): array $testcases = []; - $pathParameters = $operation->getPathParameters() - ->map(static fn ($parameter) => $parameter->getName()) - ->toArray(); - - $pathParameters = array_fill_keys($pathParameters, self::INT32_MAX); + $pathParameters = array_map(fn ($parameter) => $parameter->getName(), + $operation->getPathParameters()->toArray()); + $pathParameters = array_fill_keys(array_values($pathParameters), self::INT32_MAX); if ($operation->getRequestBodies()->count() === 0) { $testcases[] = $this->buildTestCase( From d617b21be7b3334c1230a374a19cb40b585216d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Jaume?= Date: Mon, 24 Jun 2024 16:53:49 +0200 Subject: [PATCH 07/11] fix ecs errors --- src/Definition/Example/OperationExample.php | 8 ++++---- src/Preparator/Error404Preparator.php | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Definition/Example/OperationExample.php b/src/Definition/Example/OperationExample.php index 832775b..bed5a6c 100644 --- a/src/Definition/Example/OperationExample.php +++ b/src/Definition/Example/OperationExample.php @@ -235,8 +235,8 @@ public function getQueryParameters(): array ->count() ; if ($this->forceRandom || ($this->autoComplete && \count( - $this->queryParameters - ) < $definitionParamsCount)) { + $this->queryParameters + ) < $definitionParamsCount)) { $randomQueryParams = $this->parent ->getQueryParameters() ->getRandomExamples() @@ -274,8 +274,8 @@ public function getHeaders(): array if ($this->parent !== null) { $definitionHeadersCount = $this->parent - ->getHeaders() - ->count() + 1 // content-type + ->getHeaders() + ->count() + 1 // content-type ; if ($this->parent->getSecurities()->count() > 0) { diff --git a/src/Preparator/Error404Preparator.php b/src/Preparator/Error404Preparator.php index e7cbcac..f6a1b76 100644 --- a/src/Preparator/Error404Preparator.php +++ b/src/Preparator/Error404Preparator.php @@ -41,8 +41,11 @@ private function prepareTestCase(DefinitionResponse $response): array $testcases = []; - $pathParameters = array_map(fn ($parameter) => $parameter->getName(), - $operation->getPathParameters()->toArray()); + $pathParameters = array_map( + static fn ($parameter) => $parameter->getName(), + $operation->getPathParameters() + ->toArray() + ); $pathParameters = array_fill_keys(array_values($pathParameters), self::INT32_MAX); if ($operation->getRequestBodies()->count() === 0) { From ab566c6190589673a8cdef6bf832d04c5528fb19 Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 27 Jun 2024 14:31:04 +0200 Subject: [PATCH 08/11] feat(404-prepartor): revert changes on 404 prepartor --- src/Preparator/Error404Preparator.php | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/Preparator/Error404Preparator.php b/src/Preparator/Error404Preparator.php index f6a1b76..08302be 100644 --- a/src/Preparator/Error404Preparator.php +++ b/src/Preparator/Error404Preparator.php @@ -5,14 +5,13 @@ namespace APITester\Preparator; use APITester\Definition\Collection\Operations; +use APITester\Definition\Example\OperationExample; use APITester\Definition\Example\ResponseExample; use APITester\Definition\Response as DefinitionResponse; use APITester\Test\TestCase; final class Error404Preparator extends TestCasesPreparator { - public const INT32_MAX = 2147483647; - /** * @inheritDoc */ @@ -41,21 +40,13 @@ private function prepareTestCase(DefinitionResponse $response): array $testcases = []; - $pathParameters = array_map( - static fn ($parameter) => $parameter->getName(), - $operation->getPathParameters() - ->toArray() - ); - $pathParameters = array_fill_keys(array_values($pathParameters), self::INT32_MAX); - if ($operation->getRequestBodies()->count() === 0) { $testcases[] = $this->buildTestCase( - $operation->getExample() - ->withName('RandomPath') - ->setPathParameters($pathParameters) + OperationExample::create('RandomPath', $operation) + ->setForceRandom() ->setResponse( ResponseExample::create() - ->setStatusCode('404') + ->setStatusCode($this->config->response->getStatusCode() ?? '404') ->setHeaders($this->config->response->headers ?? []) ->setContent($this->config->response->body ?? $response->getDescription()) ) @@ -64,12 +55,11 @@ private function prepareTestCase(DefinitionResponse $response): array foreach ($operation->getRequestBodies() as $ignored) { $testcases[] = $this->buildTestCase( - $operation->getExample() - ->withName('RandomPath') - ->setPathParameters($pathParameters) + OperationExample::create('RandomPath', $operation) + ->setForceRandom() ->setResponse( ResponseExample::create() - ->setStatusCode('404') + ->setStatusCode($this->config->response->getStatusCode() ?? '404') ->setHeaders($this->config->response->headers ?? []) ->setContent($this->config->response->body ?? $response->getDescription()) ) From 7ea6335ea7710dcc2734f5fcc9f3efb8033e6a11 Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 27 Jun 2024 14:49:03 +0200 Subject: [PATCH 09/11] feat(404-prepartor): exclude new 404 example from examples-preparator --- src/Preparator/ExamplesPreparator.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Preparator/ExamplesPreparator.php b/src/Preparator/ExamplesPreparator.php index 1cc5bbc..e9810fd 100644 --- a/src/Preparator/ExamplesPreparator.php +++ b/src/Preparator/ExamplesPreparator.php @@ -57,7 +57,10 @@ private function handleExtension(Operations $operations): Operations */ private function prepareTestCases(Operation $operation): iterable { - $examples = $operation->getExamples(); + $examples = $operation + ->getExamples() + ->filter(fn (OperationExample $example) => $example->getName() !== '404') + ; if ($this->config->autoCreateWhenMissing && $examples->count() === 0) { $examples = new OperationExamples([ $operation->getRandomExample(), From fa0d27c4354fbe1c8370e8c2731b3f7f8597efe3 Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 27 Jun 2024 15:10:12 +0200 Subject: [PATCH 10/11] feat(404-prepartor): handle int example name --- src/Definition/Loader/OpenApiDefinitionLoader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Definition/Loader/OpenApiDefinitionLoader.php b/src/Definition/Loader/OpenApiDefinitionLoader.php index bba1da5..9e0ca29 100644 --- a/src/Definition/Loader/OpenApiDefinitionLoader.php +++ b/src/Definition/Loader/OpenApiDefinitionLoader.php @@ -549,7 +549,7 @@ private function getHeaders(array $headers): Parameters /** * @param array $examples */ - private function getExample(string $name, array &$examples, ?int $statusCode = null): OperationExample + private function getExample(string|int $name, array &$examples, ?int $statusCode = null): OperationExample { if (!isset($examples[$name])) { $examples[$name] = new OperationExample($name, null, $statusCode); From 20d248387155ab77fee62db3407d858ea9300f78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Jaume?= Date: Fri, 28 Jun 2024 12:37:42 +0200 Subject: [PATCH 11/11] fix typing errors --- src/Definition/Example/OperationExample.php | 8 ++++---- src/Preparator/Error404Preparator.php | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Definition/Example/OperationExample.php b/src/Definition/Example/OperationExample.php index bed5a6c..1c92d80 100644 --- a/src/Definition/Example/OperationExample.php +++ b/src/Definition/Example/OperationExample.php @@ -51,7 +51,7 @@ final class OperationExample private DeepCopy $deepCopy; public function __construct( - private string $name, + private string|int $name, Operation $parent = null, ?int $statusCode = null, ) { @@ -153,19 +153,19 @@ public function withBody(BodyExample $body): self return $clone; } - public function getName(): string + public function getName(): string|int { return $this->name; } - public function setName(string $name): self + public function setName(string|int $name): self { $this->name = $name; return $this; } - public function withName(string $name): self + public function withName(string|int $name): self { /** @var OperationExample $clone */ $clone = $this->deepCopy->copy($this); diff --git a/src/Preparator/Error404Preparator.php b/src/Preparator/Error404Preparator.php index 5a6209a..99c66cb 100644 --- a/src/Preparator/Error404Preparator.php +++ b/src/Preparator/Error404Preparator.php @@ -40,7 +40,12 @@ private function prepareTestCase(DefinitionResponse $response): array $testcases = []; - foreach (range(1, $operation->getRequestBodies()->count() ?: 1) as $ignored) { + foreach ( + range( + 1, + $operation->getRequestBodies()->count() ?: 1 + ) as $ignored + ) { $notFoundExample = $operation->getExample( '404', OperationExample::create('RandomPath', $operation)->setForceRandom()