Skip to content

Commit

Permalink
Request/Response validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Oct 2, 2022
1 parent 9135517 commit afb7dfd
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 19 deletions.
4 changes: 0 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@
"slim/slim": "^4.0"
},
"require-dev": {
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^9.0",
"php-coveralls/php-coveralls": "^2.0",
"php-di/php-di": "^6.0",
"slim/psr7": "^1.0",
"squizlabs/php_codesniffer": "^3.0"
},
"suggests": {
"league/openapi-psr7-validator": "Provides validation of http request/response"
}
}
16 changes: 14 additions & 2 deletions src/Phrity/Slim/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class OpenApi implements IteratorAggregate

/**
* Constructor for Slim OpenApi route generator.
* @param OpenApiSpec|string $source File path to OpenApi schema or OpenApi instance
* @param OpenApiSpec|string $source File path to OpenApi schema or OpenApi instance
* @param array<string,mixed> $settings Optional settings
*/
public function __construct($source, array $settings = [])
Expand Down Expand Up @@ -77,7 +77,7 @@ public function getIterator(): Traversable

/**
* Register routes on Slim App.
* @param \Slim\App $app Slim App instance to register routes on
* @param App $app Slim App instance to register routes on
*/
public function route(App $app): void
{
Expand All @@ -96,16 +96,28 @@ public function getSetting(string $key)
return isset($this->settings->$key) ? $this->settings->$key : null;
}

/**
* Get request validator.
* @return RoutedServerRequestValidator Validator
*/
public function getRequestValidator(): RoutedServerRequestValidator
{
return $this->getValidatorBuilder()->getRoutedRequestValidator();
}

/**
* Get response validator.
* @return ResponseValidator Validator
*/
public function getResponseValidator(): ResponseValidator
{
return $this->getValidatorBuilder()->getResponseValidator();
}

