Skip to content

Commit 7dd1acd

Browse files
authored
Separate chain calls (#52)
1 parent 19024dd commit 7dd1acd

File tree

6 files changed

+117
-41
lines changed

6 files changed

+117
-41
lines changed

src/ErrorData.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public function addToResponse(ResponseInterface $response): ResponseInterface
5454
$response = $response->withHeader($name, $value);
5555
}
5656

57-
$response->getBody()->write($this->content);
57+
$response
58+
->getBody()
59+
->write($this->content);
5860
return $response;
5961
}
6062
}

src/Renderer/HtmlRenderer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ public function renderRequest(ServerRequestInterface $request): string
299299
public function renderCurl(ServerRequestInterface $request): string
300300
{
301301
try {
302-
$output = (new Command())->setRequest($request)->build();
302+
$output = (new Command())
303+
->setRequest($request)
304+
->build();
303305
} catch (Throwable $e) {
304306
$output = 'Error generating curl command: ' . $e->getMessage();
305307
}

tests/Middleware/ErrorCatcherTest.php

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,37 @@ final class ErrorCatcherTest extends TestCase
2525
{
2626
public function testProcessWithHeadRequestMethod(): void
2727
{
28-
$response = $this->createErrorCatcher()->process(
29-
$this->createServerRequest('HEAD', ['Accept' => ['test/html']]),
30-
$this->createRequestHandlerWithThrowable(),
31-
);
32-
$response->getBody()->rewind();
33-
$content = $response->getBody()->getContents();
28+
$response = $this
29+
->createErrorCatcher()
30+
->process(
31+
$this->createServerRequest('HEAD', ['Accept' => ['test/html']]),
32+
$this->createRequestHandlerWithThrowable(),
33+
);
34+
$response
35+
->getBody()
36+
->rewind();
37+
$content = $response
38+
->getBody()
39+
->getContents();
3440

3541
$this->assertEmpty($content);
3642
$this->assertSame([HeaderRenderer::DEFAULT_ERROR_MESSAGE], $response->getHeader('X-Error-Message'));
3743
}
3844

3945
public function testProcessWithFailAcceptRequestHeader(): void
4046
{
41-
$response = $this->createErrorCatcher()->process(
42-
$this->createServerRequest('GET', ['Accept' => ['text/plain;q=2.0']]),
43-
$this->createRequestHandlerWithThrowable(),
44-
);
45-
$response->getBody()->rewind();
46-
$content = $response->getBody()->getContents();
47+
$response = $this
48+
->createErrorCatcher()
49+
->process(
50+
$this->createServerRequest('GET', ['Accept' => ['text/plain;q=2.0']]),
51+
$this->createRequestHandlerWithThrowable(),
52+
);
53+
$response
54+
->getBody()
55+
->rewind();
56+
$content = $response
57+
->getBody()
58+
->getContents();
4759

4860
$this->assertNotSame(PlainTextRenderer::DEFAULT_ERROR_MESSAGE, $content);
4961
$this->assertStringContainsString('<html', $content);
@@ -52,13 +64,19 @@ public function testProcessWithFailAcceptRequestHeader(): void
5264
public function testAddedRenderer(): void
5365
{
5466
$mimeType = 'test/test';
55-
$catcher = $this->createErrorCatcher()->withRenderer($mimeType, PlainTextRenderer::class);
67+
$catcher = $this
68+
->createErrorCatcher()
69+
->withRenderer($mimeType, PlainTextRenderer::class);
5670
$response = $catcher->process(
5771
$this->createServerRequest('GET', ['Accept' => [$mimeType]]),
5872
$this->createRequestHandlerWithThrowable(),
5973
);
60-
$response->getBody()->rewind();
61-
$content = $response->getBody()->getContents();
74+
$response
75+
->getBody()
76+
->rewind();
77+
$content = $response
78+
->getBody()
79+
->getContents();
6280

6381
$this->assertSame(PlainTextRenderer::DEFAULT_ERROR_MESSAGE, $content);
6482
}
@@ -69,77 +87,109 @@ public function testThrownExceptionWithRendererIsNotImplementThrowableRendererIn
6987
$this->expectErrorMessage(
7088
'Class "' . self::class . '" does not implement "' . ThrowableRendererInterface::class . '".',
7189
);
72-
$this->createErrorCatcher()->withRenderer('test/test', self::class);
90+
$this
91+
->createErrorCatcher()
92+
->withRenderer('test/test', self::class);
7393
}
7494

7595
public function testThrownExceptionWithInvalidContentType()
7696
{
7797
$this->expectException(InvalidArgumentException::class);
7898
$this->expectErrorMessage('Invalid content type.');
79-
$this->createErrorCatcher()->withRenderer('test invalid content type', PlainTextRenderer::class);
99+
$this
100+
->createErrorCatcher()
101+
->withRenderer('test invalid content type', PlainTextRenderer::class);
80102
}
81103

82104
public function testWithoutRenderers(): void
83105
{
84-
$catcher = $this->createErrorCatcher()->withoutRenderers();
106+
$catcher = $this
107+
->createErrorCatcher()
108+
->withoutRenderers();
85109
$response = $catcher->process(
86110
$this->createServerRequest('GET', ['Accept' => ['test/html']]),
87111
$this->createRequestHandlerWithThrowable(),
88112
);
89-
$response->getBody()->rewind();
90-
$content = $response->getBody()->getContents();
113+
$response
114+
->getBody()
115+
->rewind();
116+
$content = $response
117+
->getBody()
118+
->getContents();
91119

92120
$this->assertSame(PlainTextRenderer::DEFAULT_ERROR_MESSAGE, $content);
93121
}
94122

95123
public function testWithoutRenderer(): void
96124
{
97-
$catcher = $this->createErrorCatcher()->withoutRenderers('*/*');
125+
$catcher = $this
126+
->createErrorCatcher()
127+
->withoutRenderers('*/*');
98128
$response = $catcher->process(
99129
$this->createServerRequest('GET', ['Accept' => ['test/html']]),
100130
$this->createRequestHandlerWithThrowable(),
101131
);
102-
$response->getBody()->rewind();
103-
$content = $response->getBody()->getContents();
132+
$response
133+
->getBody()
134+
->rewind();
135+
$content = $response
136+
->getBody()
137+
->getContents();
104138

105139
$this->assertSame(PlainTextRenderer::DEFAULT_ERROR_MESSAGE, $content);
106140
}
107141

108142
public function testAdvancedAcceptHeader(): void
109143
{
110144
$contentType = 'text/html;version=2';
111-
$catcher = $this->createErrorCatcher()->withRenderer($contentType, PlainTextRenderer::class);
145+
$catcher = $this
146+
->createErrorCatcher()
147+
->withRenderer($contentType, PlainTextRenderer::class);
112148
$response = $catcher->process(
113149
$this->createServerRequest('GET', ['Accept' => ['text/html', $contentType]]),
114150
$this->createRequestHandlerWithThrowable(),
115151
);
116-
$response->getBody()->rewind();
117-
$content = $response->getBody()->getContents();
152+
$response
153+
->getBody()
154+
->rewind();
155+
$content = $response
156+
->getBody()
157+
->getContents();
118158

119159
$this->assertSame(PlainTextRenderer::DEFAULT_ERROR_MESSAGE, $content);
120160
}
121161

122162
public function testDefaultContentType(): void
123163
{
124-
$catcher = $this->createErrorCatcher()->withRenderer('*/*', PlainTextRenderer::class);
164+
$catcher = $this
165+
->createErrorCatcher()
166+
->withRenderer('*/*', PlainTextRenderer::class);
125167
$response = $catcher->process(
126168
$this->createServerRequest('GET', ['Accept' => ['test/test']]),
127169
$this->createRequestHandlerWithThrowable(),
128170
);
129-
$response->getBody()->rewind();
130-
$content = $response->getBody()->getContents();
171+
$response
172+
->getBody()
173+
->rewind();
174+
$content = $response
175+
->getBody()
176+
->getContents();
131177

132178
$this->assertSame(PlainTextRenderer::DEFAULT_ERROR_MESSAGE, $content);
133179
}
134180

135181
public function testForceContentType(): void
136182
{
137-
$catcher = $this->createErrorCatcher()->forceContentType('application/json');
183+
$catcher = $this
184+
->createErrorCatcher()
185+
->forceContentType('application/json');
138186
$response = $catcher->process(
139187
$this->createServerRequest('GET', ['Accept' => ['text/xml']]),
140188
$this->createRequestHandlerWithThrowable(),
141189
);
142-
$response->getBody()->rewind();
190+
$response
191+
->getBody()
192+
->rewind();
143193

144194
$this->assertSame('application/json', $response->getHeaderLine(Header::CONTENT_TYPE));
145195
}
@@ -148,7 +198,9 @@ public function testForceContentTypeSetInvalidType(): void
148198
{
149199
$this->expectException(InvalidArgumentException::class);
150200
$this->expectErrorMessage('The renderer for image/gif is not set.');
151-
$this->createErrorCatcher()->forceContentType('image/gif');
201+
$this
202+
->createErrorCatcher()
203+
->forceContentType('image/gif');
152204
}
153205

154206
private function createErrorHandler(): ErrorHandler

tests/Middleware/ExceptionResponderTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ public function testCode(): void
2727
DomainException::class => Status::BAD_REQUEST,
2828
]);
2929

