diff --git a/src/OpenAPIFaker.php b/src/OpenAPIFaker.php index 5d86e78..0727ce1 100644 --- a/src/OpenAPIFaker.php +++ b/src/OpenAPIFaker.php @@ -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(); } /** diff --git a/src/SchemaFaker/ObjectFaker.php b/src/SchemaFaker/ObjectFaker.php index 6f95979..83b8816 100644 --- a/src/SchemaFaker/ObjectFaker.php +++ b/src/SchemaFaker/ObjectFaker.php @@ -22,7 +22,7 @@ final class ObjectFaker /** * @return array */ - public static function generate(Schema $schema, Options $options): array + public static function generate(Schema $schema, Options $options, bool $request = false): array { $result = []; @@ -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; } diff --git a/src/SchemaFaker/SchemaFaker.php b/src/SchemaFaker/SchemaFaker.php index a2404d0..008da87 100644 --- a/src/SchemaFaker/SchemaFaker.php +++ b/src/SchemaFaker/SchemaFaker.php @@ -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; } /** @@ -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') { diff --git a/tests/Unit/SchemaFaker/ObjectFakerTest.php b/tests/Unit/SchemaFaker/ObjectFakerTest.php index 521268d..1d1f3c2 100644 --- a/tests/Unit/SchemaFaker/ObjectFakerTest.php +++ b/tests/Unit/SchemaFaker/ObjectFakerTest.php @@ -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 = <<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 = <<options); + + self::assertIsArray($fakeData); + self::assertArrayHasKey('id', $fakeData); + self::assertArrayHasKey('username', $fakeData); + self::assertArrayNotHasKey('password', $fakeData); + + $this->assertMatchesJsonSnapshot($fakeData); + } } diff --git a/tests/Unit/SchemaFaker/__snapshots__/ObjectFakerTest__it_does_not_inlcude_readonly_properties_when_type_is_request__1.json b/tests/Unit/SchemaFaker/__snapshots__/ObjectFakerTest__it_does_not_inlcude_readonly_properties_when_type_is_request__1.json new file mode 100644 index 0000000..6900424 --- /dev/null +++ b/tests/Unit/SchemaFaker/__snapshots__/ObjectFakerTest__it_does_not_inlcude_readonly_properties_when_type_is_request__1.json @@ -0,0 +1,4 @@ +{ + "username": "laborum", + "password": "in" +} diff --git a/tests/Unit/SchemaFaker/__snapshots__/ObjectFakerTest__it_does_not_inlcude_writeonly_properties_when_type_is_response__1.json b/tests/Unit/SchemaFaker/__snapshots__/ObjectFakerTest__it_does_not_inlcude_writeonly_properties_when_type_is_response__1.json new file mode 100644 index 0000000..f60b676 --- /dev/null +++ b/tests/Unit/SchemaFaker/__snapshots__/ObjectFakerTest__it_does_not_inlcude_writeonly_properties_when_type_is_response__1.json @@ -0,0 +1,4 @@ +{ + "id": 488690146, + "username": "in" +}