forked from middlewares/debugbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mostafa.seef-aldeen
committed
Aug 21, 2023
1 parent
17bb869
commit 69243f6
Showing
10 changed files
with
486 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
beStrictAboutOutputDuringTests="true" | ||
beStrictAboutTestsThatDoNotTestAnything="true" | ||
beStrictAboutTodoAnnotatedTests="true" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
failOnWarning="true"> | ||
<testsuites> | ||
<testsuite name="Debugbar test suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true"> | ||
<directory>./src</directory> | ||
<exclude> | ||
<directory>./tests</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutTodoAnnotatedTests="true" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" failOnWarning="true"> | ||
<coverage includeUncoveredFiles="true" processUncoveredFiles="true"> | ||
<include> | ||
<directory>./src</directory> | ||
</include> | ||
<exclude> | ||
<directory>./tests</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Debugbar test suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mezzio\DebugBar\Tests\DataCollector; | ||
|
||
use DebugBar\DataCollector\DataCollector; | ||
use DebugBar\DataCollector\Renderable; | ||
|
||
class MockCollector extends DataCollector implements Renderable | ||
{ | ||
protected array $data; | ||
protected string $name; | ||
protected array $widgets; | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function __construct($data = [], $name = 'mock', $widgets = []) | ||
{ | ||
$this->data = $data; | ||
$this->name = $name; | ||
$this->widgets = $widgets; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function collect() | ||
{ | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getWidgets() | ||
{ | ||
return $this->widgets; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mezzio\DebugBar\Tests; | ||
|
||
use DebugBar\DataCollector\ConfigCollector; | ||
use DebugBar\DebugBar; | ||
use Laminas\Diactoros\Response; | ||
use Laminas\Diactoros\ResponseFactory; | ||
use Laminas\Diactoros\ServerRequest; | ||
use Laminas\Diactoros\StreamFactory; | ||
use Mezzio\DebugBar\DebugBarMiddleware; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DebugBarMiddlewareTest extends TestCase | ||
{ | ||
protected DebugBar $debugbar; | ||
protected DebugBarMiddleware $middleware; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->debugbar = $this->createMock(DebugBar::class); | ||
$this->debugbarRenderer = $this->debugbar->getJavascriptRenderer(); | ||
$this->responseFactory = new ResponseFactory(); | ||
$this->streamFactory = new StreamFactory(); | ||
$this->config = [ | ||
'disable' => false, | ||
'captureAjax' => true, | ||
'inline' => false, | ||
'collectors' => [ | ||
ConfigCollector::class, | ||
], | ||
'javascript_renderer' => [ | ||
'base_url' => '/phpdebugbar', | ||
'ajax_handler_bind_to_jquery' => false, | ||
'bind_ajax_handler_to_fetch' => true, | ||
'bind_ajax_handler_to_xhr' => true, | ||
], | ||
'storage' => null, | ||
]; | ||
$this->middleware = new DebugBarMiddleware( | ||
new DebugBar(), | ||
$this->responseFactory, | ||
$this->streamFactory, | ||
$this->config | ||
); | ||
} | ||
|
||
public function testConstructor(): void | ||
{ | ||
$middleware = new DebugBarMiddleware( | ||
new DebugBar(), | ||
$this->responseFactory, | ||
$this->streamFactory, | ||
$this->config | ||
); | ||
self::assertInstanceOf(DebugBarMiddleware::class, $middleware); | ||
} | ||
|
||
public function testNotAttachIfNotAccept(): void | ||
{ | ||
$request = new ServerRequest(); | ||
$response = new Response(); | ||
$response->getBody()->write('ResponseBody'); | ||
$requestHandler = new RequestHandlerStub($response); | ||
|
||
$result = $this->middleware->process($request, $requestHandler); | ||
|
||
$this->assertTrue($requestHandler->isCalled(), 'Request handler is not called'); | ||
$this->assertSame('ResponseBody', (string) $result->getBody()); | ||
$this->assertSame($response, $result); | ||
} | ||
|
||
public function testDisableDebugbarIfHeaderPresents(): void | ||
{ | ||
$request = new ServerRequest( | ||
[], | ||
[], | ||
null, | ||
null, | ||
'php://input', | ||
['Accept' => 'application/json', 'X-Disable-Debug-Bar' => 'true'] | ||
); | ||
$response = new Response(); | ||
$response->getBody()->write('ResponseBody'); | ||
$requestHandler = new RequestHandlerStub($response); | ||
|
||
$result = $this->middleware->process($request, $requestHandler); | ||
|
||
$this->assertSame(200, $result->getStatusCode()); | ||
$this->assertSame("ResponseBody", (string) $result->getBody()); | ||
} | ||
|
||
public function testDisableDebugbarIfCookiePresents(): void | ||
{ | ||
$cookies = ['X-Disable-Debug-Bar' => 'true']; | ||
$request = new ServerRequest( | ||
[], | ||
[], | ||
null, | ||
null, | ||
'php://input', | ||
['Accept' => 'application/json'], | ||
$cookies | ||
); | ||
$response = new Response(); | ||
$response->getBody()->write('ResponseBody'); | ||
$requestHandler = new RequestHandlerStub($response); | ||
|
||
$result = $this->middleware->process($request, $requestHandler); | ||
|
||
$this->assertSame("ResponseBody", (string) $result->getBody()); | ||
} | ||
|
||
public function testDisableDebugbarIfAttributePresents(): void | ||
{ | ||
$request = new ServerRequest( | ||
[], | ||
[], | ||
null, | ||
null, | ||
'php://input', | ||
['Accept' => 'application/json'] | ||
); | ||
$request = $request->withAttribute('X-Disable-Debug-Bar', 'true'); | ||
$response = new Response(); | ||
$response->getBody()->write('ResponseBody'); | ||
$requestHandler = new RequestHandlerStub($response); | ||
|
||
$result = $this->middleware->process($request, $requestHandler); | ||
|
||
$this->assertSame("ResponseBody", (string) $result->getBody()); | ||
} | ||
} |
Oops, something went wrong.