30-
$this->assertSame(Status::BAD_REQUEST, $this->process($middleware)->getStatusCode());
30+
$this->assertSame(
31+
Status::BAD_REQUEST,
32+
$this
33+
->process($middleware)
34+
->getStatusCode(),
35+
);
3136
}
3237

3338
public function testCallable(): void
@@ -38,7 +43,12 @@ public function testCallable(): void
3843
},
3944
]);
4045

41-
$this->assertSame(Status::CREATED, $this->process($middleware)->getStatusCode());
46+
$this->assertSame(
47+
Status::CREATED,
48+
$this
49+
->process($middleware)
50+
->getStatusCode(),
51+
);
4252
}
4353

4454
public function testAnotherException(): void

tests/Renderer/HeaderRendererTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ public function testRender(): void
1616
$renderer = new HeaderRenderer();
1717
$data = $renderer->render(new RuntimeException());
1818
$response = $data->addToResponse(new Response());
19-
$response->getBody()->rewind();
19+
$response
20+
->getBody()
21+
->rewind();
2022

21-
$this->assertEmpty($response->getBody()->getContents());
23+
$this->assertEmpty($response
24+
->getBody()
25+
->getContents());
2226
$this->assertSame([HeaderRenderer::DEFAULT_ERROR_MESSAGE], $response->getHeader('X-Error-Message'));
2327
}
2428

