Skip to content
This repository has been archived by the owner on Feb 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from chimeraphp/provide-expressive-adapter
Browse files Browse the repository at this point in the history
Provide adapter for Zend Expressive
  • Loading branch information
lcobucci authored Jun 20, 2019
2 parents ee5b04d + 9302926 commit 0609cb5
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace Chimera\Routing\Expressive;

use Chimera\Routing\Application as ApplicationInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Application as Expressive;

final class Application implements ApplicationInterface
{
/**
* @var Expressive
*/
private $application;

public function __construct(Expressive $application)
{
$this->application = $application;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
return $this->application->handle($request);
}

public function run(): void
{
$this->application->run();
}
}
101 changes: 101 additions & 0 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);

namespace Chimera\Routing\Expressive\Tests;

use Chimera\Routing\Expressive\Application;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\ResponseFactory;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Expressive\Application as Expressive;
use Zend\Expressive\MiddlewareContainer;
use Zend\Expressive\MiddlewareFactory;
use Zend\Expressive\Router\RouteCollector;
use Zend\Expressive\Router\RouterInterface;
use Zend\HttpHandlerRunner\Emitter\EmitterInterface;
use Zend\HttpHandlerRunner\RequestHandlerRunner;
use Zend\Stratigility\MiddlewarePipeInterface;

/**
* @coversDefaultClass \Chimera\Routing\Expressive\Application
*/
final class ApplicationTest extends TestCase
{
/**
* @var Expressive
*/
private $expressive;

/**
* @var EmitterInterface&MockObject
*/
private $emitter;

/**
* @var MiddlewarePipeInterface&MockObject
*/
private $pipeline;

/**
* @before
*/
public function createDependencies(): void
{
$this->pipeline = $this->createMock(MiddlewarePipeInterface::class);
$this->emitter = $this->createMock(EmitterInterface::class);

$this->expressive = new Expressive(
new MiddlewareFactory(new MiddlewareContainer($this->createMock(ContainerInterface::class))),
$this->pipeline,
new RouteCollector($this->createMock(RouterInterface::class)),
new RequestHandlerRunner(
$this->createMock(RequestHandlerInterface::class),
$this->emitter,
[ServerRequestFactory::class, 'fromGlobals'],
[new ResponseFactory(), 'createResponse']
)
);
}

/**
* @test
*
* @covers ::__construct
* @covers ::handle
*/
public function handleShouldForwardPassRequestThroughThePipeline(): void
{
$request = $this->createMock(ServerRequestInterface::class);
$response = $this->createMock(ResponseInterface::class);

$this->pipeline->expects(self::once())
->method('handle')
->with($request)
->willReturn($response);

$application = new Application($this->expressive);

self::assertSame($response, $application->handle($request));
}

/**
* @test
*
* @covers ::__construct
* @covers ::run
*/
public function runShouldInvokeApplicationRunner(): void
{
$this->emitter->expects(self::once())
->method('emit')
->with(self::isInstanceOf(ResponseInterface::class));

$application = new Application($this->expressive);
$application->run();
}
}

0 comments on commit 0609cb5

Please sign in to comment.