Skip to content

Commit

Permalink
Added support for readonly/writeonly properties
Browse files Browse the repository at this point in the history
  • Loading branch information
lvoogd-infopact committed Sep 21, 2021
1 parent 7c711d7 commit 9bd5b17
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/OpenAPIFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function mockRequest(
throw NoRequest::forPathAndMethodAndContentType($path, $method, $contentType);
}

return (new SchemaFaker($contents[$contentType]->schema, $this->options))->generate();
return (new SchemaFaker($contents[$contentType]->schema, $this->options, true))->generate();
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/SchemaFaker/ObjectFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ObjectFaker
/**
* @return array<mixed>
*/
public static function generate(Schema $schema, Options $options): array
public static function generate(Schema $schema, Options $options, bool $request = false): array
{
$result = [];

Expand All @@ -33,6 +33,12 @@ public static function generate(Schema $schema, Options $options): array
$allPropertyKeys = array_merge($requiredKeys, $selectedOptionalKeys);

foreach ($schema->properties as $key => $property) {
if ($property instanceof Schema) {
if (($request && $property->readOnly) || (! $request && $property->writeOnly)) {
continue;
}
}

if (! $options->getAlwaysFakeOptionals() && ! in_array($key, $allPropertyKeys, true)) {
continue;
}
Expand Down
6 changes: 4 additions & 2 deletions src/SchemaFaker/SchemaFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ final class SchemaFaker
{
private Schema $schema;
private Options $options;
private bool $request;

public function __construct(Schema $schema, Options $options)
public function __construct(Schema $schema, Options $options, bool $request = false)
{
$schemaData = json_decode(json_encode($schema->getSerializableData()), true);
$this->schema = new Schema($this->resolveOfConstraints($schemaData));
$this->options = $options;
$this->request = $request;
}

/**
Expand All @@ -41,7 +43,7 @@ public function generate()
}

if ($this->schema->type === 'object') {
return ObjectFaker::generate($this->schema, $this->options);
return ObjectFaker::generate($this->schema, $this->options, $this->request);
}

if ($this->schema->type === 'string') {
Expand Down
60 changes: 60 additions & 0 deletions tests/Unit/SchemaFaker/ObjectFakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,64 @@ function it_can_fake_all_properties_if_always_fake_optionals_option_is_set()

$this->assertMatchesJsonSnapshot($fakeData);
}

/** @test */
function it_does_not_inlcude_readonly_properties_when_type_is_request()
{
$yaml = <<<YAML
type: object
properties:
id:
type: integer
readOnly: true
username:
type: string
password:
type: string
writeOnly: true
required:
- id
- username
- password
YAML;

$fakeData = ObjectFaker::generate(SchemaFactory::fromYaml($yaml), $this->options, true);

self::assertIsArray($fakeData);
self::assertArrayNotHasKey('id', $fakeData);
self::assertArrayHasKey('username', $fakeData);
self::assertArrayHasKey('password', $fakeData);

$this->assertMatchesJsonSnapshot($fakeData);
}

/** @test */
function it_does_not_inlcude_writeonly_properties_when_type_is_response()
{
$yaml = <<<YAML
type: object
properties:
id:
type: integer
readOnly: true
username:
type: string
password:
type: string
writeOnly: true
required:
- id
- username
- password
YAML;

$fakeData = ObjectFaker::generate(SchemaFactory::fromYaml($yaml), $this->options);

self::assertIsArray($fakeData);
self::assertArrayHasKey('id', $fakeData);
self::assertArrayHasKey('username', $fakeData);
self::assertArrayNotHasKey('password', $fakeData);

$this->assertMatchesJsonSnapshot($fakeData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"username": "laborum",
"password": "in"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": 488690146,
"username": "in"
}

0 comments on commit 9bd5b17

Please sign in to comment.