-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SpyYinCompatibilityMiddleware (#5)
- Loading branch information
1 parent
3873a99
commit c4e1df3
Showing
7 changed files
with
174 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
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,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); | ||
} | ||
} |
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,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(); | ||
} | ||
}; | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WoohooLabs\YinMiddleware\Tests\Utils; | ||
|
||
use WoohooLabs\Yin\JsonApi\Serializer\JsonDeserializer; | ||
|
||
class DummyDeserializer extends JsonDeserializer | ||
{ | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WoohooLabs\YinMiddleware\Tests\Utils; | ||
|
||
use WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory; | ||
|
||
class DummyExceptionFactory extends DefaultExceptionFactory | ||
{ | ||
} |
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,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; | ||
} | ||
} |