Skip to content

Commit 3120159

Browse files
authored
Improve test suite (#30)
1 parent 25cec6e commit 3120159

5 files changed

+69
-22
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
2-
composer.lock
2+
composer.lock
3+
infection.log

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
"psr/http-factory-implementation": "^1.0"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "^7.1.2",
25+
"phpunit/phpunit": "^7.5.13",
2626
"mikey179/vfsStream": "^1.6.4",
2727
"slim/slim": "^3.0",
2828
"zendframework/zend-expressive": "^3.0",
2929
"zendframework/zend-expressive-fastroute": "^3.0.1",
3030
"zendframework/zend-servicemanager": "^3.3.2",
31-
"zendframework/zend-diactoros": "^2.0"
31+
"zendframework/zend-diactoros": "^2.0",
32+
"infection/infection": "^0.13.3"
3233
},
3334
"autoload": {
3435
"psr-4": {

infection.json.dist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"timeout": 10,
3+
"source": {
4+
"directories": [
5+
"src"
6+
]
7+
},
8+
"logs": {
9+
"text": "infection.log"
10+
},
11+
"mutators": {
12+
"@default": true
13+
}
14+
}

src/PhpDebugBarMiddleware.php

+20-15
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,14 @@ public function process(ServerRequest $request, RequestHandler $handler): Respon
4646

4747
$response = $handler->handle($request);
4848

49-
$forceHeaderValue = $request->getHeaderLine(self::FORCE_KEY);
50-
$forceCookieValue = $request->getCookieParams()[self::FORCE_KEY] ?? '';
51-
$forceAttibuteValue = $request->getAttribute(self::FORCE_KEY, '');
52-
$isForceEnable = in_array('true', [$forceHeaderValue, $forceCookieValue, $forceAttibuteValue], true);
53-
$isForceDisable = in_array('false', [$forceHeaderValue, $forceCookieValue, $forceAttibuteValue], true);
54-
55-
if ($isForceDisable || (!$isForceEnable && ($this->isRedirect($response) || !$this->isHtmlAccepted($request)))) {
49+
if ($this->shouldReturnResponse($request, $response)) {
5650
return $response;
5751
}
5852

5953
if ($this->isHtmlResponse($response)) {
60-
return $this->attachDebugBarToResponse($response);
54+
return $this->attachDebugBarToHtmlResponse($response);
6155
}
56+
6257
return $this->prepareHtmlResponseWithDebugBar($response);
6358
}
6459

@@ -82,6 +77,17 @@ public function handle(ServerRequest $request): Response
8277
return $this->process($request, $handler);
8378
}
8479

80+
private function shouldReturnResponse(ServerRequest $request, Response $response): bool
81+
{
82+
$forceHeaderValue = $request->getHeaderLine(self::FORCE_KEY);
83+
$forceCookieValue = $request->getCookieParams()[self::FORCE_KEY] ?? '';
84+
$forceAttibuteValue = $request->getAttribute(self::FORCE_KEY, '');
85+
$isForceEnable = in_array('true', [$forceHeaderValue, $forceCookieValue, $forceAttibuteValue], true);
86+
$isForceDisable = in_array('false', [$forceHeaderValue, $forceCookieValue, $forceAttibuteValue], true);
87+
88+
return $isForceDisable || (!$isForceEnable && ($this->isRedirect($response) || !$this->isHtmlAccepted($request)));
89+
}
90+
8591
private function prepareHtmlResponseWithDebugBar(Response $response): Response
8692
{
8793
$head = $this->debugBarRenderer->renderHead();
@@ -93,12 +99,12 @@ private function prepareHtmlResponseWithDebugBar(Response $response): Response
9399

94100
$stream = $this->streamFactory->createStream($result);
95101

96-
return $this->responseFactory->createResponse(200)
102+
return $this->responseFactory->createResponse()
97103
->withBody($stream)
98104
->withAddedHeader('Content-type', 'text/html');
99105
}
100106

101-
private function attachDebugBarToResponse(Response $response): Response
107+
private function attachDebugBarToHtmlResponse(Response $response): Response
102108
{
103109
$head = $this->debugBarRenderer->renderHead();
104110
$body = $this->debugBarRenderer->render();
@@ -129,9 +135,9 @@ private function getStaticFile(UriInterface $uri): ?Response
129135
}
130136

131137
$contentType = $this->getContentTypeByFileName($fullPathToFile);
132-
$stream = $this->streamFactory->createStreamFromResource(fopen($fullPathToFile, 'r'));
138+
$stream = $this->streamFactory->createStreamFromResource(fopen($fullPathToFile, 'rb'));
133139

134-
return $this->responseFactory->createResponse(200)
140+
return $this->responseFactory->createResponse()
135141
->withBody($stream)
136142
->withAddedHeader('Content-type', $contentType);
137143
}
@@ -185,14 +191,13 @@ private function isRedirect(Response $response): bool
185191
{
186192
$statusCode = $response->getStatusCode();
187193

188-
return ($statusCode >= 300 || $statusCode < 400) && $response->getHeaderLine('Location') !== '';
194+
return $statusCode >= 300 && $statusCode < 400 && $response->getHeaderLine('Location') !== '';
189195
}
190196

