Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed May 22, 2017
0 parents commit 4261c0e
Show file tree
Hide file tree
Showing 17 changed files with 2,116 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
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

20 changes: 20 additions & 0 deletions LICENSE
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 added README.md
Empty file.
18 changes: 18 additions & 0 deletions composer.json
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/"
}
}
}
28 changes: 28 additions & 0 deletions phpunit.xml.dist
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>
13 changes: 13 additions & 0 deletions src/Exception/DefinitionNotFoundException.php
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
{

}
13 changes: 13 additions & 0 deletions src/Exception/HttpMethodNotFoundException.php
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
{

}
13 changes: 13 additions & 0 deletions src/Exception/InvalidDefinitionException.php
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
{

}
13 changes: 13 additions & 0 deletions src/Exception/NotMatchedException.php
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
{

}
13 changes: 13 additions & 0 deletions src/Exception/PathNotFoundException.php
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
{

}
133 changes: 133 additions & 0 deletions src/SwaggerBody.php
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");
}
}
13 changes: 13 additions & 0 deletions src/SwaggerRequestBody.php
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
{

}
25 changes: 25 additions & 0 deletions src/SwaggerResponseBody.php
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);
}
}
Loading

0 comments on commit 4261c0e

Please sign in to comment.