Skip to content

Commit c25e693

Browse files
committed
feat: add new option to always fake optionals
1 parent 1334826 commit c25e693

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

src/OpenAPIFaker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function mockComponentSchema(string $schemaName)
145145
}
146146

147147
/**
148-
* @param array{minItems?:?int, maxItems?:?int} $options
148+
* @param array{minItems?:?int, maxItems?:?int, alwaysFakeOptionals?:bool} $options
149149
*/
150150
public function setOptions(array $options): self
151151
{

src/Options.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ final class Options
88
{
99
private ?int $minItems = null;
1010
private ?int $maxItems = null;
11+
private bool $alwaysFakeOptionals = false;
1112

1213
public function setMinItems(int $minItems): Options
1314
{
@@ -23,6 +24,13 @@ public function setMaxItems(int $maxItems): Options
2324
return $this;
2425
}
2526

27+
public function setAlwaysFakeOptionals(bool $alwaysFakeOptionals): self
28+
{
29+
$this->alwaysFakeOptionals = $alwaysFakeOptionals;
30+
31+
return $this;
32+
}
33+
2634
public function getMinItems(): ?int
2735
{
2836
return $this->minItems;
@@ -32,4 +40,9 @@ public function getMaxItems(): ?int
3240
{
3341
return $this->maxItems;
3442
}
43+
44+
public function getAlwaysFakeOptionals(): bool
45+
{
46+
return $this->alwaysFakeOptionals;
47+
}
3548
}

src/SchemaFaker/ObjectFaker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function generate(Schema $schema, Options $options): array
3333
$allPropertyKeys = array_merge($requiredKeys, $selectedOptionalKeys);
3434

3535
foreach ($schema->properties as $key => $property) {
36-
if (! in_array($key, $allPropertyKeys, true)) {
36+
if (! $options->getAlwaysFakeOptionals() && ! in_array($key, $allPropertyKeys, true)) {
3737
continue;
3838
}
3939

tests/Unit/SchemaFaker/ObjectFakerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @uses \Vural\OpenAPIFaker\SchemaFaker\NumberFaker
1616
*
1717
* @covers \Vural\OpenAPIFaker\SchemaFaker\ObjectFaker
18+
* @covers \Vural\OpenAPIFaker\Options
1819
*/
1920
class ObjectFakerTest extends UnitTestCase
2021
{
@@ -100,4 +101,38 @@ function it_includes_required_properties_all_the_time()
100101
self::assertArrayHasKey('username', $fakeData);
101102
$this->assertMatchesJsonSnapshot($fakeData);
102103
}
104+
105+
/** @test */
106+
function it_can_fake_all_properties_if_always_fake_optionals_option_is_set()
107+
{
108+
$options = (new Options())->setAlwaysFakeOptionals(true);
109+
110+
$yaml = <<<YAML
111+
type: object
112+
properties:
113+
id:
114+
type: integer
115+
username:
116+
type: string
117+
name:
118+
type: string
119+
age:
120+
type: integer
121+
birthdate:
122+
type: date
123+
email:
124+
type: string
125+
format: email
126+
required:
127+
- id
128+
- username
129+
YAML;
130+
131+
$fakeData = ObjectFaker::generate(SchemaFactory::fromYaml($yaml), $options);
132+
133+
self::assertIsArray($fakeData);
134+
self::assertCount(6, $fakeData);
135+
136+
$this->assertMatchesJsonSnapshot($fakeData);
137+
}
103138
}

tests/Unit/SchemaFaker/SchemaFakerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* @uses \Vural\OpenAPIFaker\SchemaFaker\StringFaker
1818
* @uses \Vural\OpenAPIFaker\SchemaFaker\NumberFaker
1919
* @uses \Vural\OpenAPIFaker\SchemaFaker\ObjectFaker
20+
* @uses \Vural\OpenAPIFaker\Options
2021
*
2122
* @covers \Vural\OpenAPIFaker\SchemaFaker\SchemaFaker
2223
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": 321051476,
3+
"username": "eaque",
4+
"name": "harum",
5+
"age": -933357512,
6+
"birthdate": [],
7+
"email": "[email protected]"
8+
}

0 commit comments

Comments
 (0)