-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from sunrise-php/release/v1.9.0
v1.9.0
- Loading branch information
Showing
8 changed files
with
317 additions
and
11 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
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
.php_cs.cache | ||
.phpunit.result.cache | ||
composer.lock | ||
coverage.xml | ||
phpbench.json | ||
phpcs.xml | ||
phpunit.xml | ||
vendor/ | ||
/.php_cs.cache | ||
/.phpunit.result.cache | ||
/composer.lock | ||
/coverage.xml | ||
/phpbench.json | ||
/phpcs.xml | ||
/phpunit.xml | ||
/vendor/ |
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,21 @@ | ||
build: | ||
environment: | ||
php: | ||
version: '8.0' | ||
pecl_extensions: | ||
- apcu | ||
ini: | ||
'apc.enable_cli': '1' | ||
'xdebug.mode': 'coverage' | ||
nodes: | ||
analysis: | ||
tests: | ||
override: | ||
- php-scrutinizer-run | ||
coverage: | ||
tests: | ||
override: | ||
- command: php vendor/bin/phpunit --coverage-clover coverage.xml | ||
coverage: | ||
file: coverage.xml | ||
format: clover |
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
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,78 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* It's free open-source software released under the MIT License. | ||
* | ||
* @author Anatoly Fenric <[email protected]> | ||
* @copyright Copyright (c) 2019, Anatoly Fenric | ||
* @license https://github.com/sunrise-php/http-router-openapi/blob/master/LICENSE | ||
* @link https://github.com/sunrise-php/http-router-openapi | ||
*/ | ||
|
||
namespace Sunrise\Http\Router\OpenApi\Test; | ||
|
||
/** | ||
* Import classes | ||
*/ | ||
use JsonSchema\Validator as JsonSchemaValidator; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Sunrise\Http\Router\OpenApi\Utility\JsonSchemaBuilder; | ||
use Sunrise\Http\Router\RouteInterface; | ||
use ReflectionClass; | ||
|
||
/** | ||
* Import functions | ||
*/ | ||
use function json_decode; | ||
use function json_encode; | ||
use function json_last_error; | ||
use function json_last_error_msg; | ||
|
||
/** | ||
* Import constants | ||
*/ | ||
use const JSON_ERROR_NONE; | ||
use const JSON_PRETTY_PRINT; | ||
use const JSON_UNESCAPED_SLASHES; | ||
use const JSON_UNESCAPED_UNICODE; | ||
|
||
/** | ||
* OpenApiAssertKitTrait | ||
*/ | ||
trait OpenApiAssertKitTrait | ||
{ | ||
|
||
/** | ||
* @param RouteInterface $route | ||
* @param ResponseInterface $response | ||
* | ||
* @return void | ||
*/ | ||
protected function assertResponseBodyMatchesDescription(RouteInterface $route, ResponseInterface $response) : void | ||
{ | ||
$body = (string) $response->getBody(); | ||
if ('' === $body) { | ||
$this->fail('Response body MUST be non-empty.'); | ||
} | ||
|
||
$data = json_decode($body); | ||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
$this->fail('Response body MUST contain valid JSON: ' . json_last_error_msg()); | ||
} | ||
|
||
$jsonSchemaBuilder = new JsonSchemaBuilder(new ReflectionClass($route->getRequestHandler())); | ||
$jsonSchema = $jsonSchemaBuilder->forResponseBody($response->getStatusCode(), 'application/json'); | ||
if (null === $jsonSchema) { | ||
$this->fail('No JSON schema found.'); | ||
} | ||
|
||
$jsonSchemaValidator = new JsonSchemaValidator(); | ||
$jsonSchemaValidator->validate($data, $jsonSchema); | ||
if (false === $jsonSchemaValidator->isValid()) { | ||
$flags = JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE; | ||
$this->fail('Invalid body: ' . json_encode($jsonSchemaValidator->getErrors(), $flags)); | ||
} | ||
|
||
$this->assertTrue(true); | ||
} | ||
} |
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
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,129 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Sunrise\Http\Router\OpenApi\Tests\Test; | ||
|
||
/** | ||
* Import classes | ||
*/ | ||
use PHPUnit\Framework\AssertionFailedError; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Sunrise\Http\Factory\ResponseFactory; | ||
use Sunrise\Http\Router\OpenApi\Test\OpenApiAssertKitTrait; | ||
use Sunrise\Http\Router\Route; | ||
use RuntimeException; | ||
|
||
/** | ||
* OpenApiAssertKitTraitTest | ||
*/ | ||
class OpenApiAssertKitTraitTest extends TestCase | ||
{ | ||
use OpenApiAssertKitTrait; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testAssertResponseBodyMatchesDescription() : void | ||
{ | ||
/** | ||
* @OpenApi\Operation( | ||
* responses={ | ||
* 200=@OpenApi\Response( | ||
* description="OK", | ||
* content={ | ||
* "application/json"=@OpenApi\MediaType( | ||
* schema=@OpenApi\Schema( | ||
* type="object", | ||
* properties={ | ||
* "foo"=@OpenApi\Schema( | ||
* type="string", | ||
* ), | ||
* "bar"=@OpenApi\Schema( | ||
* type="string", | ||
* nullable=true, | ||
* ), | ||
* }, | ||
* ), | ||
* ), | ||
* }, | ||
* ), | ||
* }, | ||
* ) | ||
*/ | ||
$rh1 = new class implements RequestHandlerInterface | ||
{ | ||
public function handle(ServerRequestInterface $request) : ResponseInterface | ||
{ | ||
throw new RuntimeException(); | ||
} | ||
}; | ||
|
||
$rh2 = new class implements RequestHandlerInterface | ||
{ | ||
public function handle(ServerRequestInterface $request) : ResponseInterface | ||
{ | ||
throw new RuntimeException(); | ||
} | ||
}; | ||
|
||
$route = new Route('foo', '/foo', ['GET'], $rh1); | ||
$response = (new ResponseFactory)->createResponse(200); | ||
$response->getBody()->write(''); | ||
try { | ||
$this->assertResponseBodyMatchesDescription($route, $response); | ||
} catch (AssertionFailedError $e) { | ||
$this->assertTrue(true); | ||
$this->assertSame('Response body MUST be non-empty.', $e->getMessage()); | ||
} | ||
|
||
$route = new Route('foo', '/foo', ['GET'], $rh1); | ||
$response = (new ResponseFactory)->createResponse(200); | ||
$response->getBody()->write('!'); | ||
try { | ||
$this->assertResponseBodyMatchesDescription($route, $response); | ||
} catch (AssertionFailedError $e) { | ||
$this->assertTrue(true); | ||
$this->assertSame('Response body MUST contain valid JSON: Syntax error', $e->getMessage()); | ||
} | ||
|
||
$route = new Route('foo', '/foo', ['GET'], $rh2); | ||
$response = (new ResponseFactory)->createResponse(200); | ||
$response->getBody()->write(json_encode([])); | ||
try { | ||
$this->assertResponseBodyMatchesDescription($route, $response); | ||
} catch (AssertionFailedError $e) { | ||
$this->assertTrue(true); | ||
$this->assertSame('No JSON schema found.', $e->getMessage()); | ||
} | ||
|
||
$route = new Route('foo', '/foo', ['GET'], $rh1); | ||
$response = (new ResponseFactory)->createResponse(200); | ||
$response->getBody()->write(json_encode(['foo', 'foo', 'bar' => 1])); | ||
try { | ||
$this->assertResponseBodyMatchesDescription($route, $response); | ||
} catch (AssertionFailedError $e) { | ||
$this->assertTrue(true); | ||
$this->assertSame('Invalid body: [ | ||
{ | ||
"property": "bar", | ||
"pointer": "/bar", | ||
"message": "Integer value found, but a string or a null is required", | ||
"constraint": "type", | ||
"context": 1 | ||
} | ||
]', $e->getMessage()); | ||
} | ||
|
||
$route = new Route('foo', '/foo', ['GET'], $rh1); | ||
$response = (new ResponseFactory)->createResponse(200); | ||
$response->getBody()->write(json_encode(['foo', 'foo', 'bar' => 'bar'])); | ||
$this->assertResponseBodyMatchesDescription($route, $response); | ||
|
||
$route = new Route('foo', '/foo', ['GET'], $rh1); | ||
$response = (new ResponseFactory)->createResponse(200); | ||
$response->getBody()->write(json_encode(['foo', 'foo', 'bar' => null])); | ||
$this->assertResponseBodyMatchesDescription($route, $response); | ||
} | ||
} |
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