191197
private function serializeResponse(Response $response) : string
192198
{
193199
$reasonPhrase = $response->getReasonPhrase();
194200
$headers = $this->serializeHeaders($response->getHeaders());
195-
$body = (string) $response->getBody();
196201
$format = 'HTTP/%s %d%s%s%s';
197202

198203
if (! empty($headers)) {
@@ -207,7 +212,7 @@ private function serializeResponse(Response $response) : string
207212
$response->getStatusCode(),
208213
($reasonPhrase ? ' ' . $reasonPhrase : ''),
209214
$headers,
210-
$body
215+
$response->getBody()
211216
);
212217
}
213218

test/PhpDebugBarMiddlewareTest.php

+30-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
class PhpDebugBarMiddlewareTest extends TestCase
2222
{
2323
protected $debugbarRenderer;
24+
/** @var PhpDebugBarMiddleware */
2425
protected $middleware;
2526

2627
protected function setUp()
@@ -76,6 +77,7 @@ public function testForceAttachDebugbarIfHeaderPresents(): void
7677

7778
$result = $this->middleware->process($request, $requestHandler);
7879

80+
$this->assertSame(200, $result->getStatusCode());
7981
$this->assertSame("<html><head>RenderHead</head><body><h1>DebugBar</h1><p>Response:</p><pre>HTTP/1.1 200 OK\r\n\r\nResponseBody</pre>RenderBody</body></html>", (string) $result->getBody());
8082
}
8183

@@ -108,7 +110,7 @@ public function testForceAttachDebugbarIfAttributePresents(): void
108110
public function testAttachToNoneHtmlResponse(): void
109111
{
110112
$request = new ServerRequest([], [], null, null, 'php://input', ['Accept' => 'text/html']);
111-
$response = new Response();
113+
$response = (new Response())->withHeader('test-header', 'value');
112114
$response->getBody()->write('ResponseBody');
113115

114116
$requestHandler = new RequestHandlerStub($response);
@@ -117,22 +119,45 @@ public function testAttachToNoneHtmlResponse(): void
117119

118120
$this->assertTrue($requestHandler->isCalled(), 'Request handler is not called');
119121
$this->assertNotSame($response, $result);
120-
$this->assertSame("<html><head>RenderHead</head><body><h1>DebugBar</h1><p>Response:</p><pre>HTTP/1.1 200 OK\r\n\r\nResponseBody</pre>RenderBody</body></html>", (string) $result->getBody());
122+
$this->assertSame("<html><head>RenderHead</head><body><h1>DebugBar</h1><p>Response:</p><pre>HTTP/1.1 200 OK\r\nTest-Header: value\r\n\r\nResponseBody</pre>RenderBody</body></html>", (string) $result->getBody());
121123
}
122124

123125
public function testNotAttachToRedirectResponse(): void
124126
{
125127
$request = new ServerRequest([], [], null, null, 'php://input', ['Accept' => 'text/html']);
126-
$response = (new Response())->withStatus(302)->withAddedHeader('Location', 'some-location');
128+
$response = (new Response())->withStatus(300)->withAddedHeader('Location', 'some-location');
127129

128130
$requestHandler = new RequestHandlerStub($response);
129131

130132
$result = $this->middleware->process($request, $requestHandler);
131133

132-
$this->assertTrue($requestHandler->isCalled(), 'Request handler is not called');
133134
$this->assertSame($response, $result);
134135
}
135136

137+
public function testAttachToNonRedirectResponse(): void
138+
{
139+
$request = new ServerRequest([], [], null, null, 'php://input', ['Accept' => 'text/html']);
140+
$response = (new Response())->withStatus(299)->withAddedHeader('Location', 'some-location');
141+
142+
$requestHandler = new RequestHandlerStub($response);
143+
144+
$result = $this->middleware->process($request, $requestHandler);
145+
146+
$this->assertNotSame($response, $result);
147+
}
148+
149+
public function testAttachToNonRedirectResponse2(): void
150+
{
151+
$request = new ServerRequest([], [], null, null, 'php://input', ['Accept' => 'text/html']);
152+
$response = (new Response())->withStatus(400)->withAddedHeader('Location', 'some-location');
153+
154+
$requestHandler = new RequestHandlerStub($response);
155+
156+
$result = $this->middleware->process($request, $requestHandler);
157+
158+
$this->assertNotSame($response, $result);
159+
}
160+
136161
public function testAttachToRedirectResponseWithoutLocation(): void
137162
{
138163
$request = new ServerRequest([], [], null, null, 'php://input', ['Accept' => 'text/html']);
@@ -268,6 +293,7 @@ public function testHandleStaticFile(string $extension, string $contentType): vo
268293
$this->assertFalse($requestHandler->isCalled(), 'Request handler is called');
269294
$this->assertNotSame($response, $result);
270295
$this->assertSame($contentType, $result->getHeaderLine('Content-type'));
296+
$this->assertSame(200, $result->getStatusCode());
271297
$this->assertSame('filecontent', (string) $result->getBody());
272298
}
273299

0 commit comments

Comments
 (0)