Skip to content

Commit a958873

Browse files
authored
Merge pull request #53 from lezhnev74/master
Adds extra validation tests for Parameters
2 parents f76c43c + 86aacac commit a958873

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

src/spec/Parameter.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,20 @@ protected function performValidation()
115115
}
116116
}
117117
if (!empty($this->content) && !empty($this->schema)) {
118-
$this->addError("A Parameter Object MUST contain either a schema property, or a content property, but not both. ");
118+
$this->addError('A Parameter Object MUST contain either a schema property, or a content property, but not both.');
119+
}
120+
if (!empty($this->content) && count($this->content) !== 1) {
121+
$this->addError('A Parameter Object with Content property MUST have A SINGLE content type.');
122+
}
123+
124+
$supportedSerializationStyles = [
125+
'path' => ['simple', 'label', 'matrix'],
126+
'query' => ['form', 'spaceDelimited', 'pipeDelimited', 'deepObject'],
127+
'header' => ['simple'],
128+
'cookie' => ['form'],
129+
];
130+
if (isset($supportedSerializationStyles[$this->in]) && !in_array($this->style, $supportedSerializationStyles[$this->in])) {
131+
$this->addError('A Parameter Object DOES NOT support this serialization style.');
119132
}
120133
}
121134
}

tests/spec/ParameterTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,91 @@ public function testDefaultValuesCookie()
148148
$this->assertEquals('form', $parameter->style);
149149
$this->assertTrue($parameter->explode);
150150
}
151+
152+
public function testItValidatesSchemaAndContentCombination()
153+
{
154+
/** @var $parameter Parameter */
155+
$parameter = Reader::readFromYaml(<<<'YAML'
156+
name: token
157+
in: cookie
158+
schema:
159+
type: object
160+
content:
161+
application/json:
162+
schema:
163+
type: object
164+
YAML
165+
, Parameter::class);
166+
167+
$result = $parameter->validate();
168+
$this->assertEquals(['A Parameter Object MUST contain either a schema property, or a content property, but not both.'], $parameter->getErrors());
169+
$this->assertFalse($result);
170+
}
171+
172+
public function testItValidatesContentCanHaveOnlySingleKey()
173+
{
174+
/** @var $parameter Parameter */
175+
$parameter = Reader::readFromYaml(<<<'YAML'
176+
name: token
177+
in: cookie
178+
content:
179+
application/json:
180+
schema:
181+
type: object
182+
application/xml:
183+
schema:
184+
type: object
185+
YAML
186+
, Parameter::class);
187+
188+
$result = $parameter->validate();
189+
$this->assertEquals(['A Parameter Object with Content property MUST have A SINGLE content type.'], $parameter->getErrors());
190+
$this->assertFalse($result);
191+
}
192+
193+
194+
public function testItValidatesSupportedSerializationStyles()
195+
{
196+
// 1. Prepare test inputs
197+
$specTemplate = <<<YAML
198+
name: token
199+
required: true
200+
in: %s
201+
style: %s
202+
YAML;
203+
$goodCombinations = [
204+
'path' => ['simple', 'label', 'matrix'],
205+
'query' => ['form', 'spaceDelimited', 'pipeDelimited', 'deepObject'],
206+
'header' => ['simple'],
207+
'cookie' => ['form'],
208+
];
209+
$badCombinations = [
210+
'path' => ['unknown', 'form', 'spaceDelimited', 'pipeDelimited', 'deepObject'],
211+
'query' => ['unknown', 'simple', 'label', 'matrix'],
212+
'header' => ['unknown', 'form', 'spaceDelimited', 'pipeDelimited', 'deepObject', 'matrix'],
213+
'cookie' => ['unknown', 'spaceDelimited', 'pipeDelimited', 'deepObject', 'matrix', 'label', 'matrix'],
214+
];
215+
216+
// 2. Run tests for valid input
217+
foreach($goodCombinations as $in=>$styles) {
218+
foreach($styles as $style) {
219+
/** @var $parameter Parameter */
220+
$parameter = Reader::readFromYaml(sprintf($specTemplate, $in, $style) , Parameter::class);
221+
$result = $parameter->validate();
222+
$this->assertEquals([], $parameter->getErrors());
223+
$this->assertTrue($result);
224+
}
225+
}
226+
227+
// 2. Run tests for invalid input
228+
foreach($badCombinations as $in=>$styles) {
229+
foreach($styles as $style) {
230+
/** @var $parameter Parameter */
231+
$parameter = Reader::readFromYaml(sprintf($specTemplate, $in, $style) , Parameter::class);
232+
$result = $parameter->validate();
233+
$this->assertEquals(['A Parameter Object DOES NOT support this serialization style.'], $parameter->getErrors());
234+
$this->assertFalse($result);
235+
}
236+
}
237+
}
151238
}

0 commit comments

Comments
 (0)