Skip to content

Commit

Permalink
add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa.seef-aldeen committed Aug 21, 2023
1 parent 17bb869 commit 69243f6
Show file tree
Hide file tree
Showing 10 changed files with 486 additions and 170 deletions.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"require": {
"php": "^7.4 || ^8.0",
"maximebf/debugbar": "^1.13",
"laminas/laminas-diactoros": "^2.3",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0",
"psr/container": "^1.0 || ^2.0",
Expand All @@ -30,7 +31,8 @@
"friendsofphp/php-cs-fixer": "^2.0",
"squizlabs/php_codesniffer": "^3.0",
"phpstan/phpstan": "^0.12",
"laminas/laminas-diactoros": "^2.3",
"laminas/laminas-config-aggregator": "^1.0",
"mezzio/mezzio": "^3.0",
"doctrine/orm": "^2.7"
},
"autoload": {
Expand All @@ -50,5 +52,10 @@
"test": "phpunit",
"coverage": "phpunit --coverage-text",
"coverage-html": "phpunit --coverage-html=coverage"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
46 changes: 15 additions & 31 deletions phpunit.xml.dist
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>
48 changes: 48 additions & 0 deletions tests/DataCollector/MockCollector.php
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;
}
}
135 changes: 135 additions & 0 deletions tests/DebugBarMiddlewareTest.php
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());
}
}
Loading

0 comments on commit 69243f6

Please sign in to comment.