Skip to content

Commit 1f8125c

Browse files
authored
Separate chain calls (#51)
1 parent 139b3b8 commit 1f8125c

File tree

2 files changed

+116
-59
lines changed

2 files changed

+116
-59
lines changed

tests/MiddlewareDispatcherTest.php

+26-11
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ public function testCallableMiddlewareCalled(): void
2828
{
2929
$request = new ServerRequest('GET', '/');
3030

31-
$dispatcher = $this->createDispatcher()->withMiddlewares([
32-
static function (): ResponseInterface {
33-
return new Response(418);
34-
},
35-
]);
31+
$dispatcher = $this
32+
->createDispatcher()
33+
->withMiddlewares([
34+
static function (): ResponseInterface {
35+
return new Response(418);
36+
},
37+
]);
3638

3739
$response = $dispatcher->dispatch($request, $this->getRequestHandler());
3840
$this->assertSame(418, $response->getStatusCode());
@@ -44,7 +46,9 @@ public function testArrayMiddlewareCall(): void
4446
$container = $this->createContainer([
4547
TestController::class => new TestController(),
4648
]);
47-
$dispatcher = $this->createDispatcher($container)->withMiddlewares([[TestController::class, 'index']]);
49+
$dispatcher = $this
50+
->createDispatcher($container)
51+
->withMiddlewares([[TestController::class, 'index']]);
4852

4953
$response = $dispatcher->dispatch($request, $this->getRequestHandler());
5054
$this->assertSame(200, $response->getStatusCode());
@@ -62,7 +66,9 @@ public function testMiddlewareFullStackCalled(): void
6266
return new Response(200, [], null, '1.1', implode($request->getAttributes()));
6367
};
6468

65-
$dispatcher = $this->createDispatcher()->withMiddlewares([$middleware1, $middleware2]);
69+
$dispatcher = $this
70+
->createDispatcher()
71+
->withMiddlewares([$middleware1, $middleware2]);
6672

6773
$response = $dispatcher->dispatch($request, $this->getRequestHandler());
6874
$this->assertSame(200, $response->getStatusCode());
@@ -80,7 +86,9 @@ public function testMiddlewareStackInterrupted(): void
8086
return new Response(200);
8187
};
8288

83-
$dispatcher = $this->createDispatcher()->withMiddlewares([$middleware1, $middleware2]);
89+
$dispatcher = $this
90+
->createDispatcher()
91+
->withMiddlewares([$middleware1, $middleware2]);
8492

8593
$response = $dispatcher->dispatch($request, $this->getRequestHandler());
8694
$this->assertSame(403, $response->getStatusCode());
@@ -99,7 +107,9 @@ public function testEventsAreDispatched(): void
99107
return new Response();
100108
};
101109

102-
$dispatcher = $this->createDispatcher(null, $eventDispatcher)->withMiddlewares([$middleware1, $middleware2]);
110+
$dispatcher = $this
111+
->createDispatcher(null, $eventDispatcher)
112+
->withMiddlewares([$middleware1, $middleware2]);
103113
$dispatcher->dispatch($request, $this->getRequestHandler());
104114

