Skip to content

Commit 05545a1

Browse files
committed
Improved tests
1 parent 2a3499b commit 05545a1

File tree

2 files changed

+48
-51
lines changed

2 files changed

+48
-51
lines changed

src/PhpDebugBarMiddleware.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,30 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
8080
private function getStaticFile(UriInterface $uri)
8181
{
8282
$path = $this->extractPath($uri);
83-
83+
8484
if (strpos($path, $this->debugBarRenderer->getBaseUrl()) !== 0) {
8585
return;
8686
}
8787

8888
$pathToFile = substr($path, strlen($this->debugBarRenderer->getBaseUrl()));
89-
89+
9090
$fullPathToFile = $this->debugBarRenderer->getBasePath() . $pathToFile;
91-
91+
9292
if (!file_exists($fullPathToFile)) {
9393
return;
9494
}
9595

9696
$contentType = $this->getContentTypeByFileName($fullPathToFile);
9797
$stream = new Stream($fullPathToFile, 'r');
98-
98+
9999
return new Response($stream, 200, [
100100
'Content-type' => $contentType,
101101
]);
102102
}
103-
103+
104104
/**
105105
* @param UriInterface $uri
106-
*
106+
*
107107
* @return string
108108
*/
109109
private function extractPath(UriInterface $uri)

test/Slim3Test.php

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,67 @@
11
<?php
22

3-
/*
4-
* To change this license header, choose License Headers in Project Properties.
5-
* To change this template file, choose Tools | Templates
6-
* and open the template in the editor.
7-
*/
8-
93
namespace PhpMiddlewareTest\PhpDebugBar;
104

115
use DebugBar\StandardDebugBar;
126
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
137
use PHPUnit_Framework_TestCase;
8+
use Psr\Http\Message\ResponseInterface;
149
use Slim\App;
10+
use Slim\Http\Environment;
11+
12+
class Slim3Test extends PHPUnit_Framework_TestCase
13+
{
14+
public function testAppendJsIntoHtmlContent()
15+
{
16+
$response = $this->dispatchApplication([
17+
'REQUEST_URI' => '/hello',
18+
'REQUEST_METHOD' => 'GET',
19+
'HTTP_ACCEPT' => 'text/html',
20+
]);
21+
22+
$responseBody = (string) $response->getBody();
23+
24+
$this->assertContains('var phpdebugbar = new PhpDebugBar.DebugBar();', $responseBody);
25+
$this->assertContains('Hello!', $responseBody);
26+
}
1527

16-
/**
17-
* Description of Slim3Test
18-
*
19-
* @author witold
20-
*/
21-
class Slim3Test extends PHPUnit_Framework_TestCase {
28+
public function testGetStatics()
29+
{
30+
$response = $this->dispatchApplication([
31+
'REQUEST_URI' => '/phpdebugbar/debugbar.js',
32+
'REQUEST_METHOD' => 'GET',
33+
'HTTP_ACCEPT' => 'text/html',
34+
]);
2235

23-
public function testSlim3() {
36+
$contentType = $response->getHeaderLine('Content-type');
37+
38+
$this->assertContains('text/javascript', $contentType);
39+
}
40+
41+
/**
42+
* @param array $server
43+
* @return ResponseInterface
44+
*/
45+
protected function dispatchApplication(array $server)
46+
{
2447
$app = new App();
25-
$app->getContainer()['enviroment'] = function() {
26-
$items = array(
27-
'DOCUMENT_ROOT' => '/home/witold/projects/slim3/public',
28-
'REMOTE_ADDR' => '127.0.0.1',
29-
'REMOTE_PORT' => '59638',
30-
'SERVER_SOFTWARE' => 'PHP 7.0.4-7ubuntu2.1 Development Server',
31-
'SERVER_PROTOCOL' => 'HTTP/1.1',
32-
'SERVER_NAME' => '0.0.0.0',
33-
'SERVER_PORT' => '8080',
34-
'REQUEST_URI' => '/phpdebugbar/debugbar.js',
35-
'REQUEST_METHOD' => 'GET',
36-
'SCRIPT_NAME' => '/phpdebugbar/debugbar.js',
37-
'SCRIPT_FILENAME' => '/home/witold/projects/slim3/public/public/index.php',
38-
'PHP_SELF' => '/phpdebugbar/debugbar.js',
39-
'HTTP_HOST' => '0.0.0.0:8080',
40-
'HTTP_CONNECTION' => 'keep-alive',
41-
'HTTP_CACHE_CONTROL' => 'max-age=0',
42-
'HTTP_UPGRADE_INSECURE_REQUESTS' => '1',
43-
'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
44-
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
45-
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, sdch',
46-
'HTTP_ACCEPT_LANGUAGE' => 'pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4',
47-
'HTTP_COOKIE' => 'zdt-hidden=0; PHPSESSID=tfun32lfu86islmbfk68s9eqi4',
48-
'REQUEST_TIME_FLOAT' => 1469139685.1076889,
49-
'REQUEST_TIME' => 1469139685,
50-
);
51-
return new \Slim\Http\Environment($items);
48+
$app->getContainer()['environment'] = function() use ($server) {
49+
$server['SCRIPT_NAME'] = '/index.php';
50+
return new Environment($server);
5251
};
5352

5453
$debugbar = new StandardDebugBar();
5554
$debugbarRenderer = $debugbar->getJavascriptRenderer('/phpdebugbar');
5655
$middleware = new PhpDebugBarMiddleware($debugbarRenderer);
5756
$app->add($middleware);
5857

59-
$app->get('/', function ($request, $response, $args) {
60-
$response->getBody()->write(' Hello ');
58+
$app->get('/hello', function ($request, $response, $args) {
59+
$response->getBody()->write('Hello!');
6160

6261
return $response;
6362
});
6463

65-
$response = $app->run(true);
66-
67-
$this->assertContains('phpdebugbar', (string) $response->getBody());
64+
return $app->run(true);
6865
}
6966

7067
}

0 commit comments

Comments
 (0)