From 4de1b1b61d491e648ebde4a3d7986c7958bc5268 Mon Sep 17 00:00:00 2001 From: bjorn Date: Wed, 18 Aug 2021 15:37:21 +0200 Subject: [PATCH] Add test to test for correct ToMany relationship patch body. --- tests/Feature/GenerateTest.php | 16 +----- tests/Feature/OpenApiSchemaTest.php | 57 +++++++++++++++++++ tests/Support/JsonApi/V1/Posts/PostSchema.php | 2 +- 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 tests/Feature/OpenApiSchemaTest.php diff --git a/tests/Feature/GenerateTest.php b/tests/Feature/GenerateTest.php index 38cf334..ec70f2b 100644 --- a/tests/Feature/GenerateTest.php +++ b/tests/Feature/GenerateTest.php @@ -35,6 +35,8 @@ public function test_spec_is_json() $spec = json_decode($output, true); + file_put_contents('output.json', $output); + $this->assertEquals('My JSON:API', $spec['info']['title']); } @@ -49,18 +51,4 @@ public function test_spec_file_generated() $this->assertEquals('My JSON:API', $spec['info']['title']); } - public function test_url_is_properly_parsed() - { - GeneratorFacade::generate('v1'); - - $openapiYaml = GeneratorFacade::generate('v1'); - - $spec = Yaml::parse($openapiYaml); - - $this->assertArrayHasKey('/posts', $spec['paths'], 'Path to resource is not replaced correctly.'); - - $this->assertArrayHasKey('/posts/{post}/relationships/author', $spec['paths'], 'Path to resource is not replaced correctly.'); - - $this->assertEquals('http://localhost/api/v1', $spec['servers'][0]['variables']['serverUrl']['default']); - } } diff --git a/tests/Feature/OpenApiSchemaTest.php b/tests/Feature/OpenApiSchemaTest.php new file mode 100644 index 0000000..a63de76 --- /dev/null +++ b/tests/Feature/OpenApiSchemaTest.php @@ -0,0 +1,57 @@ +seed(DatabaseSeeder::class); + + $output = GeneratorFacade::generate('v1', 'json'); + $this->spec = json_decode($output, true); + + + } + + public function test_url_is_properly_parsed() + { + $this->assertArrayHasKey('/posts', $this->spec['paths'], 'Path to resource is not replaced correctly.'); + + $this->assertArrayHasKey('/posts/{post}/relationships/author', $this->spec['paths'], 'Path to resource is not replaced correctly.'); + + $this->assertEquals('http://localhost/api/v1', $this->spec['servers'][0]['variables']['serverUrl']['default']); + } + + public function test_has_many_should_be_array_of_reg() + { + $this->assertEquals( + $this->spec['paths']['/posts/{post}/relationships/tags']['patch']['requestBody']['content']['application/vnd.api+json']['schema']['properties']['data'], + [ + 'type' => 'array', + 'items' => [ + '$ref' => '#/components/schemas/resources.posts.relationship.tags.update', + ] + ]); + $this->assertEquals( + $this->spec['paths']['/posts/{post}/relationships/tags']['post']['requestBody']['content']['application/vnd.api+json']['schema']['properties']['data'], + [ + 'type' => 'array', + 'items' => [ + '$ref' => '#/components/schemas/resources.posts.relationship.tags.update', + ] + ]); + } +} diff --git a/tests/Support/JsonApi/V1/Posts/PostSchema.php b/tests/Support/JsonApi/V1/Posts/PostSchema.php index b175a7e..9c8edb8 100644 --- a/tests/Support/JsonApi/V1/Posts/PostSchema.php +++ b/tests/Support/JsonApi/V1/Posts/PostSchema.php @@ -69,7 +69,7 @@ public function fields(): array return [ HashId::make()->alreadyHashed(), BelongsTo::make('author')->type('users')->readOnly(), - HasMany::make('comments')->readOnly(), + HasMany::make('comments'), Str::make('content'), DateTime::make('createdAt')->sortable()->readOnly(), SoftDelete::make('deletedAt')->sortable(),