Skip to content

Commit

Permalink
Merge 722e363 into 9e58e21
Browse files Browse the repository at this point in the history
  • Loading branch information
vitormattos committed May 30, 2021
2 parents 9e58e21 + 722e363 commit faf13da
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Base/Body.php
Expand Up @@ -106,6 +106,10 @@ protected function matchString($name, $schemaArray, $body, $type)
$this->checkPattern($name, $body, $schemaArray['pattern']);
}

if (!is_string($body)) {
throw new NotMatchedException("Value '" . var_export($body, true) . "' in '$name' not matched in pattern. ", $this->structure);
}

return true;
}

Expand Down
32 changes: 31 additions & 1 deletion tests/SwaggerResponseBodyTest.php
Expand Up @@ -314,8 +314,19 @@ public function testMatchResponseBodyWhenValueWithNestedPropertiesIsNullAndNulls
* @throws \ByJG\ApiTools\Exception\NotMatchedException
* @throws \ByJG\ApiTools\Exception\PathNotFoundException
*/
public function testMatchResponseBodyWhenValueWithPatterns()
public function testNotMatchResponseBodyWhenValueWithPatterns()
{
$this->expectException(\ByJG\ApiTools\Exception\NotMatchedException::class);
$this->expectExceptionMessage(<<<'EOL'
Value '18' in 'age' not matched in pattern. ->
{
"description": "successful operation",
"schema": {
"$ref": "#\/definitions\/DateShelter"
}
}
EOL
);
$allowNullValues = false;
$body = [
"date" => "2010-05-11",
Expand All @@ -325,6 +336,25 @@ public function testMatchResponseBodyWhenValueWithPatterns()
$this->assertTrue($responseParameter->match($body));
}

/**
* @throws \ByJG\ApiTools\Exception\DefinitionNotFoundException
* @throws \ByJG\ApiTools\Exception\GenericSwaggerException
* @throws \ByJG\ApiTools\Exception\HttpMethodNotFoundException
* @throws \ByJG\ApiTools\Exception\InvalidDefinitionException
* @throws \ByJG\ApiTools\Exception\NotMatchedException
* @throws \ByJG\ApiTools\Exception\PathNotFoundException
*/
public function testMatchResponseBodyWhenValueWithPatterns()
{
$allowNullValues = false;
$body = [
"date" => "2010-05-11",
"age" => '18'
];
$responseParameter = self::swaggerSchema($allowNullValues)->getResponseParameters('/v2/pet/dateShelter', 'get', 200);
$this->assertTrue($responseParameter->match($body));
}

/**
* @throws \ByJG\ApiTools\Exception\DefinitionNotFoundException
* @throws \ByJG\ApiTools\Exception\GenericSwaggerException
Expand Down

0 comments on commit faf13da

Please sign in to comment.