/**
* Get validator builder.
* @return ValidatorBuilder Builder
*/
private function getValidatorBuilder(): ValidatorBuilder
{
if (empty($this->validation_builder)) {
Expand Down
12 changes: 10 additions & 2 deletions src/Phrity/Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(OpenApi $openapi, string $path, string $method, Oper

/**
* Get string representation of route.
* @return string String representation of route
* @return string String representation of route
*/
public function __toString(): string
{
Expand All @@ -54,7 +54,7 @@ public function __toString(): string

/**
* Register route on Slim App.
* @param \Slim\App $app Slim App instance to register routes on
* @param App $app Slim App instance to register routes on
*/
public function route(App $app): void
{
Expand All @@ -80,6 +80,10 @@ public function route(App $app): void
}
}

/**
* Validate a request.
* @param Request $request Server request message
*/
public function validateRequest(Request $request): void
{
$this->openapi->getRequestValidator()->validate(
Expand All @@ -88,6 +92,10 @@ public function validateRequest(Request $request): void
);
}

/**
* Validate a response.
* @param Response $response Response message
*/
public function validateResponse(Response $response): void
{
$this->openapi->getResponseValidator()->validate(
Expand Down
3 changes: 1 addition & 2 deletions tests/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ public function setUp(): void
error_reporting(-1);
}

// Test a simple schema with paths
/**
*
* Test a simple schema with paths
*/
public function testRoutesStrict(): void
{
Expand Down
12 changes: 12 additions & 0 deletions tests/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public function testRoutingNoRoute(): void
$response = $slim->handle($request);
}

/**
* Test Slim routing using standard array argument
*/
public function testRequestResponse(): void
{
$slim = AppFactory::create();
Expand All @@ -104,6 +107,9 @@ public function testRequestResponse(): void
$this->assertEquals("MyController::argument=something,else", $response->getBody()->__toString());
}

/**
* Test Slim routing using listed arguments
*/
public function testRequestResponseArgs(): void
{
$slim = AppFactory::create();
Expand All @@ -120,6 +126,9 @@ public function testRequestResponseArgs(): void
$this->assertEquals("MyController::arguments=something,else", $response->getBody()->__toString());
}

/**
* Test Slim routing using container instance for mapping
*/
public function testContainer(): void
{
$container = new Container();
Expand All @@ -135,6 +144,9 @@ public function testContainer(): void
$this->assertEquals("ContainerController::byContainer", $response->getBody()->__toString());
}

/**
* Test binding Route to request
*/
public function testRouteBind(): void
{
$slim = AppFactory::create();
Expand Down
29 changes: 27 additions & 2 deletions tests/classes/Phrity/Slim/FactoryTrait.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
<?php

/**
* File for Slim OpenApi tests
* @package Phrity > Slim > OpenApi
*/

declare(strict_types=1);

namespace Phrity\Slim;

use Psr\Http\Message\{
ServerRequestInterface,
StreamInterface
};
use Slim\Psr7\Factory\{
ServerRequestFactory,
StreamFactory,
};

/**
* Slim OpenApi test helper
*/
trait FactoryTrait
{
private static function createStream(string $data)
/**
* Create a stream.
* @param string $data Data to write on stream
* @return StreamInterface A stream
*/
private static function createStream(string $data): StreamInterface
{
return (new StreamFactory())->createStream($data);
}

private static function createServerRequest(string $method, string $path)
/**
* Create a server request.
* @param string $method HTTP method
* @param string $path Request path
* @return StreamInterface A server request
*/
private static function createServerRequest(string $method, string $path): ServerRequestInterface
{
return (new ServerRequestFactory())->createServerRequest($method, $path);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/classes/Test/ContainerController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/**
* File for Slim OpenApi tests
* @package Phrity > Slim > OpenApi
*/

declare(strict_types=1);

namespace Test;

use Psr\Container\ContainerInterface;
Expand All @@ -8,6 +15,9 @@
ResponseInterface as Response
};

/**
* Slim OpenApi test controller
*/
class ContainerController
{
private ContainerInterface $container;
Expand Down
20 changes: 15 additions & 5 deletions tests/classes/Test/MyController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
<?php

/**
* File for Slim OpenApi tests
* @package Phrity > Slim > OpenApi
*/

declare(strict_types=1);

namespace Test;

use Psr\Http\Message\{
ServerRequestInterface as Request,
ResponseInterface as Response
};

/**
* Slim OpenApi test controller
*/
class MyController
{
/**
Expand All @@ -16,7 +26,7 @@ class MyController
* @param array<string,mixed> $arguments
* @return Response
*/
public function put(Request $request, Response $response, array $arguments)
public function put(Request $request, Response $response, array $arguments): Response
{
$response->getBody()->write("MyController::put");
return $response;
Expand All @@ -29,7 +39,7 @@ public function put(Request $request, Response $response, array $arguments)
* @param array<string,mixed> $arguments
* @return Response
*/
public function __invoke(Request $request, Response $response, array $arguments)
public function __invoke(Request $request, Response $response, array $arguments): Response
{
$response->getBody()->write("MyController::__invoke");
return $response;
Expand All @@ -42,7 +52,7 @@ public function __invoke(Request $request, Response $response, array $arguments)
* @param array<string,string> $arguments
* @return Response
*/
public function argument(Request $request, Response $response, array $arguments)
public function argument(Request $request, Response $response, array $arguments): Response
{
$response->getBody()->write("MyController::argument={$arguments['arg_1']},{$arguments['arg_2']}");
return $response;
Expand All @@ -56,7 +66,7 @@ public function argument(Request $request, Response $response, array $arguments)
* @param string $arg_2
* @return Response
*/
public function arguments(Request $request, Response $response, string $arg_1, string $arg_2)
public function arguments(Request $request, Response $response, string $arg_1, string $arg_2): Response
{
$response->getBody()->write("MyController::arguments={$arg_1},{$arg_2}");
return $response;
Expand All @@ -69,7 +79,7 @@ public function arguments(Request $request, Response $response, string $arg_1, s
* @param array<string,string> $arguments
* @return Response
*/
public function bind(Request $request, Response $response, array $arguments)
public function bind(Request $request, Response $response, array $arguments): Response
{
$route = $request->getAttribute('openapi-route');
$response->getBody()->write("MyController::bind={$route}");
Expand Down
21 changes: 19 additions & 2 deletions tests/classes/Test/ValidatingController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/**
* File for Slim OpenApi tests
* @package Phrity > Slim > OpenApi
*/

declare(strict_types=1);

namespace Test;

use Phrity\Slim\FactoryTrait;
Expand All @@ -8,6 +15,9 @@
ResponseInterface as Response
};

/**
* Slim OpenApi test controller
*/
class ValidatingController
{
use FactoryTrait;
Expand All @@ -19,7 +29,7 @@ class ValidatingController
* @param array<string,string> $arguments
* @return Response
*/
public function post(Request $request, Response $response, array $arguments)
public function post(Request $request, Response $response, array $arguments): Response
{
$route = $request->getAttribute('openapi-route');
$route->validateRequest($request);
Expand All @@ -31,7 +41,14 @@ public function post(Request $request, Response $response, array $arguments)
return $response;
}

public function put(Request $request, Response $response, array $arguments)
/**
* Request method called by Slim.
* @param Request $request
* @param Response $response
* @param array<string,string> $arguments
* @return Response
*/
public function put(Request $request, Response $response, array $arguments): Response
{
$route = $request->getAttribute('openapi-route');
$data = json_decode($request->getBody()->__toString());
Expand Down

0 comments on commit afb7dfd

Please sign in to comment.