Skip to content

Commit ea59e10

Browse files
authored
Merge pull request #75 from OpenClassrooms/http-dump
2 parents cb9074c + 492141b commit ea59e10

File tree

8 files changed

+119
-81
lines changed

8 files changed

+119
-81
lines changed

.idea/php.xml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

devenv.lock

Lines changed: 25 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
"devenv": {
44
"locked": {
55
"dir": "src/modules",
6-
"lastModified": 1708326833,
7-
"narHash": "sha256-cihQPArrJhDU1jqqKDeyI91ZK62lUlGXYNncz2k9mjk=",
6+
"lastModified": 1736426010,
87
"owner": "cachix",
98
"repo": "devenv",
10-
"rev": "bc4602a41c197edd5838c8a97da2ed0c1fc75d0e",
9+
"rev": "1c384bc4be3ee571511fbbc6fdc94fe47d60f6cf",
1110
"type": "github"
1211
},
1312
"original": {
@@ -20,11 +19,10 @@
2019
"flake-compat": {
2120
"flake": false,
2221
"locked": {
23-
"lastModified": 1696426674,
24-
"narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=",
22+
"lastModified": 1733328505,
2523
"owner": "edolstra",
2624
"repo": "flake-compat",
27-
"rev": "0f9255e01c2351cc7d116c072cb317785dd33b33",
25+
"rev": "ff81ac966bb2cae68946d5ed5fc4994f96d0ffec",
2826
"type": "github"
2927
},
3028
"original": {
@@ -33,37 +31,39 @@
3331
"type": "github"
3432
}
3533
},
36-
"flake-utils": {
34+
"git-hooks": {
3735
"inputs": {
38-
"systems": "systems"
36+
"flake-compat": "flake-compat",
37+
"gitignore": "gitignore",
38+
"nixpkgs": [
39+
"nixpkgs"
40+
]
3941
},
4042
"locked": {
41-
"lastModified": 1701680307,
42-
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
43-
"owner": "numtide",
44-
"repo": "flake-utils",
45-
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
43+
"lastModified": 1735882644,
44+
"owner": "cachix",
45+
"repo": "git-hooks.nix",
46+
"rev": "a5a961387e75ae44cc20f0a57ae463da5e959656",
4647
"type": "github"
4748
},
4849
"original": {
49-
"owner": "numtide",
50-
"repo": "flake-utils",
50+
"owner": "cachix",
51+
"repo": "git-hooks.nix",
5152
"type": "github"
5253
}
5354
},
5455
"gitignore": {
5556
"inputs": {
5657
"nixpkgs": [
57-
"pre-commit-hooks",
58+
"git-hooks",
5859
"nixpkgs"
5960
]
6061
},
6162
"locked": {
62-
"lastModified": 1703887061,
63-
"narHash": "sha256-gGPa9qWNc6eCXT/+Z5/zMkyYOuRZqeFZBDbopNZQkuY=",
63+
"lastModified": 1709087332,
6464
"owner": "hercules-ci",
6565
"repo": "gitignore.nix",
66-
"rev": "43e1aa1308018f37118e34d3a9cb4f5e75dc11d5",
66+
"rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
6767
"type": "github"
6868
},
6969
"original": {
@@ -74,11 +74,10 @@
7474
},
7575
"nixpkgs": {
7676
"locked": {
77-
"lastModified": 1708247094,
78-
"narHash": "sha256-H2VS7VwesetGDtIaaz4AMsRkPoSLEVzL/Ika8gnbUnE=",
77+
"lastModified": 1736848588,
7978
"owner": "NixOS",
8079
"repo": "nixpkgs",
81-
"rev": "045b51a3ae66f673ed44b5bbd1f4a341d96703bf",
80+
"rev": "357cd3dfdb8993af11268d755d53357720675e66",
8281
"type": "github"
8382
},
8483
"original": {
@@ -88,66 +87,14 @@
8887
"type": "github"
8988
}
9089
},
91-
"nixpkgs-stable": {
92-
"locked": {
93-
"lastModified": 1704874635,
94-
"narHash": "sha256-YWuCrtsty5vVZvu+7BchAxmcYzTMfolSPP5io8+WYCg=",
95-
"owner": "NixOS",
96-
"repo": "nixpkgs",
97-
"rev": "3dc440faeee9e889fe2d1b4d25ad0f430d449356",
98-
"type": "github"
99-
},
100-
"original": {
101-
"owner": "NixOS",
102-
"ref": "nixos-23.11",
103-
"repo": "nixpkgs",
104-
"type": "github"
105-
}
106-
},
107-
"pre-commit-hooks": {
108-
"inputs": {
109-
"flake-compat": "flake-compat",
110-
"flake-utils": "flake-utils",
111-
"gitignore": "gitignore",
112-
"nixpkgs": [
113-
"nixpkgs"
114-
],
115-
"nixpkgs-stable": "nixpkgs-stable"
116-
},
117-
"locked": {
118-
"lastModified": 1708018599,
119-
"narHash": "sha256-M+Ng6+SePmA8g06CmUZWi1AjG2tFBX9WCXElBHEKnyM=",
120-
"owner": "cachix",
121-
"repo": "pre-commit-hooks.nix",
122-
"rev": "5df5a70ad7575f6601d91f0efec95dd9bc619431",
123-
"type": "github"
124-
},
125-
"original": {
126-
"owner": "cachix",
127-
"repo": "pre-commit-hooks.nix",
128-
"type": "github"
129-
}
130-
},
13190
"root": {
13291
"inputs": {
13392
"devenv": "devenv",
93+
"git-hooks": "git-hooks",
13494
"nixpkgs": "nixpkgs",
135-
"pre-commit-hooks": "pre-commit-hooks"
136-
}
137-
},
138-
"systems": {
139-
"locked": {
140-
"lastModified": 1681028828,
141-
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
142-
"owner": "nix-systems",
143-
"repo": "default",
144-
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
145-
"type": "github"
146-
},
147-
"original": {
148-
"owner": "nix-systems",
149-
"repo": "default",
150-
"type": "github"
95+
"pre-commit-hooks": [
96+
"git-hooks"
97+
]
15198
}
15299
}
153100
},

src/Definition/Loader/OpenApiDefinitionLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ private function extractDeepExamples(Schema $schema, bool $optional = false, str
593593

594594
if (isset($schema->type)) {
595595
if ($schema->type === 'array' && $schema->items instanceof Schema) {
596+
if ($schema->example !== null) {
597+
return $schema->example;
598+
}
599+
596600
return [
597601
$this->extractDeepExamples($schema->items, false, $path),
598602
];

src/Preparator/Config/PreparatorConfig.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class PreparatorConfig
1515

1616
public ResponseConfig $response;
1717

18+
/**
19+
* @var array<string, string>
20+
*/
21+
public array $headers = [];
22+
1823
public function __construct()
1924
{
2025
$this->response = new ResponseConfig();

src/Preparator/TestCasesPreparator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ final public function buildTestCase(
5151
$example->setAuthenticationHeaders($this->tokens);
5252
}
5353

54+
$example->setHeaders($this->config->headers);
55+
5456
return new TestCase(
5557
static::getName()
5658
. ' - '
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace APITester\Requester;
6+
7+
use APITester\Util\Json;
8+
use Nyholm\Psr7\Response;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
12+
final class HttpDumpRequester extends Requester
13+
{
14+
/**
15+
* @var ResponseInterface[]
16+
*/
17+
private array $responses = [];
18+
19+
public function request(RequestInterface $request, string $id): void
20+
{
21+
$httpDump = $this->requestToHttp($request, $id);
22+
23+
echo "\n" . $httpDump . "\n";
24+
25+
$response = new Response($request->getMethod() === 'POST' ? 201 : 200);
26+
$this->responses[$id] = $response;
27+
}
28+
29+
public function getResponse(string $id): ResponseInterface
30+
{
31+
return $this->responses[$id];
32+
}
33+
34+
public static function getName(): string
35+
{
36+
return 'http-dump';
37+
}
38+
39+
private function requestToHttp(RequestInterface $request, string $requestName): string
40+
{
41+
$dump = "###\n";
42+
$dump .= $request->getMethod() . ' ' . '{{url}}' . $request->getUri() . "\n";
43+
44+
foreach ($request->getHeaders() as $name => $values) {
45+
foreach ($values as $value) {
46+
$dump .= "{$name}: {$value}\n";
47+
}
48+
}
49+
50+
$body = (string) $request->getBody();
51+
if (!empty($body)) {
52+
if (Json::isJson($body)) {
53+
$body = Json::prettify($body);
54+
}
55+
$dump .= "\n" . $body . "\n";
56+
}
57+
58+
return $dump;
59+
}
60+
}

src/Requester/Requester.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ abstract public function request(RequestInterface $request, string $id): void;
2626

2727
abstract public function getResponse(string $id): ResponseInterface;
2828

29-
abstract public function setBaseUri(string $baseUri): void;
30-
3129
protected function fillRequestVars(RequestInterface $request): void
3230
{
3331
foreach ($request->getHeaders() as $name => $header) {

src/Test/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function __construct(
9191
$this->id = Random::id('testcase_');
9292
$this->excludedFields = array_unique([...$this->excludedFields, ...$excludedFields]);
9393
$nameParts = explode(' - ', $name);
94-
$this->preparator = $nameParts[0] ?? null;
94+
$this->preparator = $nameParts[0] ?? '';
9595
$this->operation = $nameParts[1] ?? null;
9696
$this->request = $operationExample->getPsrRequest();
9797
$this->schemaValidation = $schemaValidation;

0 commit comments

Comments
 (0)