Skip to content

Commit 50473b5

Browse files
committed
feat: add support for options
1 parent 7998e14 commit 50473b5

17 files changed

+337
-37
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"require-dev": {
2121
"doctrine/coding-standard": "^8.1",
2222
"ergebnis/composer-normalize": "^2.9",
23-
"infection/infection": "^0.18",
23+
"infection/infection": "^0.20",
2424
"phpstan/extension-installer": "^1.0",
2525
"phpstan/phpstan": "^0.12.50",
2626
"phpstan/phpstan-phpunit": "^0.12.16",

infection.json.dist

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@
1414
"global-ignore": [
1515
"Vural\\OpenAPIFaker\\SchemaFaker\\BooleanFaker"
1616
],
17+
"LessThan": {
18+
"ignoreSourceCodeByRegex": [
19+
".*\\$minimum.*",
20+
".*\\$maximum.*"
21+
]
22+
},
23+
"GreaterThan": {
24+
"ignoreSourceCodeByRegex": [
25+
".*\\$minimum.*",
26+
".*\\$maximum.*"
27+
]
28+
},
1729
"@default": true
1830
},
19-
"timeout": 10
31+
"timeout": 20
2032
}

src/OpenAPIFaker.php

+22-3
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
use Vural\OpenAPIFaker\SchemaFaker\SchemaFaker;
2222

2323
use function array_key_exists;
24+
use function method_exists;
2425
use function strtolower;
2526

2627
final class OpenAPIFaker
2728
{
2829
private OpenApi $openAPISchema;
30+
private Options $options;
2931

3032
/** @codeCoverageIgnore */
3133
private function __construct()
3234
{
35+
$this->options = new Options();
3336
}
3437

3538
/**
@@ -81,7 +84,7 @@ public function mockRequest(
8184
throw NoRequest::forPathAndMethodAndContentType($path, $method, $contentType);
8285
}
8386

84-
return (new SchemaFaker($contents[$contentType]->schema))->generate();
87+
return (new SchemaFaker($contents[$contentType]->schema, $this->options))->generate();
8588
}
8689

8790
/**
@@ -117,7 +120,7 @@ public function mockResponse(
117120
/** @var MediaType $content */
118121
$content = $contents[$contentType];
119122

120-
return (new SchemaFaker($content->schema))->generate();
123+
return (new SchemaFaker($content->schema, $this->options))->generate();
121124
}
122125

123126
/**
@@ -138,7 +141,23 @@ public function mockComponentSchema(string $schemaName)
138141
/** @var Schema $schema */
139142
$schema = $this->openAPISchema->components->schemas[$schemaName];
140143

141-
return (new SchemaFaker($schema))->generate();
144+
return (new SchemaFaker($schema, $this->options))->generate();
145+
}
146+
147+
/**
148+
* @param array{minItems?:?int, maxItems?:?int} $options
149+
*/
150+
public function setOptions(array $options): self
151+
{
152+
foreach ($options as $key => $value) {
153+
if (! method_exists($this->options, 'set' . $key)) {
154+
continue;
155+
}
156+
157+
$this->options->{'set' . $key}($value);
158+
}
159+
160+
return $this;
142161
}
143162

