Skip to content

Commit 9329d50

Browse files
authored
Prepare 2.1.0 release (#123)
1 parent 101dab3 commit 9329d50

28 files changed

+230
-225
lines changed

composer.json

+2-12
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
"phpstan/phpstan": "^1.9",
2626
"phpstan/phpstan-phpunit": "^1",
2727
"phpunit/phpunit": "^9.5",
28+
"rector/rector": "^0.14.7",
2829
"spatie/phpunit-snapshot-assertions": "^4.2",
29-
"symfony/var-dumper": "^5.4",
30+
"symfony/var-dumper": "^6",
3031
"thecodingmachine/phpstan-safe-rule": "^1.2"
3132
},
3233
"prefer-stable": true,
@@ -51,17 +52,6 @@
5152
"sort-packages": true
5253
},
5354
"scripts": {
54-
"check": [
55-
"@codestyle",
56-
"@phpstan",
57-
"@test"
58-
],
59-
"ci:check": [
60-
"@codestyle",
61-
"@phpstan",
62-
"@test-coverage",
63-
"@test-mutation"
64-
],
6555
"codestyle": "vendor/bin/phpcs",
6656
"phpstan": "vendor/bin/phpstan analyse --ansi",
6757
"test": "vendor/bin/phpunit",

infection.json.dist

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
"@default": true,
1212
"global-ignore": [
1313
"Vural\\OpenAPIFaker\\SchemaFaker\\BooleanFaker",
14-
"Vural\\OpenAPIFaker\\Utils\\RegexUtils::generateSample",
15-
"Vural\\OpenAPIFaker\\Utils\\StringUtils::convertToBinary",
16-
"Vural\\OpenAPIFaker\\Utils\\StringUtils::ensureLength",
17-
"Vural\\OpenAPIFaker\\Utils\\NumberUtils::ensureRange"
14+
"Vural\\OpenAPIFaker\\Utils\\RegexUtils",
1815
],
1916
"LessThan": {
2017
"ignore": [

phpstan-baseline.neon

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: "#^Method Vural\\\\OpenAPIFaker\\\\OpenAPIFaker\\:\\:findComponentSchema\\(\\) should return cebe\\\\openapi\\\\spec\\\\Schema but returns cebe\\\\openapi\\\\spec\\\\Reference\\|cebe\\\\openapi\\\\spec\\\\Schema\\.$#"
5+
count: 1
6+
path: src/OpenAPIFaker.php
7+
38
-
49
message: "#^Parameter \\#1 \\$schema of class Vural\\\\OpenAPIFaker\\\\SchemaFaker\\\\SchemaFaker constructor expects cebe\\\\openapi\\\\spec\\\\Schema, cebe\\\\openapi\\\\spec\\\\Reference\\|cebe\\\\openapi\\\\spec\\\\Schema\\|null given\\.$#"
510
count: 1
6-
path: src/SchemaFaker/RequestFaker.php
7-
11+
path: src/SchemaFaker/ArrayFaker.php
12+
813
-
914
message: "#^Parameter \\#1 \\$schema of class Vural\\\\OpenAPIFaker\\\\SchemaFaker\\\\SchemaFaker constructor expects cebe\\\\openapi\\\\spec\\\\Schema, cebe\\\\openapi\\\\spec\\\\Reference\\|cebe\\\\openapi\\\\spec\\\\Schema\\|null given\\.$#"
1015
count: 1
11-
path: src/SchemaFaker/ResponseFaker.php
16+
path: src/SchemaFaker/RequestFaker.php
1217

1318
-
1419
message: "#^Parameter \\#1 \\$schema of class Vural\\\\OpenAPIFaker\\\\SchemaFaker\\\\SchemaFaker constructor expects cebe\\\\openapi\\\\spec\\\\Schema, cebe\\\\openapi\\\\spec\\\\Reference\\|cebe\\\\openapi\\\\spec\\\\Schema\\|null given\\.$#"
1520
count: 1
16-
path: src/SchemaFaker/ArrayFaker.php
21+
path: src/SchemaFaker/ResponseFaker.php

rector.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
6+
use Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector;
7+
use Rector\Config\RectorConfig;
8+
use Rector\Set\ValueObject\LevelSetList;
9+
use Rector\Set\ValueObject\SetList;
10+
use Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector;
11+
12+
return static function (RectorConfig $rectorConfig): void {
13+
$rectorConfig->paths([
14+
__DIR__ . '/src',
15+
]);
16+
17+
$rectorConfig->sets([
18+
LevelSetList::UP_TO_PHP_80,
19+
SetList::CODE_QUALITY,
20+
SetList::DEAD_CODE,
21+
SetList::TYPE_DECLARATION,
22+
SetList::TYPE_DECLARATION_STRICT,
23+
SetList::CODING_STYLE,
24+
SetList::PRIVATIZATION,
25+
]);
26+
27+
$rectorConfig->skip([
28+
VarConstantCommentRector::class,
29+
]);
30+
};

src/Exception/NoContentType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
use Exception;
88

9-
class NoContentType extends Exception
9+
final class NoContentType extends Exception
1010
{
1111
}

src/Exception/NoExample.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,17 @@
66

77
use Exception;
88

9-
use function Safe\sprintf;
9+
use function sprintf;
1010

11-
class NoExample extends Exception
11+
final class NoExample extends Exception
1212
{
13-
protected string $example;
14-
1513
public static function forRequest(string $example): self
1614
{
17-
$e = new self(sprintf('OpenAPI spec does not have a example "%s" request', $example));
18-
$e->example = $example;
19-
20-
return $e;
15+
return new self(sprintf('OpenAPI spec does not have an example "%s" request', $example));
2116
}
2217

2318
public static function forResponse(string $example): self
2419
{
25-
$e = new self(sprintf('OpenAPI spec does not have a example "%s" response', $example));
26-
$e->example = $example;
27-
28-
return $e;
20+
return new self(sprintf('OpenAPI spec does not have an example "%s" response', $example));
2921
}
3022
}

src/Exception/NoPath.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@
66

77
use Exception;
88

9-
use function Safe\sprintf;
9+
use function sprintf;
1010

11-
class NoPath extends Exception
11+
final class NoPath extends Exception
1212
{
13-
protected string $method;
14-
protected string $path;
15-
1613
public static function forPathAndMethod(string $path, string $method): self
1714
{
18-
$e = new self(sprintf('OpenAPI spec does not have a path for %s %s', $method, $path));
19-
$e->path = $path;
20-
$e->method = $method;
21-
22-
return $e;
15+
return new self(sprintf('OpenAPI spec does not have a path for %s %s', $method, $path));
2316
}
2417
}

src/Exception/NoRequest.php

+4-17
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,17 @@
66

77
use Exception;
88

9-
use function Safe\sprintf;
9+
use function sprintf;
1010

11-
class NoRequest extends Exception
11+
final class NoRequest extends Exception
1212
{
13-
protected string $method;
14-
protected string $path;
15-
protected string $contentType;
16-
1713
public static function forPathAndMethod(string $path, string $method): self
1814
{
19-
$e = new self(sprintf('OpenAPI spec does not have a response for %s %s', $method, $path));
20-
$e->path = $path;
21-
$e->method = $method;
22-
23-
return $e;
15+
return new self(sprintf('OpenAPI spec does not have a response for %s %s', $method, $path));
2416
}
2517

2618
public static function forPathAndMethodAndContentType(string $path, string $method, string $contentType): self
2719
{
28-
$e = new self(sprintf('OpenAPI spec does not have a response for %s %s', $method, $path));
29-
$e->path = $path;
30-
$e->method = $method;
31-
$e->contentType = $contentType;
32-
33-
return $e;
20+
return new self(sprintf('OpenAPI spec does not have a response for %s %s', $method, $path));
3421
}
3522
}

src/Exception/NoResponse.php

+4-17
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,17 @@
66

77
use Exception;
88

9-
use function Safe\sprintf;
9+
use function sprintf;
1010

11-
class NoResponse extends Exception
11+
final class NoResponse extends Exception
1212
{
13-
protected string $method;
14-
protected string $path;
15-
protected string|null $statusCode = null;
16-
1713
public static function forPathAndMethod(string $path, string $method): self
1814
{
19-
$e = new self(sprintf('OpenAPI spec does not have a response for %s %s', $method, $path));
20-
$e->path = $path;
21-
$e->method = $method;
22-
23-
return $e;
15+
return new self(sprintf('OpenAPI spec does not have a response for %s %s', $method, $path));
2416
}
2517

2618
public static function forPathAndMethodAndStatusCode(string $path, string $method, string $statusCode): self
2719
{
28-
$e = new self(sprintf('OpenAPI spec does not have a response for status code %s at %s %s', $statusCode, $method, $path));
29-
$e->path = $path;
30-
$e->method = $method;
31-
$e->statusCode = $statusCode;
32-
33-
return $e;
20+
return new self(sprintf('OpenAPI spec does not have a response for status code %s at %s %s', $statusCode, $method, $path));
3421
}
3522
}

src/Exception/NoSchema.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Exception;
88

9-
use function Safe\sprintf;
9+
use function sprintf;
1010

11-
class NoSchema extends Exception
11+
final class NoSchema extends Exception
1212
{
1313
public string $name;
1414

src/OpenAPIFaker.php

+30-12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
final class OpenAPIFaker
3131
{
3232
private OpenApi $openAPISchema;
33+
3334
private Options $options;
3435

3536
/** @codeCoverageIgnore */
@@ -44,7 +45,7 @@ private function __construct()
4445
*/
4546
public static function createFromJson(string $json): self
4647
{
47-
$instance = new static();
48+
$instance = new self();
4849
$instance->openAPISchema = (new LeagueOpenAPI\SchemaFactory\JsonFactory($json))->createSchema();
4950

5051
return $instance;
@@ -56,15 +57,15 @@ public static function createFromJson(string $json): self
5657
*/
5758
public static function createFromYaml(string $yaml): self
5859
{
59-
$instance = new static();
60+
$instance = new self();
6061
$instance->openAPISchema = (new LeagueOpenAPI\SchemaFactory\YamlFactory($yaml))->createSchema();
6162

6263
return $instance;
6364
}
6465

6566
public static function createFromSchema(OpenApi $schema): self
6667
{
67-
$instance = new static();
68+
$instance = new self();
6869
$instance->openAPISchema = $schema;
6970

7071
return $instance;
@@ -135,18 +136,21 @@ public function mockResponseForExample(
135136
/** @throws Exception */
136137
public function mockComponentSchema(string $schemaName): mixed
137138
{
138-
if ($this->openAPISchema->components === null) {
139-
throw NoSchema::forZeroComponents();
140-
}
139+
$schema = $this->findComponentSchema($schemaName);
141140

142-
if (! array_key_exists($schemaName, $this->openAPISchema->components->schemas)) {
143-
throw NoSchema::forComponentName($schemaName);
144-
}
141+
return (new SchemaFaker($schema, $this->options))->generate();
142+
}
145143

146-
/** @var Schema $schema */
147-
$schema = $this->openAPISchema->components->schemas[$schemaName];
144+
/** @throws Exception */
145+
public function mockComponentSchemaForExample(string $schemaName): mixed
146+
{
147+
$schema = $this->findComponentSchema($schemaName);
148148

149-
return (new SchemaFaker($schema, $this->options))->generate();
149+
if ($schema->example === null) {
150+
throw new NoExample();
151+
}
152+
153+
return $schema->example;
150154
}
151155

152156
/** @param array{minItems?:?int, maxItems?:?int, alwaysFakeOptionals?:bool, strategy?:string} $options */
@@ -238,4 +242,18 @@ private function findContentForResponse(
238242

239243
return $content;
240244
}
245+
246+
/** @throws NoSchema */
247+
private function findComponentSchema(string $schemaName): Schema
248+
{
249+
if ($this->openAPISchema->components === null) {
250+
throw NoSchema::forZeroComponents();
251+
}
252+
253+
if (! array_key_exists($schemaName, $this->openAPISchema->components->schemas)) {
254+
throw NoSchema::forComponentName($schemaName);
255+
}
256+
257+
return $this->openAPISchema->components->schemas[$schemaName];
258+
}
241259
}

src/Options.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77
use InvalidArgumentException;
88

99
use function in_array;
10-
use function Safe\sprintf;
10+
use function sprintf;
1111

1212
final class Options
1313
{
14-
public const STRATEGY_STATIC = 'static';
14+
public const STRATEGY_STATIC = 'static';
15+
1516
public const STRATEGY_DYNAMIC = 'dynamic';
1617

17-
private int|null $minItems = null;
18-
private int|null $maxItems = null;
18+
private int|null $minItems = null;
19+
20+
private int|null $maxItems = null;
21+
1922
private bool $alwaysFakeOptionals = false;
20-
private string $strategy = self::STRATEGY_DYNAMIC;
23+
24+
private string $strategy = self::STRATEGY_DYNAMIC;
25+
26+
private const ALLOWED = [self::STRATEGY_STATIC, self::STRATEGY_DYNAMIC];
2127

2228
public function setMinItems(int $minItems): Options
2329
{
@@ -43,9 +49,7 @@ public function setAlwaysFakeOptionals(bool $alwaysFakeOptionals): Options
4349
/** @throws InvalidArgumentException */
4450
public function setStrategy(string $strategy): Options
4551
{
46-
$allowed = [self::STRATEGY_STATIC, self::STRATEGY_DYNAMIC];
47-
48-
if (! in_array($strategy, $allowed, true)) {
52+
if (! in_array($strategy, self::ALLOWED, true)) {
4953
throw new InvalidArgumentException(sprintf('Unknown generation strategy: %s', $strategy));
5054
}
5155

src/SchemaFaker/ArrayFaker.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ public static function generate(Schema $schema, Options $options): array
5050

5151
$itemSchema = new SchemaFaker($schema->items, $options);
5252

53-
for ($i = 0; $i < $itemSize; $i++) {
53+
for ($i = 0; $i < $itemSize; ++$i) {
5454
$fakeData[] = $itemSchema->generate();
5555

56-
if ($schema->uniqueItems !== true) {
56+
if (! $schema->uniqueItems) {
5757
continue;
5858
}
5959

6060
$uniqueData = array_unique($fakeData, is_array($fakeData[0]) ? SORT_REGULAR : SORT_STRING);
6161

62-
if (count($uniqueData) >= count($fakeData)) {
62+
if (count($uniqueData) > count($fakeData)) {
6363
continue;
6464
}
6565

0 commit comments

Comments
 (0)