From 569d7765207ca739208cef55c7b8023db458c980 Mon Sep 17 00:00:00 2001 From: Dmitriy Lezhnev Date: Thu, 9 Jan 2020 11:45:57 +0500 Subject: [PATCH 1/6] Adds extra validation tests for Parameters - "testItValidatesSchemaAndContentCombination" checks that both "content" and "schema" cannot be present - "testItValidatesSupportedSerializationStyles" checks that only certain serialization styles supported for each Parameter type. --- src/spec/Parameter.php | 12 ++++++- tests/spec/ParameterTest.php | 65 ++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/spec/Parameter.php b/src/spec/Parameter.php index 9cc81820..7c4dc0dc 100644 --- a/src/spec/Parameter.php +++ b/src/spec/Parameter.php @@ -115,7 +115,17 @@ 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.'); + } + + $supportedSerializationStyles = [ + 'path' => ['simple', 'label', 'matrix'], + 'query' => ['form', 'spaceDelimited', 'pipeDelimited', 'deepObject'], + 'header' => ['simple'], + 'cookie' => ['form'], + ]; + if(!in_array($this->style, $supportedSerializationStyles[$this->in])) { + $this->addError('A Parameter Object DOES NOT support this serialization style.'); } } } diff --git a/tests/spec/ParameterTest.php b/tests/spec/ParameterTest.php index a72ba46e..97dd7671 100644 --- a/tests/spec/ParameterTest.php +++ b/tests/spec/ParameterTest.php @@ -148,4 +148,69 @@ 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 testItValidatesSupportedSerializationStyles() + { + // 1. Prepare test inputs + $specTemplate = << ['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); + } + } + } } \ No newline at end of file From 519db334e06d97fab718442d23c50fe7adc6010e Mon Sep 17 00:00:00 2001 From: Dmitriy Lezhnev Date: Thu, 9 Jan 2020 12:00:55 +0500 Subject: [PATCH 2/6] Add test that Parameter's content property can only have one key --- src/spec/Parameter.php | 3 +++ tests/spec/ParameterTest.php | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/spec/Parameter.php b/src/spec/Parameter.php index 7c4dc0dc..437365b5 100644 --- a/src/spec/Parameter.php +++ b/src/spec/Parameter.php @@ -117,6 +117,9 @@ 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.'); } + if(!empty($this->content) && count($this->content)!==1) { + $this->addError('A Parameter Object MUST with Content property must have A SINGLE content type.'); + } $supportedSerializationStyles = [ 'path' => ['simple', 'label', 'matrix'], diff --git a/tests/spec/ParameterTest.php b/tests/spec/ParameterTest.php index 97dd7671..de72cd61 100644 --- a/tests/spec/ParameterTest.php +++ b/tests/spec/ParameterTest.php @@ -169,6 +169,28 @@ public function testItValidatesSchemaAndContentCombination() $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 MUST with Content property must have A SINGLE content type.'], $parameter->getErrors()); + $this->assertFalse($result); + } + + public function testItValidatesSupportedSerializationStyles() { // 1. Prepare test inputs From f13a3cd4b75bccd109d47c670b2969be62d29ae2 Mon Sep 17 00:00:00 2001 From: Dmitriy Lezhnev Date: Thu, 9 Jan 2020 12:04:39 +0500 Subject: [PATCH 3/6] Fix code style --- src/spec/Parameter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/spec/Parameter.php b/src/spec/Parameter.php index 437365b5..d21da631 100644 --- a/src/spec/Parameter.php +++ b/src/spec/Parameter.php @@ -117,7 +117,7 @@ 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.'); } - if(!empty($this->content) && count($this->content)!==1) { + if (! empty($this->content) && count($this->content) !== 1) { $this->addError('A Parameter Object MUST with Content property must have A SINGLE content type.'); } @@ -127,7 +127,7 @@ protected function performValidation() 'header' => ['simple'], 'cookie' => ['form'], ]; - if(!in_array($this->style, $supportedSerializationStyles[$this->in])) { + if (! in_array($this->style, $supportedSerializationStyles[$this->in])) { $this->addError('A Parameter Object DOES NOT support this serialization style.'); } } From 8d109ee970a2aa899f1661260538011759131c39 Mon Sep 17 00:00:00 2001 From: Dmitriy Lezhnev Date: Thu, 9 Jan 2020 12:09:00 +0500 Subject: [PATCH 4/6] fix misspell --- src/spec/Parameter.php | 2 +- tests/spec/ParameterTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/spec/Parameter.php b/src/spec/Parameter.php index d21da631..07416d90 100644 --- a/src/spec/Parameter.php +++ b/src/spec/Parameter.php @@ -118,7 +118,7 @@ protected function performValidation() $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 MUST with Content property must have A SINGLE content type.'); + $this->addError('A Parameter Object with Content property MUST have A SINGLE content type.'); } $supportedSerializationStyles = [ diff --git a/tests/spec/ParameterTest.php b/tests/spec/ParameterTest.php index de72cd61..14557f8c 100644 --- a/tests/spec/ParameterTest.php +++ b/tests/spec/ParameterTest.php @@ -186,7 +186,7 @@ public function testItValidatesContentCanHaveOnlySingleKey() , Parameter::class); $result = $parameter->validate(); - $this->assertEquals(['A Parameter Object MUST with Content property must have A SINGLE content type.'], $parameter->getErrors()); + $this->assertEquals(['A Parameter Object with Content property MUST have A SINGLE content type.'], $parameter->getErrors()); $this->assertFalse($result); } From b80c37f552e3c67b94bf515730fdc7fd102273ec Mon Sep 17 00:00:00 2001 From: Dmitry Lezhnev Date: Fri, 6 Mar 2020 21:55:27 +0500 Subject: [PATCH 5/6] Update src/spec/Parameter.php Co-Authored-By: Carsten Brandt --- src/spec/Parameter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spec/Parameter.php b/src/spec/Parameter.php index 07416d90..9cf94347 100644 --- a/src/spec/Parameter.php +++ b/src/spec/Parameter.php @@ -117,7 +117,7 @@ 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.'); } - if (! empty($this->content) && count($this->content) !== 1) { + if (!empty($this->content) && count($this->content) !== 1) { $this->addError('A Parameter Object with Content property MUST have A SINGLE content type.'); } From 86aacac56fb5bc556f0fa0fbf9774e063d620c58 Mon Sep 17 00:00:00 2001 From: Dmitry Lezhnev Date: Fri, 6 Mar 2020 21:55:43 +0500 Subject: [PATCH 6/6] Update src/spec/Parameter.php Co-Authored-By: Carsten Brandt --- src/spec/Parameter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spec/Parameter.php b/src/spec/Parameter.php index 9cf94347..d133ff67 100644 --- a/src/spec/Parameter.php +++ b/src/spec/Parameter.php @@ -127,7 +127,7 @@ protected function performValidation() 'header' => ['simple'], 'cookie' => ['form'], ]; - if (! in_array($this->style, $supportedSerializationStyles[$this->in])) { + if (isset($supportedSerializationStyles[$this->in]) && !in_array($this->style, $supportedSerializationStyles[$this->in])) { $this->addError('A Parameter Object DOES NOT support this serialization style.'); } }