From c843243488dd31e32100444161e6b2e2321c5325 Mon Sep 17 00:00:00 2001 From: Arinzechukwu Date: Wed, 13 Sep 2023 15:50:58 +0100 Subject: [PATCH] Added test to confirm referenced parameters are compiling to arrays --- tests/spec/PathTest.php | 31 ++++++++++ tests/spec/data/path-params/global.yaml | 28 +++++++++ tests/spec/data/path-params/openapi.yaml | 18 ++++++ tests/spec/data/path-params/user.yaml | 76 ++++++++++++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 tests/spec/data/path-params/global.yaml create mode 100644 tests/spec/data/path-params/openapi.yaml create mode 100644 tests/spec/data/path-params/user.yaml diff --git a/tests/spec/PathTest.php b/tests/spec/PathTest.php index 6c46e4b..e5be1bf 100644 --- a/tests/spec/PathTest.php +++ b/tests/spec/PathTest.php @@ -192,4 +192,35 @@ public function testPathItemReference() $this->assertEquals('A bar', $barPath->get->responses['200']->description); $this->assertEquals('non-existing resource', $barPath->get->responses['404']->description); } + + public function testPathParametersAreArrays() + { + $file = __DIR__ . '/data/path-params/openapi.yaml'; + /** @var $openapi \cebe\openapi\spec\OpenApi */ + $openapi = Reader::readFromYamlFile($file, \cebe\openapi\spec\OpenApi::class, true); + + $result = $openapi->validate(); + $this->assertEquals([], $openapi->getErrors(), print_r($openapi->getErrors(), true)); + $this->assertTrue($result); + + $this->assertInstanceOf(Paths::class, $openapi->paths); + $this->assertIsArray($openapi->paths->getPaths()); + $this->assertInstanceOf(PathItem::class, $usersPath = $openapi->paths['/v1/{organizationId}/user']); + $this->assertInstanceOf(PathItem::class, $userIdPath = $openapi->paths['/v1/{organizationId}/user/{id}']); + + $result = $usersPath->validate(); + $this->assertTrue($result); + $this->assertIsArray($usersPath->parameters); + $this->assertInstanceOf(\cebe\openapi\spec\Parameter::class, $usersPath->parameters[0]); + $this->assertInstanceOf(\cebe\openapi\spec\Parameter::class, $usersPath->parameters[1]); + $this->assertEquals($usersPath->parameters[0]->name, 'api-version'); + + $result = $userIdPath->validate(); + $this->assertTrue($result); + $this->assertIsArray($userIdPath->parameters); + $this->assertInstanceOf(\cebe\openapi\spec\Parameter::class, $userIdPath->parameters[0]); + $this->assertInstanceOf(\cebe\openapi\spec\Parameter::class, $userIdPath->parameters[1]); + $this->assertEquals($userIdPath->parameters[2]->name, 'id'); + + } } diff --git a/tests/spec/data/path-params/global.yaml b/tests/spec/data/path-params/global.yaml new file mode 100644 index 0000000..38348ee --- /dev/null +++ b/tests/spec/data/path-params/global.yaml @@ -0,0 +1,28 @@ +components: + parameters: + Version: + in: header + name: api-version + required: false + schema: + type: string + format: date + example: '2021-05-18' + description: The API version + OrganizationId: + in: path + name: organizationId + required: true + schema: + type: string + format: uuid + description: The Organization ID + responses: + BadRequest: + description: Bad Request + Forbidden: + description: Forbidden + NotFound: + description: Not Found + Success: + description: Success \ No newline at end of file diff --git a/tests/spec/data/path-params/openapi.yaml b/tests/spec/data/path-params/openapi.yaml new file mode 100644 index 0000000..722d818 --- /dev/null +++ b/tests/spec/data/path-params/openapi.yaml @@ -0,0 +1,18 @@ +openapi: 3.0.0 +info: + version: "2021-05-18" + title: Test REST API + description: Specifications for the Test REST API. + contact: + name: bplainia + email: bplainia@lhespotlight.org + +servers: + - url: 'http://localhost:8000' + description: 'Test' + +paths: + /v1/{organizationId}/user: + $ref: 'user.yaml#/paths/Users' + /v1/{organizationId}/user/{id}: + $ref: 'user.yaml#/paths/UserId' \ No newline at end of file diff --git a/tests/spec/data/path-params/user.yaml b/tests/spec/data/path-params/user.yaml new file mode 100644 index 0000000..a842e78 --- /dev/null +++ b/tests/spec/data/path-params/user.yaml @@ -0,0 +1,76 @@ +paths: + Users: + parameters: + - $ref: 'global.yaml#/components/parameters/Version' + - $ref: 'global.yaml#/components/parameters/OrganizationId' + post: + summary: Creates a user + security: + - BearerAuth: [] + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + data: + $ref: '#/components/schemas/User' + responses: + '201': + description: Created + content: + application/json: + schema: + type: object + properties: + data: + $ref: '#/components/schemas/User' + '400': + $ref: 'global.yaml#/components/responses/BadRequest' + '403': + $ref: 'global.yaml#/components/responses/Forbidden' + UserId: + parameters: + - $ref: 'global.yaml#/components/parameters/Version' + - $ref: 'global.yaml#/components/parameters/OrganizationId' + - $ref: '#/components/parameters/UserId' + get: + summary: Gets a user + security: + - BearerAuth: [] + responses: + '200': + description: A bar + content: + application/json: + schema: + type: object + properties: + data: + $ref: '#/components/schemas/User' + '400': + $ref: 'global.yaml#/components/responses/BadRequest' + '403': + $ref: 'global.yaml#/components/responses/Forbidden' + '404': + $ref: 'global.yaml#/components/responses/NotFound' +components: + schemas: + User: + type: object + properties: + id: + type: string + format: uuid + name: + type: string + parameters: + UserId: + in: path + name: id + required: true + schema: + type: string + format: uuid + description: User's ID \ No newline at end of file