|
1 | 1 | <?php
|
2 | 2 |
|
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 |
| - |
9 | 3 | namespace PhpMiddlewareTest\PhpDebugBar;
|
10 | 4 |
|
11 | 5 | use DebugBar\StandardDebugBar;
|
12 | 6 | use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
|
13 | 7 | use PHPUnit_Framework_TestCase;
|
| 8 | +use Psr\Http\Message\ResponseInterface; |
14 | 9 | 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 | + } |
15 | 27 |
|
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 | + ]); |
22 | 35 |
|
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 | + { |
24 | 47 | $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); |
52 | 51 | };
|
53 | 52 |
|
54 | 53 | $debugbar = new StandardDebugBar();
|
55 | 54 | $debugbarRenderer = $debugbar->getJavascriptRenderer('/phpdebugbar');
|
56 | 55 | $middleware = new PhpDebugBarMiddleware($debugbarRenderer);
|
57 | 56 | $app->add($middleware);
|
58 | 57 |
|
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!'); |
61 | 60 |
|
62 | 61 | return $response;
|
63 | 62 | });
|
64 | 63 |
|
65 |
| - $response = $app->run(true); |
66 |
| - |
67 |
| - $this->assertContains('phpdebugbar', (string) $response->getBody()); |
| 64 | + return $app->run(true); |
68 | 65 | }
|
69 | 66 |
|
70 | 67 | }
|
0 commit comments