Skip to content

Commit f56e872

Browse files
authored
Refactor middleware factory and tests
1 parent 402cf9f commit f56e872

8 files changed

+86
-135
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"psr/http-message": "^1.0",
2424
"psr/http-server-handler": "^1.0",
2525
"psr/http-server-middleware": "^1.0",
26+
"psr/container": "^1.0|^2.0",
2627
"yiisoft/injector": "^1.0"
2728
},
2829
"require-dev": {

src/MiddlewareFactory.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,15 @@ public function create($middlewareDefinition): MiddlewareInterface
5151
private function wrapCallable($callback): MiddlewareInterface
5252
{
5353
if (is_array($callback)) {
54-
/**
55-
* @var string $controller
56-
* @var string $action
57-
*/
58-
[$controller, $action] = $callback;
59-
return new class($controller, $action, $this->container, $callback) implements MiddlewareInterface {
54+
return new class($this->container, $callback) implements MiddlewareInterface {
6055
private string $class;
6156
private string $method;
6257
private ContainerInterface $container;
6358
private array $callback;
6459

65-
public function __construct(string $class, string $method, ContainerInterface $container, array $callback)
60+
public function __construct(ContainerInterface $container, array $callback)
6661
{
67-
$this->class = $class;
68-
$this->method = $method;
62+
[$this->class , $this->method] = $callback;
6963
$this->container = $container;
7064
$this->callback = $callback;
7165
}

src/MiddlewareStack.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
8484
{
8585
$this->eventDispatcher->dispatch(new BeforeMiddleware($this->middleware, $request));
8686

87-
$response = null;
8887
try {
8988
return $response = $this->middleware->process($request, $this->handler);
9089
} finally {
91-
$this->eventDispatcher->dispatch(new AfterMiddleware($this->middleware, $response));
90+
$this->eventDispatcher->dispatch(new AfterMiddleware($this->middleware, $response ?? null));
9291
}
9392
}
9493
};

tests/MiddlewareDispatcherTest.php

+35-36
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,34 @@
1717
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
1818
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory;
1919
use Yiisoft\Middleware\Dispatcher\MiddlewareStack;
20-
use Yiisoft\Middleware\Dispatcher\Tests\Support\Container;
21-
use Yiisoft\Middleware\Dispatcher\Tests\Support\MockEventDispatcher;
20+
use Yiisoft\Middleware\Dispatcher\Tests\Support\FailMiddleware;
2221
use Yiisoft\Middleware\Dispatcher\Tests\Support\TestController;
2322
use Yiisoft\Middleware\Dispatcher\Tests\Support\TestMiddleware;
23+
use Yiisoft\Test\Support\Container\SimpleContainer;
24+
use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher;
2425

2526
final class MiddlewareDispatcherTest extends TestCase
2627
{
27-
public function testAddMiddleware(): void
28-
{
29-
$container = $this->createMock(ContainerInterface::class);
30-
$request = new ServerRequest('GET', '/');
31-
32-
$dispatcher = $this->createDispatcher($container)->withMiddlewares([
33-
function () {
34-
return new Response(418);
35-
},
36-
]);
37-
38-
$response = $dispatcher->dispatch($request, $this->getRequestHandler());
39-
$this->assertSame(418, $response->getStatusCode());
40-
}
41-
42-
public function testAddCallableMiddleware(): void
28+
public function testCallableMiddlewareCalled(): void
4329
{
4430
$request = new ServerRequest('GET', '/');
4531

4632
$dispatcher = $this->createDispatcher()->withMiddlewares([
4733
static function (): ResponseInterface {
48-
return (new Response())->withStatus(418);
34+
return new Response(418);
4935
},
5036
]);
5137

5238
$response = $dispatcher->dispatch($request, $this->getRequestHandler());
5339
$this->assertSame(418, $response->getStatusCode());
5440
}
5541

56-
public function testAddCallableArrayMiddleware(): void
42+
public function testArrayMiddlewareCall(): void
5743
{
5844
$request = new ServerRequest('GET', '/');
59-
$container = $this->createContainer([TestController::class => new TestController()]);
45+
$container = $this->createContainer([
46+
TestController::class => new TestController(),
47+
]);
6048
$dispatcher = $this->createDispatcher($container)->withMiddlewares([[TestController::class, 'index']]);
6149

6250
$response = $dispatcher->dispatch($request, $this->getRequestHandler());
@@ -99,21 +87,9 @@ public function testMiddlewareStackInterrupted(): void
9987
$this->assertSame(403, $response->getStatusCode());
10088
}
10189

102-
public function testArrayMiddlewareSuccessfulCall(): void
103-
{
104-
$request = new ServerRequest('GET', '/');
105-
$container = $this->createContainer([
106-
TestController::class => new TestController(),
107-
]);
108-
$dispatcher = $this->createDispatcher($container)->withMiddlewares([[TestController::class, 'index']]);
109-
110-
$response = $dispatcher->dispatch($request, $this->getRequestHandler());
111-
$this->assertSame(200, $response->getStatusCode());
112-
}
113-
11490
public function testEventsAreDispatched(): void
11591
{
116-
$eventDispatcher = new MockEventDispatcher();
92+
$eventDispatcher = new SimpleEventDispatcher();
11793

11894
$request = new ServerRequest('GET', '/');
11995

@@ -134,10 +110,33 @@ public function testEventsAreDispatched(): void
134110
AfterMiddleware::class,
135111
AfterMiddleware::class,
136112
],
137-
$eventDispatcher->getClassesEvents()
113+
$eventDispatcher->getEventClasses()
138114
);
139115
}
140116

117+
public function testEventsAreDispatchedWhenMiddlewareFailedWithException(): void
118+
{
119+
$this->expectException(\RuntimeException::class);
120+
$this->expectExceptionMessage('Middleware failed.');
121+
122+
$request = new ServerRequest('GET', '/');
123+
$eventDispatcher = new SimpleEventDispatcher();
124+
$middleware = fn () => new FailMiddleware();
125+
$dispatcher = $this->createDispatcher(null, $eventDispatcher)->withMiddlewares([$middleware]);
126+
127+
try {
128+
$dispatcher->dispatch($request, $this->getRequestHandler());
129+
} finally {
130+
$this->assertEquals(
131+
[
132+
BeforeMiddleware::class,
133+
AfterMiddleware::class,
134+
],
135+
$eventDispatcher->getEventClasses()
136+
);
137+
}
138+
}
139+
141140
public function dataHasMiddlewares(): array
142141
{
143142
return [
@@ -214,6 +213,6 @@ private function createDispatcher(ContainerInterface $container = null, ?EventDi
214213

215214
private function createContainer(array $instances = []): ContainerInterface
216215
{
217-
return new Container($instances);
216+
return new SimpleContainer($instances);
218217
}
219218
}

tests/MiddlewareFactoryTest.php

+28-22
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
use Yiisoft\Middleware\Dispatcher\InvalidMiddlewareDefinitionException;
1717
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory;
1818
use Yiisoft\Middleware\Dispatcher\MiddlewareFactoryInterface;
19-
use Yiisoft\Middleware\Dispatcher\Tests\Support\Container;
2019
use Yiisoft\Middleware\Dispatcher\Tests\Support\UseParamsController;
2120
use Yiisoft\Middleware\Dispatcher\Tests\Support\UseParamsMiddleware;
2221
use Yiisoft\Middleware\Dispatcher\Tests\Support\InvalidController;
2322
use Yiisoft\Middleware\Dispatcher\Tests\Support\TestController;
2423
use Yiisoft\Middleware\Dispatcher\Tests\Support\TestMiddleware;
24+
use Yiisoft\Test\Support\Container\SimpleContainer;
2525

2626
final class MiddlewareFactoryTest extends TestCase
2727
{
@@ -45,18 +45,6 @@ public function testCreateFromArray(): void
4545
);
4646
}
4747

48-
public function testCreateInvalidFromArray(): void
49-
{
50-
$container = $this->getContainer([InvalidController::class => new InvalidController()]);
51-
$middleware = $this->getMiddlewareFactory($container)->create([InvalidController::class, 'index']);
52-
53-
$this->expectException(InvalidMiddlewareDefinitionException::class);
54-
$middleware->process(
55-
$this->createMock(ServerRequestInterface::class),
56-
$this->createMock(RequestHandlerInterface::class)
57-
);
58-
}
59-
6048
public function testCreateFromClosureResponse(): void
6149
{
6250
$container = $this->getContainer([TestController::class => new TestController()]);
@@ -119,7 +107,7 @@ public function testCreateWithUseParamsController(): void
119107
);
120108
}
121109

122-
public function testCreateWithInvalidCallback(): void
110+
public function testInvalidMiddlewareWithWrongCallable(): void
123111
{
124112
$container = $this->getContainer([TestController::class => new TestController()]);
125113
$middleware = $this->getMiddlewareFactory($container)->create(
@@ -135,44 +123,62 @@ static function () {
135123
);
136124
}
137125

138-
public function testInvalidMiddleware(): void
126+
public function testInvalidMiddlewareWithWrongInstance(): void
139127
{
140128
$this->expectException(InvalidMiddlewareDefinitionException::class);
141129
$this->getMiddlewareFactory()->create(new stdClass());
142130
}
143131

144-
public function testInvalidMiddlewareAddWrongString(): void
132+
public function testInvalidMiddlewareWithWrongString(): void
145133
{
146134
$this->expectException(InvalidMiddlewareDefinitionException::class);
147135
$this->getMiddlewareFactory()->create('test');
148136
}
149137

150-
public function testInvalidMiddlewareAddWrongStringClass(): void
138+
public function testInvalidMiddlewareWithWrongClass(): void
151139
{
152140
$this->expectException(InvalidMiddlewareDefinitionException::class);
153141
$this->expectExceptionMessage('Parameter should be either PSR middleware class name or a callable.');
154142
$this->getMiddlewareFactory()->create(TestController::class);
155143
}
156144

157-
public function testInvalidMiddlewareAddWrongArraySize(): void
145+
public function testInvalidMiddlewareWithWrongController(): void
146+
{
147+
$container = $this->getContainer([InvalidController::class => new InvalidController()]);
148+
$middleware = $this->getMiddlewareFactory($container)->create([InvalidController::class, 'index']);
149+
150+
$this->expectException(InvalidMiddlewareDefinitionException::class);
151+
$middleware->process(
152+
$this->createMock(ServerRequestInterface::class),
153+
$this->createMock(RequestHandlerInterface::class)
154+
);
155+
}
156+
157+
public function testInvalidMiddlewareWithWrongArraySize(): void
158158
{
159159
$this->expectException(InvalidMiddlewareDefinitionException::class);
160160
$this->getMiddlewareFactory()->create(['test']);
161161
}
162162

163-
public function testInvalidMiddlewareAddWrongArrayClass(): void
163+
public function testInvalidMiddlewareWithWrongArrayClass(): void
164164
{
165165
$this->expectException(InvalidMiddlewareDefinitionException::class);
166166
$this->getMiddlewareFactory()->create(['class', 'test']);
167167
}
168168

169-
public function testInvalidMiddlewareAddWrongArrayType(): void
169+
public function testInvalidMiddlewareWithWrongArrayType(): void
170170
{
171171
$this->expectException(InvalidMiddlewareDefinitionException::class);
172172
$this->getMiddlewareFactory()->create(['class' => TestController::class, 'index']);
173173
}
174174

175-
public function testInvalidMiddlewareAddWrongArrayWithIntItems(): void
175+
public function testInvalidMiddlewareWithWrongArrayWithInstance(): void
176+
{
177+
$this->expectException(InvalidMiddlewareDefinitionException::class);
178+
$this->getMiddlewareFactory()->create([new TestController(), 'index']);
179+
}
180+
181+
public function testInvalidMiddlewareWithWrongArrayWithIntItems(): void
176182
{
177183
$this->expectException(InvalidMiddlewareDefinitionException::class);
178184
$this->getMiddlewareFactory()->create([7, 42]);
@@ -189,7 +195,7 @@ private function getMiddlewareFactory(ContainerInterface $container = null): Mid
189195

190196
private function getContainer(array $instances = []): ContainerInterface
191197
{
192-
return new Container($instances);
198+
return new SimpleContainer($instances);
193199
}
194200

195201
private function getRequestHandler(): RequestHandlerInterface

tests/Support/Container.php

-31
This file was deleted.

tests/Support/FailMiddleware.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Middleware\Dispatcher\Tests\Support;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
use Psr\Http\Server\MiddlewareInterface;
10+
use Psr\Http\Server\RequestHandlerInterface;
11+
12+
final class FailMiddleware implements MiddlewareInterface
13+
{
14+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
15+
{
16+
throw new \RuntimeException('Middleware failed.');
17+
}
18+
}

tests/Support/MockEventDispatcher.php

-35
This file was deleted.

0 commit comments

Comments
 (0)