144163
/**

src/Options.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vural\OpenAPIFaker;
6+
7+
final class Options
8+
{
9+
private ?int $minItems = null;
10+
private ?int $maxItems = null;
11+
12+
public function setMinItems(int $minItems): Options
13+
{
14+
$this->minItems = $minItems;
15+
16+
return $this;
17+
}
18+
19+
public function setMaxItems(int $maxItems): Options
20+
{
21+
$this->maxItems = $maxItems;
22+
23+
return $this;
24+
}
25+
26+
public function getMinItems(): ?int
27+
{
28+
return $this->minItems;
29+
}
30+
31+
public function getMaxItems(): ?int
32+
{
33+
return $this->maxItems;
34+
}
35+
}

src/SchemaFaker/ArrayFaker.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use cebe\openapi\spec\Schema;
88
use Faker\Provider\Base;
9+
use Vural\OpenAPIFaker\Options;
910

1011
use function array_unique;
1112
use function count;
@@ -18,15 +19,31 @@ final class ArrayFaker
1819
/**
1920
* @return array<mixed>
2021
*/
21-
public static function generate(Schema $schema): array
22+
public static function generate(Schema $schema, Options $options): array
2223
{
23-
$minimum = $schema->minItems ?? 0;
24-
$maximum = $schema->maxItems ?? $minimum + 15;
24+
$minimum = $schema->minItems ?? 0;
25+
$maximum = $schema->maxItems ?? $minimum + 15;
26+
27+
if ($options->getMinItems() && $minimum < $options->getMinItems()) {
28+
/** @var int $minimum */
29+
$minimum = $options->getMinItems();
30+
}
31+
32+
if ($options->getMaxItems() && $maximum > $options->getMaxItems()) {
33+
/** @var int $maximum */
34+
$maximum = $options->getMaxItems();
35+
36+
// Don't allow user to set min items above our maximum
37+
if ($minimum > $maximum) {
38+
$minimum = $maximum;
39+
}
40+
}
41+
2542
$itemSize = Base::numberBetween($minimum, $maximum);
2643

2744
$fakeData = [];
2845

29-
$itemSchema = new SchemaFaker($schema->items);
46+
$itemSchema = new SchemaFaker($schema->items, $options);
3047

3148
for ($i = 0; $i < $itemSize; $i++) {
3249
$fakeData[] = $itemSchema->generate();

src/SchemaFaker/ObjectFaker.php

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

77
use cebe\openapi\spec\Schema;
88
use Faker\Provider\Base;
9+
use Vural\OpenAPIFaker\Options;
910

1011
use function array_diff;
1112
use function array_keys;
@@ -21,7 +22,7 @@ final class ObjectFaker
2122
/**
2223
* @return array<mixed>
2324
*/
24-
public static function generate(Schema $schema): array
25+
public static function generate(Schema $schema, Options $options): array
2526
{
2627
$result = [];
2728

@@ -36,7 +37,7 @@ public static function generate(Schema $schema): array
3637
continue;
3738
}
3839

39-
$result[$key] = (new SchemaFaker($property))->generate();
40+
$result[$key] = (new SchemaFaker($property, $options))->generate();
4041
}
4142

4243
return $result;

src/SchemaFaker/SchemaFaker.php

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

77
use cebe\openapi\spec\Schema;
88
use Faker\Provider\Base;
9+
use Vural\OpenAPIFaker\Options;
910

1011
use function array_key_exists;
1112
use function array_reverse;
@@ -21,11 +22,13 @@
2122
final class SchemaFaker
2223
{
2324
private Schema $schema;
25+
private Options $options;
2426

25-
public function __construct(Schema $schema)
27+
public function __construct(Schema $schema, Options $options)
2628
{
27-
$schemaData = json_decode(json_encode($schema->getSerializableData()), true);
28-
$this->schema = new Schema($this->resolveOfConstraints($schemaData));
29+
$schemaData = json_decode(json_encode($schema->getSerializableData()), true);
30+
$this->schema = new Schema($this->resolveOfConstraints($schemaData));
31+
$this->options = $options;
2932
}
3033

3134
/**
@@ -34,11 +37,11 @@ public function __construct(Schema $schema)
3437
public function generate()
3538
{
3639
if ($this->schema->type === 'array') {
37-
return ArrayFaker::generate($this->schema);
40+
return ArrayFaker::generate($this->schema, $this->options);
3841
}
3942

4043
if ($this->schema->type === 'object') {
41-
return ObjectFaker::generate($this->schema);
44+
return ObjectFaker::generate($this->schema, $this->options);
4245
}
4346

4447
if ($this->schema->type === 'string') {
@@ -54,7 +57,7 @@ public function generate()
5457
}
5558

5659
if ($this->schema->properties !== null) {
57-
return ObjectFaker::generate($this->schema);
60+
return ObjectFaker::generate($this->schema, $this->options);
5861
}
5962

6063
return [];

tests/Integration/OpenAPIFakerTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,40 @@ function it_will_throw_exception_if_schema_does_not_exist()
438438
$faker->mockComponentSchema('DummySchema');
439439
}
440440

441+
/**
442+
* @uses \Vural\OpenAPIFaker\SchemaFaker\NumberFaker
443+
* @uses \Vural\OpenAPIFaker\SchemaFaker\SchemaFaker
444+
* @uses \Vural\OpenAPIFaker\OpenAPIFaker::createFromYaml
445+
* @uses \Vural\OpenAPIFaker\OpenAPIFaker::mockComponentSchema
446+
*
447+
* @test
448+
* @covers \Vural\OpenAPIFaker\Options
449+
* @covers \Vural\OpenAPIFaker\OpenAPIFaker::setOptions
450+
* @covers \Vural\OpenAPIFaker\SchemaFaker\ArrayFaker
451+
*/
452+
function it_can_set_options()
453+
{
454+
$specYaml = <<<YAML
455+
openapi: 3.0.2
456+
components:
457+
schemas:
458+
Dummy:
459+
type: array
460+
items:
461+
type: integer
462+
minItems: 3
463+
YAML;
464+
465+
$fakeData = OpenAPIFaker::createFromYaml($specYaml)->setOptions([
466+
'minItems' => 5,
467+
'notExistingOption' => 'foo',
468+
'maxItems' => 6,
469+
])->mockComponentSchema('Dummy');
470+
471+
self::assertGreaterThanOrEqual(5, count($fakeData));
472+
self::assertLessThanOrEqual(6, count($fakeData));
473+
}
474+
441475
private function getTodosSpec(): string
442476
{
443477
return <<<'YAML'

0 commit comments

Comments
 (0)