Skip to content

Commit

Permalink
Add additional test for nested properties (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gertjuhh committed Jan 26, 2024
1 parent 8207642 commit e04cde4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
30 changes: 27 additions & 3 deletions tests/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ info:
servers:
- url: 'http://localhost'
paths:
"/hello-world":
/hello-world:
get:
responses:
200:
description: Hello world request
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:
Expand All @@ -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
36 changes: 34 additions & 2 deletions tests/unit/OpenApiValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testValidatorThrowsErrorWhenRequestIsInvalid(): void
->method('getRequest')
->willReturn($request);

self::expectExceptionObject(
$this->expectExceptionObject(
new AssertionFailedError(
\sprintf(
'%s%s%s',
Expand Down Expand Up @@ -72,7 +72,7 @@ public function testValidatorThrowsErrorWhenResponseIsInvalid(): void
->method('getResponse')
->willReturn($response);

self::expectExceptionObject(
$this->expectExceptionObject(
new AssertionFailedError(
\sprintf(
'%s%s%s%s%s',
Expand All @@ -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);
}
}

0 comments on commit e04cde4

Please sign in to comment.