Skip to content

Adds extra validation tests for Parameters #53

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

Merged
merged 6 commits into from
Mar 6, 2020
Merged
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
15 changes: 14 additions & 1 deletion src/spec/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,20 @@ protected function performValidation()
}
}
if (!empty($this->content) && !empty($this->schema)) {
$this->addError("A Parameter Object MUST contain either a schema property, or a content property, but not both. ");
$this->addError('A Parameter Object MUST contain either a schema property, or a content property, but not both.');
}
if (!empty($this->content) && count($this->content) !== 1) {
$this->addError('A Parameter Object with Content property MUST have A SINGLE content type.');
}

$supportedSerializationStyles = [
'path' => ['simple', 'label', 'matrix'],
'query' => ['form', 'spaceDelimited', 'pipeDelimited', 'deepObject'],
'header' => ['simple'],
'cookie' => ['form'],
];
if (isset($supportedSerializationStyles[$this->in]) && !in_array($this->style, $supportedSerializationStyles[$this->in])) {
$this->addError('A Parameter Object DOES NOT support this serialization style.');
}
}
}
87 changes: 87 additions & 0 deletions tests/spec/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,91 @@ public function testDefaultValuesCookie()
$this->assertEquals('form', $parameter->style);
$this->assertTrue($parameter->explode);
}

public function testItValidatesSchemaAndContentCombination()
{
/** @var $parameter Parameter */
$parameter = Reader::readFromYaml(<<<'YAML'
name: token
in: cookie
schema:
type: object
content:
application/json:
schema:
type: object
YAML
, Parameter::class);

$result = $parameter->validate();
$this->assertEquals(['A Parameter Object MUST contain either a schema property, or a content property, but not both.'], $parameter->getErrors());
$this->assertFalse($result);
}

public function testItValidatesContentCanHaveOnlySingleKey()
{
/** @var $parameter Parameter */
$parameter = Reader::readFromYaml(<<<'YAML'
name: token
in: cookie
content:
application/json:
schema:
type: object
application/xml:
schema:
type: object
YAML
, Parameter::class);

$result = $parameter->validate();
$this->assertEquals(['A Parameter Object with Content property MUST have A SINGLE content type.'], $parameter->getErrors());
$this->assertFalse($result);
}


public function testItValidatesSupportedSerializationStyles()
{
// 1. Prepare test inputs
$specTemplate = <<<YAML
name: token
required: true
in: %s
style: %s
YAML;
$goodCombinations = [
'path' => ['simple', 'label', 'matrix'],
'query' => ['form', 'spaceDelimited', 'pipeDelimited', 'deepObject'],
'header' => ['simple'],
'cookie' => ['form'],
];
$badCombinations = [
'path' => ['unknown', 'form', 'spaceDelimited', 'pipeDelimited', 'deepObject'],
'query' => ['unknown', 'simple', 'label', 'matrix'],
'header' => ['unknown', 'form', 'spaceDelimited', 'pipeDelimited', 'deepObject', 'matrix'],
'cookie' => ['unknown', 'spaceDelimited', 'pipeDelimited', 'deepObject', 'matrix', 'label', 'matrix'],
];

// 2. Run tests for valid input
foreach($goodCombinations as $in=>$styles) {
foreach($styles as $style) {
/** @var $parameter Parameter */
$parameter = Reader::readFromYaml(sprintf($specTemplate, $in, $style) , Parameter::class);
$result = $parameter->validate();
$this->assertEquals([], $parameter->getErrors());
$this->assertTrue($result);
}
}

// 2. Run tests for invalid input
foreach($badCombinations as $in=>$styles) {
foreach($styles as $style) {
/** @var $parameter Parameter */
$parameter = Reader::readFromYaml(sprintf($specTemplate, $in, $style) , Parameter::class);
$result = $parameter->validate();
$this->assertEquals(['A Parameter Object DOES NOT support this serialization style.'], $parameter->getErrors());
$this->assertFalse($result);
}
}
}
}