@@ -28,9 +32,13 @@ public function testRenderVerbose(): void
2832
$throwable = new RuntimeException();
2933
$data = $renderer->renderVerbose($throwable);
3034
$response = $data->addToResponse(new Response());
31-
$response->getBody()->rewind();
35+
$response
36+
->getBody()
37+
->rewind();
3238

33-
$this->assertEmpty($response->getBody()->getContents());
39+
$this->assertEmpty($response
40+
->getBody()
41+
->getContents());
3442
$this->assertSame([RuntimeException::class], $response->getHeader('X-Error-Type'));
3543
$this->assertSame([$throwable->getMessage()], $response->getHeader('X-Error-Message'));
3644
$this->assertSame([(string) $throwable->getCode()], $response->getHeader('X-Error-Code'));

tests/Renderer/HtmlRendererTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ public function testCreateServerInformationLink(?string $serverSoftware, string
191191
{
192192
$renderer = new HtmlRenderer();
193193
$serverRequestMock = $this->createServerRequestMock();
194-
$serverRequestMock->method('getServerParams')->willReturn(['SERVER_SOFTWARE' => $serverSoftware]);
194+
$serverRequestMock
195+
->method('getServerParams')
196+
->willReturn(['SERVER_SOFTWARE' => $serverSoftware]);
195197

196198
$this->assertStringContainsString($expected, $renderer->createServerInformationLink($serverRequestMock));
197199
}

0 commit comments

Comments
 (0)