From 44bad934c024a47827e0b6f2bee6656c03bfb734 Mon Sep 17 00:00:00 2001 From: sidux Date: Wed, 15 Jan 2025 17:34:31 +0100 Subject: [PATCH 1/4] feat(http): add new requester to help generate http request for phpstorm --- devenv.lock | 103 +++++++--------------------- src/Requester/HttpDumpRequester.php | 60 ++++++++++++++++ src/Requester/Requester.php | 2 - 3 files changed, 85 insertions(+), 80 deletions(-) create mode 100644 src/Requester/HttpDumpRequester.php diff --git a/devenv.lock b/devenv.lock index 8610224..aca2b74 100644 --- a/devenv.lock +++ b/devenv.lock @@ -3,11 +3,10 @@ "devenv": { "locked": { "dir": "src/modules", - "lastModified": 1708326833, - "narHash": "sha256-cihQPArrJhDU1jqqKDeyI91ZK62lUlGXYNncz2k9mjk=", + "lastModified": 1736426010, "owner": "cachix", "repo": "devenv", - "rev": "bc4602a41c197edd5838c8a97da2ed0c1fc75d0e", + "rev": "1c384bc4be3ee571511fbbc6fdc94fe47d60f6cf", "type": "github" }, "original": { @@ -20,11 +19,10 @@ "flake-compat": { "flake": false, "locked": { - "lastModified": 1696426674, - "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "lastModified": 1733328505, "owner": "edolstra", "repo": "flake-compat", - "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "rev": "ff81ac966bb2cae68946d5ed5fc4994f96d0ffec", "type": "github" }, "original": { @@ -33,37 +31,39 @@ "type": "github" } }, - "flake-utils": { + "git-hooks": { "inputs": { - "systems": "systems" + "flake-compat": "flake-compat", + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ] }, "locked": { - "lastModified": 1701680307, - "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "lastModified": 1735882644, + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "a5a961387e75ae44cc20f0a57ae463da5e959656", "type": "github" }, "original": { - "owner": "numtide", - "repo": "flake-utils", + "owner": "cachix", + "repo": "git-hooks.nix", "type": "github" } }, "gitignore": { "inputs": { "nixpkgs": [ - "pre-commit-hooks", + "git-hooks", "nixpkgs" ] }, "locked": { - "lastModified": 1703887061, - "narHash": "sha256-gGPa9qWNc6eCXT/+Z5/zMkyYOuRZqeFZBDbopNZQkuY=", + "lastModified": 1709087332, "owner": "hercules-ci", "repo": "gitignore.nix", - "rev": "43e1aa1308018f37118e34d3a9cb4f5e75dc11d5", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", "type": "github" }, "original": { @@ -74,11 +74,10 @@ }, "nixpkgs": { "locked": { - "lastModified": 1708247094, - "narHash": "sha256-H2VS7VwesetGDtIaaz4AMsRkPoSLEVzL/Ika8gnbUnE=", + "lastModified": 1736848588, "owner": "NixOS", "repo": "nixpkgs", - "rev": "045b51a3ae66f673ed44b5bbd1f4a341d96703bf", + "rev": "357cd3dfdb8993af11268d755d53357720675e66", "type": "github" }, "original": { @@ -88,66 +87,14 @@ "type": "github" } }, - "nixpkgs-stable": { - "locked": { - "lastModified": 1704874635, - "narHash": "sha256-YWuCrtsty5vVZvu+7BchAxmcYzTMfolSPP5io8+WYCg=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "3dc440faeee9e889fe2d1b4d25ad0f430d449356", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixos-23.11", - "repo": "nixpkgs", - "type": "github" - } - }, - "pre-commit-hooks": { - "inputs": { - "flake-compat": "flake-compat", - "flake-utils": "flake-utils", - "gitignore": "gitignore", - "nixpkgs": [ - "nixpkgs" - ], - "nixpkgs-stable": "nixpkgs-stable" - }, - "locked": { - "lastModified": 1708018599, - "narHash": "sha256-M+Ng6+SePmA8g06CmUZWi1AjG2tFBX9WCXElBHEKnyM=", - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "rev": "5df5a70ad7575f6601d91f0efec95dd9bc619431", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "type": "github" - } - }, "root": { "inputs": { "devenv": "devenv", + "git-hooks": "git-hooks", "nixpkgs": "nixpkgs", - "pre-commit-hooks": "pre-commit-hooks" - } - }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" + "pre-commit-hooks": [ + "git-hooks" + ] } } }, diff --git a/src/Requester/HttpDumpRequester.php b/src/Requester/HttpDumpRequester.php new file mode 100644 index 0000000..f331e53 --- /dev/null +++ b/src/Requester/HttpDumpRequester.php @@ -0,0 +1,60 @@ +requestToHttp($request, $id); + + echo "\n" . $httpDump . "\n"; + + $response = new Response(200, [], 'Simulated response for ' . $id); + $this->responses[$id] = $response; + } + + public function getResponse(string $id): ResponseInterface + { + return $this->responses[$id]; + } + + public static function getName(): string + { + return 'http-dump'; + } + + private function requestToHttp(RequestInterface $request, string $requestName): string + { + $dump = "###\n"; + $dump .= $request->getMethod() . ' ' . '{{url}}' . $request->getUri() . "\n"; + + foreach ($request->getHeaders() as $name => $values) { + foreach ($values as $value) { + $dump .= "{$name}: {$value}\n"; + } + } + + $body = (string) $request->getBody(); + if (!empty($body)) { + if (Json::isJson($body)) { + $body = Json::prettify($body); + } + $dump .= "\n" . $body . "\n"; + } + + return $dump; + } +} diff --git a/src/Requester/Requester.php b/src/Requester/Requester.php index 31b5f63..1de165a 100644 --- a/src/Requester/Requester.php +++ b/src/Requester/Requester.php @@ -26,8 +26,6 @@ abstract public function request(RequestInterface $request, string $id): void; abstract public function getResponse(string $id): ResponseInterface; - abstract public function setBaseUri(string $baseUri): void; - protected function fillRequestVars(RequestInterface $request): void { foreach ($request->getHeaders() as $name => $header) { From 56b9129973e19d11824d5abf5f7eb1d1317e8bfd Mon Sep 17 00:00:00 2001 From: sidux Date: Wed, 15 Jan 2025 18:54:43 +0100 Subject: [PATCH 2/4] fix(example): fix ignored array examples --- .idea/php.xml | 22 +++++++++++++++++++ .../Loader/OpenApiDefinitionLoader.php | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/.idea/php.xml b/.idea/php.xml index 97cddef..631724e 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -214,6 +214,28 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Definition/Loader/OpenApiDefinitionLoader.php b/src/Definition/Loader/OpenApiDefinitionLoader.php index fdc9843..581b00e 100644 --- a/src/Definition/Loader/OpenApiDefinitionLoader.php +++ b/src/Definition/Loader/OpenApiDefinitionLoader.php @@ -593,6 +593,10 @@ private function extractDeepExamples(Schema $schema, bool $optional = false, str if (isset($schema->type)) { if ($schema->type === 'array' && $schema->items instanceof Schema) { + if ($schema->example !== null) { + return $schema->example; + } + return [ $this->extractDeepExamples($schema->items, false, $path), ]; From 2d5823a1a14ec05bbfa5d6ec7afe91466fc0ed38 Mon Sep 17 00:00:00 2001 From: sidux Date: Wed, 15 Jan 2025 19:12:27 +0100 Subject: [PATCH 3/4] feat(prepartor): add ability to configure headers --- src/Preparator/Config/PreparatorConfig.php | 2 ++ src/Preparator/TestCasesPreparator.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Preparator/Config/PreparatorConfig.php b/src/Preparator/Config/PreparatorConfig.php index 0b5786d..8cfbb4d 100644 --- a/src/Preparator/Config/PreparatorConfig.php +++ b/src/Preparator/Config/PreparatorConfig.php @@ -15,6 +15,8 @@ class PreparatorConfig public ResponseConfig $response; + public array $headers = []; + public function __construct() { $this->response = new ResponseConfig(); diff --git a/src/Preparator/TestCasesPreparator.php b/src/Preparator/TestCasesPreparator.php index a437643..097c070 100644 --- a/src/Preparator/TestCasesPreparator.php +++ b/src/Preparator/TestCasesPreparator.php @@ -51,6 +51,8 @@ final public function buildTestCase( $example->setAuthenticationHeaders($this->tokens); } + $example->setHeaders($this->config->headers); + return new TestCase( static::getName() . ' - ' From 492141b6f3cd4e094283b0ef202a1bbbe5ef4b3f Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 16 Jan 2025 15:22:51 +0100 Subject: [PATCH 4/4] fix(example): simplify needed config --- src/Preparator/Config/PreparatorConfig.php | 3 +++ src/Requester/HttpDumpRequester.php | 2 +- src/Test/TestCase.php | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Preparator/Config/PreparatorConfig.php b/src/Preparator/Config/PreparatorConfig.php index 8cfbb4d..57d1fcc 100644 --- a/src/Preparator/Config/PreparatorConfig.php +++ b/src/Preparator/Config/PreparatorConfig.php @@ -15,6 +15,9 @@ class PreparatorConfig public ResponseConfig $response; + /** + * @var array + */ public array $headers = []; public function __construct() diff --git a/src/Requester/HttpDumpRequester.php b/src/Requester/HttpDumpRequester.php index f331e53..4cdb68c 100644 --- a/src/Requester/HttpDumpRequester.php +++ b/src/Requester/HttpDumpRequester.php @@ -22,7 +22,7 @@ public function request(RequestInterface $request, string $id): void echo "\n" . $httpDump . "\n"; - $response = new Response(200, [], 'Simulated response for ' . $id); + $response = new Response($request->getMethod() === 'POST' ? 201 : 200); $this->responses[$id] = $response; } diff --git a/src/Test/TestCase.php b/src/Test/TestCase.php index 92c26c9..1eb9e8f 100644 --- a/src/Test/TestCase.php +++ b/src/Test/TestCase.php @@ -91,7 +91,7 @@ public function __construct( $this->id = Random::id('testcase_'); $this->excludedFields = array_unique([...$this->excludedFields, ...$excludedFields]); $nameParts = explode(' - ', $name); - $this->preparator = $nameParts[0] ?? null; + $this->preparator = $nameParts[0] ?? ''; $this->operation = $nameParts[1] ?? null; $this->request = $operationExample->getPsrRequest(); $this->schemaValidation = $schemaValidation;