Skip to content

Commit

Permalink
Add SpyYinCompatibilityMiddleware (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Nov 24, 2019
1 parent 3873a99 commit c4e1df3
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

ADDED:

- [#5](https://github.com/woohoolabs/yin-middleware/issues/5): `YinCompatibilityMiddleware` in order to facilitate the usage of Yin in other frameworks

CHANGED:

REMOVED:
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ The following sections will guide you through how to use and configure the provi
class possibly), otherwise the `JsonApiDispatcherMiddleware` and the `JsonApiExceptionHandlerMiddleware` will throw an
exception.

### YinCompatibilityMiddleware

This middleware facilitates the usage of Yin and Yin-Middleware in other frameworks. It does so by upgrading a basic PSR-7
request object to `JsonApiRequest`, which is suitable for working with Yin. Please keep in mind, that this middleware should
precede any other middleware that uses `JsonApiRequest` as `$request` parameter.

```php
$harmony->addMiddleware(new YinCompatibilityMiddleware());
```

Available configuration options for the middleware (they can be passed to the constructor):

- `exceptionFactory`: The [ExceptionFactoryInterface](https://github.com/woohoolabs/yin/#exceptions) instance to be used
- `deserializer`: The [DeserializerInterface](https://github.com/woohoolabs/yin/#custom-deserialization) instance to be used

### JsonApiRequestValidatorMiddleware

The middleware is mainly useful in a development environment, and it is able to validate a
Expand Down
44 changes: 44 additions & 0 deletions src/Middleware/YinCompatibilityMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace WoohooLabs\YinMiddleware\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory;
use WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface;
use WoohooLabs\Yin\JsonApi\Request\JsonApiRequest;
use WoohooLabs\Yin\JsonApi\Request\JsonApiRequestInterface;
use WoohooLabs\Yin\JsonApi\Serializer\DeserializerInterface;
use WoohooLabs\Yin\JsonApi\Serializer\JsonDeserializer;

class YinCompatibilityMiddleware implements MiddlewareInterface
{
private ExceptionFactoryInterface $exceptionFactory;
protected DeserializerInterface $deserializer;

public function __construct(
?ExceptionFactoryInterface $exceptionFactory = null,
?DeserializerInterface $deserializer = null
) {
$this->exceptionFactory = $exceptionFactory ?? new DefaultExceptionFactory();
$this->deserializer = $deserializer ?? new JsonDeserializer();
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($request instanceof JsonApiRequestInterface === false) {
$request = $this->createJsonApiRequest($request);
}

return $handler->handle($request);
}

protected function createJsonApiRequest(ServerRequestInterface $request): JsonApiRequest
{
return new JsonApiRequest($request, $this->exceptionFactory, $this->deserializer);
}
}
65 changes: 65 additions & 0 deletions tests/Middleware/YinCompatibilityMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace WoohooLabs\YinMiddleware\Tests\Middleware;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory;
use WoohooLabs\Yin\JsonApi\Request\JsonApiRequest;
use WoohooLabs\Yin\JsonApi\Request\JsonApiRequestInterface;
use WoohooLabs\YinMiddleware\Tests\Utils\DummyDeserializer;
use WoohooLabs\YinMiddleware\Tests\Utils\DummyExceptionFactory;
use WoohooLabs\YinMiddleware\Tests\Utils\SpyYinCompatibilityMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;

class YinCompatibilityMiddlewareTest extends TestCase
{
/**
* @test
*/
public function processServerRequest(): void
{
$middleware = new SpyYinCompatibilityMiddleware(new DummyExceptionFactory(), new DummyDeserializer());

$middleware->process($this->createServerRequest(), $this->createHandler());

$this->assertInstanceOf(JsonApiRequest::class, $middleware->getUpgradedRequest());
}

/**
* @test
*/
public function processYinRequest(): void
{
$middleware = new SpyYinCompatibilityMiddleware();

$middleware->process($this->createYinRequest(), $this->createHandler());

$this->assertNull($middleware->getUpgradedRequest());
}

private function createServerRequest(): ServerRequest
{
return new ServerRequest();
}

private function createYinRequest(): JsonApiRequestInterface
{
return new JsonApiRequest(new ServerRequest(), new DefaultExceptionFactory());
}

private function createHandler(): RequestHandlerInterface
{
return new class implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new Response();
}
};
}
}
11 changes: 11 additions & 0 deletions tests/Utils/DummyDeserializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace WoohooLabs\YinMiddleware\Tests\Utils;

use WoohooLabs\Yin\JsonApi\Serializer\JsonDeserializer;

class DummyDeserializer extends JsonDeserializer
{
}
11 changes: 11 additions & 0 deletions tests/Utils/DummyExceptionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace WoohooLabs\YinMiddleware\Tests\Utils;

use WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory;

class DummyExceptionFactory extends DefaultExceptionFactory
{
}
26 changes: 26 additions & 0 deletions tests/Utils/SpyYinCompatibilityMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace WoohooLabs\YinMiddleware\Tests\Utils;

use Psr\Http\Message\ServerRequestInterface;
use WoohooLabs\Yin\JsonApi\Request\JsonApiRequest;
use WoohooLabs\YinMiddleware\Middleware\YinCompatibilityMiddleware;

class SpyYinCompatibilityMiddleware extends YinCompatibilityMiddleware
{
private ?JsonApiRequest $upgradedRequest = null;

protected function createJsonApiRequest(ServerRequestInterface $request): JsonApiRequest
{
$this->upgradedRequest = parent::createJsonApiRequest($request);

return $this->upgradedRequest;
}

public function getUpgradedRequest(): ?JsonApiRequest
{
return $this->upgradedRequest;
}
}

0 comments on commit c4e1df3

Please sign in to comment.