From e04cde4e889cef5b00972d6833ebc3064fe4f15d Mon Sep 17 00:00:00 2001 From: Geert Broekmans Date: Fri, 26 Jan 2024 15:07:08 +0100 Subject: [PATCH] Add additional test for nested properties (#16) --- tests/openapi.yaml | 30 +++++++++++++++++++++--- tests/unit/OpenApiValidatorTest.php | 36 +++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/tests/openapi.yaml b/tests/openapi.yaml index 6af0ebd..7aa4102 100644 --- a/tests/openapi.yaml +++ b/tests/openapi.yaml @@ -5,7 +5,7 @@ info: servers: - url: 'http://localhost' paths: - "/hello-world": + /hello-world: get: responses: 200: @@ -13,7 +13,18 @@ paths: content: application/json: schema: - $ref: "#/components/schemas/HelloWorld" + $ref: '#/components/schemas/HelloWorld' + + /nested-property: + get: + responses: + 200: + description: Nested property request + content: + application/json: + schema: + $ref: '#/components/schemas/NestedProperty' + components: schemas: HelloWorld: @@ -23,4 +34,17 @@ components: properties: hello: type: string - example: "world" + example: 'world' + + NestedProperty: + type: object + required: + - nested + properties: + nested: + type: object + required: + - property + properties: + property: + type: string diff --git a/tests/unit/OpenApiValidatorTest.php b/tests/unit/OpenApiValidatorTest.php index 07bb278..51ed676 100644 --- a/tests/unit/OpenApiValidatorTest.php +++ b/tests/unit/OpenApiValidatorTest.php @@ -42,7 +42,7 @@ public function testValidatorThrowsErrorWhenRequestIsInvalid(): void ->method('getRequest') ->willReturn($request); - self::expectExceptionObject( + $this->expectExceptionObject( new AssertionFailedError( \sprintf( '%s%s%s', @@ -72,7 +72,7 @@ public function testValidatorThrowsErrorWhenResponseIsInvalid(): void ->method('getResponse') ->willReturn($response); - self::expectExceptionObject( + $this->expectExceptionObject( new AssertionFailedError( \sprintf( '%s%s%s%s%s', @@ -87,4 +87,36 @@ public function testValidatorThrowsErrorWhenResponseIsInvalid(): void self::assertOpenApiSchema('tests/openapi.yaml', $browser); } + + public function testValidatorThrowsErrorWhenNestedResponseIsInvalid(): void + { + $request = Request::create(uri: 'https://localhost/nested-property', server: [ + 'HTTP_X_REQUESTED_WITH', + 'XMLHttpRequest', + ]); + $response = new JsonResponse(['nested' => new \stdClass()], headers: ['content-type' => 'application/json']); + + $browser = $this->createMock(KernelBrowser::class); + $browser->expects(self::once()) + ->method('getRequest') + ->willReturn($request); + $browser->expects(self::once()) + ->method('getResponse') + ->willReturn($response); + + $this->expectExceptionObject( + new AssertionFailedError( + \sprintf( + '%s%s%s%s%s', + 'OpenAPI response error at nested.property:', + "\n", + 'Body does not match schema for content-type "application/json" for Response [get /nested-property 200]', + "\n", + 'Keyword validation failed: Required property \'property\' must be present in the object' + ) + ) + ); + + self::assertOpenApiSchema('tests/openapi.yaml', $browser); + } }