-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4261c0e
Showing
17 changed files
with
2,116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
language: php | ||
php: | ||
- "7.1" | ||
- "7.0" | ||
- "5.6" | ||
|
||
install: | ||
- composer install | ||
|
||
script: | ||
- phpunit | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Joao Gilberto Magalhaes | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "byjg/php-swagger-test", | ||
"description": "A set of tools for test your REST calls based on the swagger documentation", | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "João Gilberto Magalhães", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"ByJG\\Swagger\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
To change this license header, choose License Headers in Project Properties. | ||
To change this template file, choose Tools | Templates | ||
and open the template in the editor. | ||
--> | ||
|
||
<!-- see http://www.phpunit.de/wiki/Documentation --> | ||
<phpunit bootstrap="./vendor/autoload.php" | ||
colors="false" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
stopOnFailure="false"> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./src</directory> | ||
</whitelist> | ||
</filter> | ||
|
||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* User: jg | ||
* Date: 22/05/17 | ||
* Time: 10:16 | ||
*/ | ||
|
||
namespace ByJG\Swagger\Exception; | ||
|
||
class DefinitionNotFoundException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* User: jg | ||
* Date: 22/05/17 | ||
* Time: 10:16 | ||
*/ | ||
|
||
namespace ByJG\Swagger\Exception; | ||
|
||
class HttpMethodNotFoundException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* User: jg | ||
* Date: 22/05/17 | ||
* Time: 10:16 | ||
*/ | ||
|
||
namespace ByJG\Swagger\Exception; | ||
|
||
class InvalidDefinitionException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* User: jg | ||
* Date: 22/05/17 | ||
* Time: 10:16 | ||
*/ | ||
|
||
namespace ByJG\Swagger\Exception; | ||
|
||
class NotMatchedException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* User: jg | ||
* Date: 22/05/17 | ||
* Time: 10:16 | ||
*/ | ||
|
||
namespace ByJG\Swagger\Exception; | ||
|
||
class PathNotFoundException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
/** | ||
* User: jg | ||
* Date: 22/05/17 | ||
* Time: 10:52 | ||
*/ | ||
|
||
namespace ByJG\Swagger; | ||
|
||
use ByJG\Swagger\Exception\NotMatchedException; | ||
|
||
abstract class SwaggerBody | ||
{ | ||
/** | ||
* @var \ByJG\Swagger\SwaggerSchema | ||
*/ | ||
protected $swaggerSchema; | ||
|
||
protected $structure; | ||
|
||
protected $name; | ||
|
||
/** | ||
* SwaggerRequestBody constructor. | ||
* | ||
* @param \ByJG\Swagger\SwaggerSchema $swaggerSchema | ||
* @param string $name | ||
* @param array $structure | ||
*/ | ||
public function __construct(SwaggerSchema $swaggerSchema, $name, $structure) | ||
{ | ||
$this->swaggerSchema = $swaggerSchema; | ||
$this->name = $name; | ||
$this->structure = $structure; | ||
} | ||
|
||
abstract public function match($body); | ||
|
||
protected function matchString($name, $schema, $body) | ||
{ | ||
if (isset($schema['enum'])) { | ||
if (!in_array($body, $schema['enum'])) { | ||
throw new NotMatchedException("Value '$body' in '$name' not matched in ENUM"); | ||
}; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function matchNumber($name, $body) | ||
{ | ||
if (!is_numeric($body)) { | ||
throw new NotMatchedException("Expected '$name' to be numeric, but found '$body'"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function matchBool($name, $body) | ||
{ | ||
if (!is_bool($body)) { | ||
throw new NotMatchedException("Expected '$name' to be boolean, but found '$body'"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function matchArray($name, $schema, $body) | ||
{ | ||
foreach ((array)$body as $item) { | ||
$this->matchSchema($name, $schema['items'], $item); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @param $name | ||
* @param $schema | ||
* @param $body | ||
* @return bool | ||
* @throws \ByJG\Swagger\Exception\NotMatchedException | ||
* @throws \Exception | ||
*/ | ||
protected function matchSchema($name, $schema, $body) | ||
{ | ||
if (isset($schema['type'])) { | ||
if ($schema['type'] == 'string') { | ||
return $this->matchString($name, $schema, $body); | ||
} | ||
|
||
if ($schema['type'] == 'integer' || $schema['type'] == 'float') { | ||
return $this->matchNumber($name, $body); | ||
} | ||
|
||
if ($schema['type'] == 'bool' || $schema['type'] == 'boolean') { | ||
return $this->matchBool($name, $body); | ||
} | ||
|
||
if ($schema['type'] == 'array') { | ||
return $this->matchArray($name, $schema, $body); | ||
} | ||
} | ||
|
||
if (isset($schema['$ref'])) { | ||
$defintion = $this->swaggerSchema->getDefintion($schema['$ref']); | ||
return $this->matchSchema($schema['$ref'], $defintion, $body); | ||
} | ||
|
||
if (isset($schema['properties'])) { | ||
foreach ($schema['properties'] as $prop => $def) { | ||
if (!isset($body[$prop])) { | ||
// if ($required) { | ||
// throw new NotMatchedException("Required property '$prop' in '$name' not found in object"); | ||
// } | ||
continue; | ||
} | ||
$this->matchSchema($prop, $def, $body[$prop]); | ||
unset($body[$prop]); | ||
} | ||
|
||
if (count($body) > 0) { | ||
throw new NotMatchedException( | ||
"The properties '" | ||
. implode(', ', array_keys($body)) | ||
. "' not defined in '$name'" | ||
); | ||
} | ||
return true; | ||
} | ||
|
||
throw new \Exception("Not all cases are defined. Please open an issue about this. Schema: $name"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* User: jg | ||
* Date: 22/05/17 | ||
* Time: 10:52 | ||
*/ | ||
|
||
namespace ByJG\Swagger; | ||
|
||
class SwaggerRequestBody extends SwaggerBody | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
/** | ||
* User: jg | ||
* Date: 22/05/17 | ||
* Time: 10:52 | ||
*/ | ||
|
||
namespace ByJG\Swagger; | ||
|
||
use ByJG\Swagger\Exception\NotMatchedException; | ||
|
||
class SwaggerResponseBody extends SwaggerBody | ||
{ | ||
public function match($body) | ||
{ | ||
if (!isset($this->structure['schema'])) { | ||
if (!empty($body)) { | ||
throw new NotMatchedException("Expected empty body for " . $this->name); | ||
} | ||
return true; | ||
} | ||
|
||
return $this->matchSchema($this->name, $this->structure['schema'], $body); | ||
} | ||
} |
Oops, something went wrong.