Skip to content

Commit 233eac8

Browse files
committed
Zend expressive tests
1 parent 85897a2 commit 233eac8

File tree

5 files changed

+89
-13
lines changed

5 files changed

+89
-13
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
"require-dev": {
1818
"phpunit/phpunit": "^4.8.6",
1919
"mikey179/vfsStream": "^1.6",
20-
"slim/slim": "^3.0"
20+
"slim/slim": "^3.0",
21+
"zendframework/zend-expressive": "^1.0",
22+
"zendframework/zend-expressive-fastroute": "^1.0",
23+
"zendframework/zend-servicemanager": "^3.0"
2124
},
2225
"autoload": {
2326
"psr-4": {

test/AbstractMiddlewareRunnerTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@
44

55
use PHPUnit_Framework_TestCase;
66
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
78

89
abstract class AbstractMiddlewareRunnerTest extends PHPUnit_Framework_TestCase
910
{
11+
1012
final public function testAppendJsIntoHtmlContent()
1113
{
1214
$response = $this->dispatchApplication([
1315
'REQUEST_URI' => '/hello',
1416
'REQUEST_METHOD' => 'GET',
1517
'HTTP_ACCEPT' => 'text/html',
18+
], [
19+
'/hello' => function (ServerRequestInterface $request, ResponseInterface $response, $next) {
20+
$response->getBody()->write('Hello!');
21+
return $response;
22+
},
1623
]);
1724

1825
$responseBody = (string) $response->getBody();
@@ -49,5 +56,5 @@ final public function testGetStatics()
4956
/**
5057
* @return ResponseInterface
5158
*/
52-
abstract protected function dispatchApplication(array $server);
59+
abstract protected function dispatchApplication(array $server, array $pipe = []);
5360
}

test/Slim3Test.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,27 @@
22

33
namespace PhpMiddlewareTest\PhpDebugBar;
44

5-
use DebugBar\StandardDebugBar;
6-
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
5+
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory;
76
use Slim\App;
87
use Slim\Http\Environment;
98

109
final class Slim3Test extends AbstractMiddlewareRunnerTest
1110
{
12-
protected function dispatchApplication(array $server)
11+
protected function dispatchApplication(array $server, array $pipe = [])
1312
{
1413
$app = new App();
1514
$app->getContainer()['environment'] = function() use ($server) {
1615
return new Environment($server);
1716
};
1817

19-
$debugbar = new StandardDebugBar();
20-
$debugbarRenderer = $debugbar->getJavascriptRenderer('/phpdebugbar');
21-
$middleware = new PhpDebugBarMiddleware($debugbarRenderer);
22-
$app->add($middleware);
18+
$middlewareFactory = new PhpDebugBarMiddlewareFactory();
19+
$middleware = $middlewareFactory();
2320

24-
$app->get('/hello', function ($request, $response, $args) {
25-
$response->getBody()->write('Hello!');
21+
$app->add($middleware);
2622

27-
return $response;
28-
});
23+
foreach ($pipe as $pattern => $middleware) {
24+
$app->get($pattern, $middleware);
25+
}
2926

3027
return $app->run(true);
3128
}

test/TestEmitter.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpMiddlewareTest\PhpDebugBar;
4+
5+
use BadMethodCallException;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Zend\Diactoros\Response\EmitterInterface;
8+
9+
final class TestEmitter implements EmitterInterface
10+
{
11+
private $response;
12+
13+
public function emit(ResponseInterface $response)
14+
{
15+
$this->response = $response;
16+
17+
return $response;
18+
}
19+
20+
public function getResponse()
21+
{
22+
if ($this->response instanceof ResponseInterface) {
23+
return $this->response;
24+
}
25+
26+
throw new BadMethodCallException('Not emitted yet');
27+
}
28+
}

test/ZendExpressiveTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace PhpMiddlewareTest\PhpDebugBar;
4+
5+
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
6+
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory;
7+
use Zend\Diactoros\ServerRequestFactory;
8+
use Zend\Expressive\Application;
9+
use Zend\Expressive\Router\FastRouteRouter;
10+
use Zend\ServiceManager\ServiceManager;
11+
12+
final class ZendExpressiveTest extends AbstractMiddlewareRunnerTest
13+
{
14+
protected function dispatchApplication(array $server, array $pipe = [])
15+
{
16+
$container = new ServiceManager([
17+
'factories' => [
18+
PhpDebugBarMiddleware::class => PhpDebugBarMiddlewareFactory::class,
19+
],
20+
]);
21+
$router = new FastRouteRouter();
22+
$emitter = new TestEmitter();
23+
24+
$app = new Application($router, $container, null, $emitter);
25+
26+
$app->pipe(PhpDebugBarMiddleware::class);
27+
28+
foreach ($pipe as $pattern => $middleware) {
29+
$app->get($pattern, $middleware);
30+
}
31+
32+
$app->pipeRoutingMiddleware();
33+
$app->pipeDispatchMiddleware();
34+
35+
$serverRequest = ServerRequestFactory::fromGlobals($server);
36+
37+
$app->run($serverRequest);
38+
39+
return $emitter->getResponse();
40+
}
41+
}

0 commit comments

Comments
 (0)