Skip to content

Commit

Permalink
Improve test suite (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
snapshotpl authored Jul 1, 2019
1 parent 25cec6e commit 3120159
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
composer.lock
composer.lock
infection.log
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
"psr/http-factory-implementation": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^7.1.2",
"phpunit/phpunit": "^7.5.13",
"mikey179/vfsStream": "^1.6.4",
"slim/slim": "^3.0",
"zendframework/zend-expressive": "^3.0",
"zendframework/zend-expressive-fastroute": "^3.0.1",
"zendframework/zend-servicemanager": "^3.3.2",
"zendframework/zend-diactoros": "^2.0"
"zendframework/zend-diactoros": "^2.0",
"infection/infection": "^0.13.3"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 14 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"timeout": 10,
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "infection.log"
},
"mutators": {
"@default": true
}
}
35 changes: 20 additions & 15 deletions src/PhpDebugBarMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,14 @@ public function process(ServerRequest $request, RequestHandler $handler): Respon

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

$forceHeaderValue = $request->getHeaderLine(self::FORCE_KEY);
$forceCookieValue = $request->getCookieParams()[self::FORCE_KEY] ?? '';
$forceAttibuteValue = $request->getAttribute(self::FORCE_KEY, '');
$isForceEnable = in_array('true', [$forceHeaderValue, $forceCookieValue, $forceAttibuteValue], true);
$isForceDisable = in_array('false', [$forceHeaderValue, $forceCookieValue, $forceAttibuteValue], true);

if ($isForceDisable || (!$isForceEnable && ($this->isRedirect($response) || !$this->isHtmlAccepted($request)))) {
if ($this->shouldReturnResponse($request, $response)) {
return $response;
}

if ($this->isHtmlResponse($response)) {
return $this->attachDebugBarToResponse($response);
return $this->attachDebugBarToHtmlResponse($response);
}

return $this->prepareHtmlResponseWithDebugBar($response);
}

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

private function shouldReturnResponse(ServerRequest $request, Response $response): bool
{
$forceHeaderValue = $request->getHeaderLine(self::FORCE_KEY);
$forceCookieValue = $request->getCookieParams()[self::FORCE_KEY] ?? '';
$forceAttibuteValue = $request->getAttribute(self::FORCE_KEY, '');
$isForceEnable = in_array('true', [$forceHeaderValue, $forceCookieValue, $forceAttibuteValue], true);
$isForceDisable = in_array('false', [$forceHeaderValue, $forceCookieValue, $forceAttibuteValue], true);

return $isForceDisable || (!$isForceEnable && ($this->isRedirect($response) || !$this->isHtmlAccepted($request)));
}

private function prepareHtmlResponseWithDebugBar(Response $response): Response
{
$head = $this->debugBarRenderer->renderHead();
Expand All @@ -93,12 +99,12 @@ private function prepareHtmlResponseWithDebugBar(Response $response): Response

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

return $this->responseFactory->createResponse(200)
return $this->responseFactory->createResponse()
->withBody($stream)
->withAddedHeader('Content-type', 'text/html');
}

private function attachDebugBarToResponse(Response $response): Response
private function attachDebugBarToHtmlResponse(Response $response): Response
{
$head = $this->debugBarRenderer->renderHead();
$body = $this->debugBarRenderer->render();
Expand Down Expand Up @@ -129,9 +135,9 @@ private function getStaticFile(UriInterface $uri): ?Response
}

$contentType = $this->getContentTypeByFileName($fullPathToFile);
$stream = $this->streamFactory->createStreamFromResource(fopen($fullPathToFile, 'r'));
$stream = $this->streamFactory->createStreamFromResource(fopen($fullPathToFile, 'rb'));

return $this->responseFactory->createResponse(200)
return $this->responseFactory->createResponse()
->withBody($stream)
->withAddedHeader('Content-type', $contentType);
}
Expand Down Expand Up @@ -185,14 +191,13 @@ private function isRedirect(Response $response): bool
{
$statusCode = $response->getStatusCode();

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

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

if (! empty($headers)) {
Expand All @@ -207,7 +212,7 @@ private function serializeResponse(Response $response) : string
$response->getStatusCode(),
($reasonPhrase ? ' ' . $reasonPhrase : ''),
$headers,
$body
$response->getBody()
);
}

Expand Down
34 changes: 30 additions & 4 deletions test/PhpDebugBarMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class PhpDebugBarMiddlewareTest extends TestCase
{
protected $debugbarRenderer;
/** @var PhpDebugBarMiddleware */
protected $middleware;

protected function setUp()
Expand Down Expand Up @@ -76,6 +77,7 @@ public function testForceAttachDebugbarIfHeaderPresents(): void

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

$this->assertSame(200, $result->getStatusCode());
$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());
}

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

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

$this->assertTrue($requestHandler->isCalled(), 'Request handler is not called');
$this->assertNotSame($response, $result);
$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());
$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());
}

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

$requestHandler = new RequestHandlerStub($response);

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

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

public function testAttachToNonRedirectResponse(): void
{
$request = new ServerRequest([], [], null, null, 'php://input', ['Accept' => 'text/html']);
$response = (new Response())->withStatus(299)->withAddedHeader('Location', 'some-location');

$requestHandler = new RequestHandlerStub($response);

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

$this->assertNotSame($response, $result);
}

public function testAttachToNonRedirectResponse2(): void
{
$request = new ServerRequest([], [], null, null, 'php://input', ['Accept' => 'text/html']);
$response = (new Response())->withStatus(400)->withAddedHeader('Location', 'some-location');

$requestHandler = new RequestHandlerStub($response);

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

$this->assertNotSame($response, $result);
}

public function testAttachToRedirectResponseWithoutLocation(): void
{
$request = new ServerRequest([], [], null, null, 'php://input', ['Accept' => 'text/html']);
Expand Down Expand Up @@ -268,6 +293,7 @@ public function testHandleStaticFile(string $extension, string $contentType): vo
$this->assertFalse($requestHandler->isCalled(), 'Request handler is called');
$this->assertNotSame($response, $result);
$this->assertSame($contentType, $result->getHeaderLine('Content-type'));
$this->assertSame(200, $result->getStatusCode());
$this->assertSame('filecontent', (string) $result->getBody());
}

Expand Down

0 comments on commit 3120159

Please sign in to comment.