Skip to content

Commit

Permalink
Merge pull request #58 from OpenClassrooms/faker_new_version
Browse files Browse the repository at this point in the history
Update openapi-schema-faker
  • Loading branch information
sidux authored Feb 19, 2024
2 parents 3f2f62e + a2bc9d4 commit de28b13
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source_url "https://raw.githubusercontent.com/cachix/devenv/d1f7b48e35e6dee421cfd0f51481d17f77586997/direnvrc" "sha256-YBzqskFZxmNb3kYVoKD9ZixoPXJh1C9ZvTLGFRkauZ0="

use devenv
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,13 @@ fabric.properties
/bin/
/build/
composer.lock

# Devenv
.devenv*
devenv.local.nix

# direnv
.direnv

# pre-commit
.pre-commit-config.yaml
12 changes: 9 additions & 3 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"ext-iconv": "*",
"ext-json": "*",
"ext-mbstring": "*",
"canvural/php-openapi-faker": "^1.0",
"canvural/php-openapi-faker": "^2.0",
"cebe/php-openapi": "^1.5",
"fakerphp/faker": "^1.9",
"firebase/php-jwt": "^6.0.0",
Expand Down
156 changes: 156 additions & 0 deletions devenv.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"nodes": {
"devenv": {
"locked": {
"dir": "src/modules",
"lastModified": 1708326833,
"narHash": "sha256-cihQPArrJhDU1jqqKDeyI91ZK62lUlGXYNncz2k9mjk=",
"owner": "cachix",
"repo": "devenv",
"rev": "bc4602a41c197edd5838c8a97da2ed0c1fc75d0e",
"type": "github"
},
"original": {
"dir": "src/modules",
"owner": "cachix",
"repo": "devenv",
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1696426674,
"narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "0f9255e01c2351cc7d116c072cb317785dd33b33",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1701680307,
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"pre-commit-hooks",
"nixpkgs"
]
},
"locked": {
"lastModified": 1703887061,
"narHash": "sha256-gGPa9qWNc6eCXT/+Z5/zMkyYOuRZqeFZBDbopNZQkuY=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "43e1aa1308018f37118e34d3a9cb4f5e75dc11d5",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1708247094,
"narHash": "sha256-H2VS7VwesetGDtIaaz4AMsRkPoSLEVzL/Ika8gnbUnE=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "045b51a3ae66f673ed44b5bbd1f4a341d96703bf",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"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",
"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"
}
}
},
"root": "root",
"version": 7
}
20 changes: 20 additions & 0 deletions devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{ pkgs, ... }:

{
languages.php = {
enable = true;
ini = ''
memory_limit = -1
opcache.enable = 1
opcache.revalidate_freq = 0
opcache.validate_timestamps = 1
opcache.max_accelerated_files = 30000
opcache.memory_consumption = 256M
opcache.interned_strings_buffer = 20
realpath_cache_ttl = 3600
xdebug.idekey = "PHPSTORM"
xdebug.start_with_request = "yes"
date.timezone = "Europe/Paris"
'';
};
}
3 changes: 3 additions & 0 deletions devenv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inputs:
nixpkgs:
url: github:NixOS/nixpkgs/nixpkgs-unstable
9 changes: 5 additions & 4 deletions src/Definition/Example/OperationExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ final class OperationExample