105115
$this->assertEquals(
@@ -121,7 +131,9 @@ public function testEventsAreDispatchedWhenMiddlewareFailedWithException(): void
121131
$request = new ServerRequest('GET', '/');
122132
$eventDispatcher = new SimpleEventDispatcher();
123133
$middleware = fn () => new FailMiddleware();
124-
$dispatcher = $this->createDispatcher(null, $eventDispatcher)->withMiddlewares([$middleware]);
134+
$dispatcher = $this
135+
->createDispatcher(null, $eventDispatcher)
136+
->withMiddlewares([$middleware]);
125137

126138
try {
127139
$dispatcher->dispatch($request, $this->getRequestHandler());
@@ -151,7 +163,10 @@ public function testHasMiddlewares(array $definitions, bool $expected): void
151163
{
152164
self::assertSame(
153165
$expected,
154-
$this->createDispatcher()->withMiddlewares($definitions)->hasMiddlewares()
166+
$this
167+
->createDispatcher()
168+
->withMiddlewares($definitions)
169+
->hasMiddlewares()
155170
);
156171
}
157172

tests/MiddlewareFactoryTest.php

+90-48
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,26 @@ final class MiddlewareFactoryTest extends TestCase
2828
public function testCreateFromString(): void
2929
{
3030
$container = $this->getContainer([TestMiddleware::class => new TestMiddleware()]);
31-
$middleware = $this->getMiddlewareFactory($container)->create(TestMiddleware::class);
31+
$middleware = $this
32+
->getMiddlewareFactory($container)
33+
->create(TestMiddleware::class);
3234
self::assertInstanceOf(TestMiddleware::class, $middleware);
3335
}
3436

3537
public function testCreateFromArray(): void
3638
{
3739
$container = $this->getContainer([TestController::class => new TestController()]);
38-
$middleware = $this->getMiddlewareFactory($container)->create([TestController::class, 'index']);
40+
$middleware = $this
41+
->getMiddlewareFactory($container)
42+
->create([TestController::class, 'index']);
3943
self::assertSame(
4044
'yii',
41-
$middleware->process(
42-
$this->createMock(ServerRequestInterface::class),
43-
$this->createMock(RequestHandlerInterface::class)
44-
)->getHeaderLine('test')
45+
$middleware
46+
->process(
47+
$this->createMock(ServerRequestInterface::class),
48+
$this->createMock(RequestHandlerInterface::class)
49+
)
50+
->getHeaderLine('test')
4551
);
4652
self::assertSame(
4753
[TestController::class, 'index'],
@@ -52,73 +58,91 @@ public function testCreateFromArray(): void
5258
public function testCreateFromClosureResponse(): void
5359
{
5460
$container = $this->getContainer([TestController::class => new TestController()]);
55-
$middleware = $this->getMiddlewareFactory($container)->create(
56-
static function (): ResponseInterface {
57-
return (new Response())->withStatus(418);
58-
}
59-
);
61+
$middleware = $this
62+
->getMiddlewareFactory($container)
63+
->create(
64+
static function (): ResponseInterface {
65+
return (new Response())->withStatus(418);
66+
}
67+
);
6068
self::assertSame(
6169
418,
62-
$middleware->process(
63-
$this->createMock(ServerRequestInterface::class),
64-
$this->createMock(RequestHandlerInterface::class)
65-
)->getStatusCode()
70+
$middleware
71+
->process(
72+
$this->createMock(ServerRequestInterface::class),
73+
$this->createMock(RequestHandlerInterface::class)
74+
)
75+
->getStatusCode()
6676
);
6777
}
6878

6979
public function testCreateFromClosureMiddleware(): void
7080
{
7181
$container = $this->getContainer([TestController::class => new TestController()]);
72-
$middleware = $this->getMiddlewareFactory($container)->create(
73-
static function (): MiddlewareInterface {
74-
return new TestMiddleware();
75-
}
76-
);
82+
$middleware = $this
83+
->getMiddlewareFactory($container)
84+
->create(
85+
static function (): MiddlewareInterface {
86+
return new TestMiddleware();
87+
}
88+
);
7789
self::assertSame(
7890
'42',
79-
$middleware->process(
80-
$this->createMock(ServerRequestInterface::class),
81-
$this->createMock(RequestHandlerInterface::class)
82-
)->getHeaderLine('test')
91+
$middleware
92+
->process(
93+
$this->createMock(ServerRequestInterface::class),
94+
$this->createMock(RequestHandlerInterface::class)
95+
)
96+
->getHeaderLine('test')
8397
);
8498
}
8599

86100
public function testCreateWithUseParamsMiddleware(): void
87101
{
88102
$container = $this->getContainer([UseParamsMiddleware::class => new UseParamsMiddleware()]);
89-
$middleware = $this->getMiddlewareFactory($container)->create(UseParamsMiddleware::class);
103+
$middleware = $this
104+
->getMiddlewareFactory($container)
105+
->create(UseParamsMiddleware::class);
90106

91107
self::assertSame(
92108
'GET',
93-
$middleware->process(
94-
new ServerRequest('GET', '/'),
95-
$this->getRequestHandler()
96-
)->getHeaderLine('method')
109+
$middleware
110+
->process(
111+
new ServerRequest('GET', '/'),
112+
$this->getRequestHandler()
113+
)
114+
->getHeaderLine('method')
97115
);
98116
}
99117

100118
public function testCreateWithUseParamsController(): void
101119
{
102120
$container = $this->getContainer([UseParamsController::class => new UseParamsController()]);
103-
$middleware = $this->getMiddlewareFactory($container)->create([UseParamsController::class, 'index']);
121+
$middleware = $this
122+
->getMiddlewareFactory($container)
123+
->create([UseParamsController::class, 'index']);
104124

105125
self::assertSame(
106126
'GET',
107-
$middleware->process(
108-
new ServerRequest('GET', '/'),
109-
$this->getRequestHandler()
110-
)->getHeaderLine('method')
127+
$middleware
128+
->process(
129+
new ServerRequest('GET', '/'),
130+
$this->getRequestHandler()
131+
)
132+
->getHeaderLine('method')
111133
);
112134
}
113135

114136
public function testInvalidMiddlewareWithWrongCallable(): void
115137
{
116138
$container = $this->getContainer([TestController::class => new TestController()]);
117-
$middleware = $this->getMiddlewareFactory($container)->create(
118-
static function () {
119-
return 42;
120-
}
121-
);
139+
$middleware = $this
140+
->getMiddlewareFactory($container)
141+
->create(
142+
static function () {
143+
return 42;
144+
}
145+
);
122146

123147
$this->expectException(InvalidMiddlewareDefinitionException::class);
124148
$middleware->process(
@@ -130,26 +154,34 @@ static function () {
130154
public function testInvalidMiddlewareWithWrongInstance(): void
131155
{
132156
$this->expectException(InvalidMiddlewareDefinitionException::class);
133-
$this->getMiddlewareFactory()->create(new stdClass());
157+
$this
158+
->getMiddlewareFactory()
159+
->create(new stdClass());
134160
}
135161

136162
public function testInvalidMiddlewareWithWrongString(): void
137163
{
138164
$this->expectException(InvalidMiddlewareDefinitionException::class);
139-
$this->getMiddlewareFactory()->create('test');
165+
$this
166+
->getMiddlewareFactory()
167+
->create('test');
140168
}
141169

142170
public function testInvalidMiddlewareWithWrongClass(): void
143171
{
144172
$this->expectException(InvalidMiddlewareDefinitionException::class);
145173
$this->expectExceptionMessage('Parameter should be either PSR middleware class name or a callable.');
146-
$this->getMiddlewareFactory()->create(TestController::class);
174+
$this
175+
->getMiddlewareFactory()
176+
->create(TestController::class);
147177
}
148178

149179
public function testInvalidMiddlewareWithWrongController(): void
150180
{
151181
$container = $this->getContainer([InvalidController::class => new InvalidController()]);
152-
$middleware = $this->getMiddlewareFactory($container)->create([InvalidController::class, 'index']);
182+
$middleware = $this
183+
->getMiddlewareFactory($container)
184+
->create([InvalidController::class, 'index']);
153185

154186
$this->expectException(InvalidMiddlewareDefinitionException::class);
155187
$middleware->process(
@@ -161,31 +193,41 @@ public function testInvalidMiddlewareWithWrongController(): void
161193
public function testInvalidMiddlewareWithWrongArraySize(): void
162194
{
163195
$this->expectException(InvalidMiddlewareDefinitionException::class);
164-
$this->getMiddlewareFactory()->create(['test']);
196+
$this
197+
->getMiddlewareFactory()
198+
->create(['test']);
165199
}
166200

167201
public function testInvalidMiddlewareWithWrongArrayClass(): void
168202
{
169203
$this->expectException(InvalidMiddlewareDefinitionException::class);
170-
$this->getMiddlewareFactory()->create(['class', 'test']);
204+
$this
205+
->getMiddlewareFactory()
206+
->create(['class', 'test']);
171207
}
172208

173209
public function testInvalidMiddlewareWithWrongArrayType(): void
174210
{
175211
$this->expectException(InvalidMiddlewareDefinitionException::class);
176-
$this->getMiddlewareFactory()->create(['class' => TestController::class, 'index']);
212+
$this
213+
->getMiddlewareFactory()
214+
->create(['class' => TestController::class, 'index']);
177215
}
178216

179217
public function testInvalidMiddlewareWithWrongArrayWithInstance(): void
180218
{
181219
$this->expectException(InvalidMiddlewareDefinitionException::class);
182-
$this->getMiddlewareFactory()->create([new TestController(), 'index']);
220+
$this
221+
->getMiddlewareFactory()
222+
->create([new TestController(), 'index']);
183223
}
184224

185225
public function testInvalidMiddlewareWithWrongArrayWithIntItems(): void
186226
{
187227
$this->expectException(InvalidMiddlewareDefinitionException::class);
188-
$this->getMiddlewareFactory()->create([7, 42]);
228+
$this
229+
->getMiddlewareFactory()
230+
->create([7, 42]);
189231
}
190232

191233
private function getMiddlewareFactory(ContainerInterface $container = null): MiddlewareFactoryInterface

0 commit comments

Comments
 (0)