Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test to confirm referenced parameters are compiling to arrays #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/spec/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

}
}
28 changes: 28 additions & 0 deletions tests/spec/data/path-params/global.yaml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions tests/spec/data/path-params/openapi.yaml
Original file line number Diff line number Diff line change
@@ -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: [email protected]

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'
76 changes: 76 additions & 0 deletions tests/spec/data/path-params/user.yaml
Original file line number Diff line number Diff line change
@@ -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