public function __construct(
private string $name,
Operation $parent = null
Operation $parent = null,
?int $statusCode = null,
) {
if ($parent !== null) {
$this->parent = $parent;
}
$this->response = new ResponseExample();
$this->response = new ResponseExample("{$statusCode}");
$this->deepCopy = new DeepCopy();
$this->deepCopy->addTypeFilter(
new ShallowCopyFilter(),
Expand Down Expand Up @@ -122,9 +123,9 @@ public function setBodyContent(array $content): self
return $this;
}

public static function create(string $name, Operation $operation = null): self
public static function create(string $name, ?Operation $operation = null, ?int $statusCode = null): self
{
return new self($name, $operation);
return new self($name, $operation, $statusCode);
}

public function withBody(BodyExample $body): self
Expand Down
15 changes: 12 additions & 3 deletions src/Definition/Loader/OpenApiDefinitionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,15 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
{
$examples = [];

$successStatusCode = null;
if ($operation->responses !== null) {
$successStatusCodes = array_filter(
array_keys($operation->responses->getResponses()),
static fn ($status) => in_array($status, [200, 201, 206], true)
);
$successStatusCode = array_shift($successStatusCodes);
}

foreach ($parameters as $parameter) {
foreach ($parameter->examples ?? [] as $name => $example) {
$operationExample = $this->getExample((string) $name, $examples);
Expand All @@ -365,7 +374,7 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
if (\is_array($example)) {
$example = implode(',', $example);
}
$operationExample = $this->getExample('properties', $examples);
$operationExample = $this->getExample('properties', $examples, $successStatusCode);
$operationExample->setParameter($parameter->name, (string) $example, $parameter->in);
}
}
Expand Down Expand Up @@ -461,10 +470,10 @@ private function getHeaders(array $headers): Parameters
/**
* @param array<OperationExample> $examples
*/
private function getExample(string $name, array &$examples): OperationExample
private function getExample(string $name, array &$examples, ?int $statusCode = null): OperationExample
{
if (!isset($examples[$name])) {
$examples[$name] = new OperationExample($name);
$examples[$name] = new OperationExample($name, null, $statusCode);
}

return $examples[$name];
Expand Down
28 changes: 15 additions & 13 deletions tests/Test/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final class TestCaseTest extends UnitTestCase
{
private ?TestCase $testCase;

public function testGivenValidResponseRegardingSchemaAndShouldValidateSchemaResponseOptionIsDisabledWhenAssertThenNoErrorIsThrown(): void
{
public function testGivenValidResponseRegardingSchemaAndShouldValidateSchemaResponseOptionIsDisabledWhenAssertThenNoErrorIsThrown(
): void {
$this->testCase = $this->givenTestCase(
200,
$this->getValidSchema(),
Expand All @@ -29,8 +29,8 @@ public function testGivenValidResponseRegardingSchemaAndShouldValidateSchemaResp
$this->whenAssert();
}

public function testGivenInvalidValidResponseRegardingSchemaAndShouldValidateSchemaResponseOptionIsDisabledWhenAssertThenNoErrorIsThrown(): void
{
public function testGivenInvalidValidResponseRegardingSchemaAndShouldValidateSchemaResponseOptionIsDisabledWhenAssertThenNoErrorIsThrown(
): void {
$this->testCase = $this->givenTestCase(
200,
$this->getValidSchema(),
Expand All @@ -41,8 +41,8 @@ public function testGivenInvalidValidResponseRegardingSchemaAndShouldValidateSch
$this->whenAssert();
}

public function testGivenInvalidResponseRegardingSchemaAndShouldValidateSchemaResponseOptionIsEnabledWhenAssertThenAnErrorIsThrown(): void
{
public function testGivenInvalidResponseRegardingSchemaAndShouldValidateSchemaResponseOptionIsEnabledWhenAssertThenAnErrorIsThrown(
): void {
$this->expectException(InvalidResponseSchemaException::class);

$this->testCase = $this->givenTestCase(
Expand All @@ -54,8 +54,8 @@ public function testGivenInvalidResponseRegardingSchemaAndShouldValidateSchemaRe
$this->whenAssert();
}

public function testGivenSchemaResponseExistsButForDifferentStatusCodeAndShouldValidateSchemaResponseOptionIsEnabledWhenAssertThenNoErrorIsThrown(): void
{
public function testGivenSchemaResponseExistsButForDifferentStatusCodeAndShouldValidateSchemaResponseOptionIsEnabledWhenAssertThenNoErrorIsThrown(
): void {
$this->testCase = $this->givenTestCase(
201,
$this->getValidSchema(),
Expand All @@ -65,8 +65,8 @@ public function testGivenSchemaResponseExistsButForDifferentStatusCodeAndShouldV
$this->whenAssert();
}

public function testGivenValidResponseRegardingSchemaAndShouldValidateSchemaResponseOptionIsEnabledWhenAssertThenNoErrorIsThrown(): void
{
public function testGivenValidResponseRegardingSchemaAndShouldValidateSchemaResponseOptionIsEnabledWhenAssertThenNoErrorIsThrown(
): void {
$this->testCase = $this->givenTestCase(
200,
$this->getValidSchema(),
Expand Down Expand Up @@ -110,8 +110,8 @@ private function givenTestCase(
$testCase = new TestCase(
'test1',
OperationExample::create(
'test1_example',
Operation::create('test1', '/test1')
name: 'test1_example',
operation: Operation::create('test1', '/test1')
->setResponses(
new Responses([
\APITester\Definition\Response::create($operationResponseStatusCode)
Expand All @@ -120,13 +120,15 @@ private function givenTestCase(
$operationResponseBody
),
])
)
),
statusCode: 200
),
['body'],
$shouldValidateResponseSchema
);

$testCase->setResponse(new Response(200, [], $responseContent));

return $testCase;
}

Expand Down

0 comments on commit de28b13

Please sign in to comment.