mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-01 22:48:45 +02:00
N°2651 - Remove test directories from lib
This commit is contained in:
@@ -1,200 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bundle\WebProfilerBundle\Tests\Controller;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
|
||||
use Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Profiler\Profile;
|
||||
|
||||
class ProfilerControllerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getEmptyTokenCases
|
||||
*/
|
||||
public function testEmptyToken($token)
|
||||
{
|
||||
$urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
|
||||
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
$profiler = $this
|
||||
->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$controller = new ProfilerController($urlGenerator, $profiler, $twig, []);
|
||||
|
||||
$response = $controller->toolbarAction(Request::create('/_wdt/empty'), $token);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function getEmptyTokenCases()
|
||||
{
|
||||
return [
|
||||
[null],
|
||||
// "empty" is also a valid empty token case, see https://github.com/symfony/symfony/issues/10806
|
||||
['empty'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getOpenFileCases
|
||||
*/
|
||||
public function testOpeningDisallowedPaths($path, $isAllowed)
|
||||
{
|
||||
$urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
|
||||
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
$profiler = $this
|
||||
->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$controller = new ProfilerController($urlGenerator, $profiler, $twig, [], 'bottom', null, __DIR__.'/../..');
|
||||
|
||||
try {
|
||||
$response = $controller->openAction(Request::create('/_wdt/open', Request::METHOD_GET, ['file' => $path]));
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertTrue($isAllowed);
|
||||
} catch (NotFoundHttpException $e) {
|
||||
$this->assertFalse($isAllowed);
|
||||
}
|
||||
}
|
||||
|
||||
public function getOpenFileCases()
|
||||
{
|
||||
return [
|
||||
['README.md', true],
|
||||
['composer.json', true],
|
||||
['Controller/ProfilerController.php', true],
|
||||
['.gitignore', false],
|
||||
['../TwigBundle/README.md', false],
|
||||
['Controller/../README.md', false],
|
||||
['Controller/./ProfilerController.php', false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideCspVariants
|
||||
*/
|
||||
public function testReturns404onTokenNotFound($withCsp)
|
||||
{
|
||||
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
$profiler = $this
|
||||
->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$profiler
|
||||
->expects($this->exactly(2))
|
||||
->method('loadProfile')
|
||||
->willReturnCallback(function ($token) {
|
||||
return 'found' == $token ? new Profile($token) : null;
|
||||
})
|
||||
;
|
||||
|
||||
$controller = $this->createController($profiler, $twig, $withCsp);
|
||||
|
||||
$response = $controller->toolbarAction(Request::create('/_wdt/found'), 'found');
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
|
||||
$response = $controller->toolbarAction(Request::create('/_wdt/notFound'), 'notFound');
|
||||
$this->assertEquals(404, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideCspVariants
|
||||
*/
|
||||
public function testSearchResult($withCsp)
|
||||
{
|
||||
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
$profiler = $this
|
||||
->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$controller = $this->createController($profiler, $twig, $withCsp);
|
||||
|
||||
$tokens = [
|
||||
[
|
||||
'token' => 'token1',
|
||||
'ip' => '127.0.0.1',
|
||||
'method' => 'GET',
|
||||
'url' => 'http://example.com/',
|
||||
'time' => 0,
|
||||
'parent' => null,
|
||||
'status_code' => 200,
|
||||
],
|
||||
[
|
||||
'token' => 'token2',
|
||||
'ip' => '127.0.0.1',
|
||||
'method' => 'GET',
|
||||
'url' => 'http://example.com/not_found',
|
||||
'time' => 0,
|
||||
'parent' => null,
|
||||
'status_code' => 404,
|
||||
],
|
||||
];
|
||||
$profiler
|
||||
->expects($this->once())
|
||||
->method('find')
|
||||
->willReturn($tokens);
|
||||
|
||||
$request = Request::create('/_profiler/empty/search/results', 'GET', [
|
||||
'limit' => 2,
|
||||
'ip' => '127.0.0.1',
|
||||
'method' => 'GET',
|
||||
'url' => 'http://example.com/',
|
||||
]);
|
||||
|
||||
$twig->expects($this->once())
|
||||
->method('render')
|
||||
->with($this->stringEndsWith('results.html.twig'), $this->equalTo([
|
||||
'token' => 'empty',
|
||||
'profile' => null,
|
||||
'tokens' => $tokens,
|
||||
'ip' => '127.0.0.1',
|
||||
'method' => 'GET',
|
||||
'status_code' => null,
|
||||
'url' => 'http://example.com/',
|
||||
'start' => null,
|
||||
'end' => null,
|
||||
'limit' => 2,
|
||||
'panel' => null,
|
||||
'request' => $request,
|
||||
]));
|
||||
|
||||
$response = $controller->searchResultsAction($request, 'empty');
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function provideCspVariants()
|
||||
{
|
||||
return [
|
||||
[true],
|
||||
[false],
|
||||
];
|
||||
}
|
||||
|
||||
private function createController($profiler, $twig, $withCSP)
|
||||
{
|
||||
$urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
|
||||
|
||||
if ($withCSP) {
|
||||
$nonceGenerator = $this->getMockBuilder('Symfony\Bundle\WebProfilerBundle\Csp\NonceGenerator')->getMock();
|
||||
|
||||
return new ProfilerController($urlGenerator, $profiler, $twig, [], 'bottom', new ContentSecurityPolicyHandler($nonceGenerator));
|
||||
}
|
||||
|
||||
return new ProfilerController($urlGenerator, $profiler, $twig, []);
|
||||
}
|
||||
}
|
||||
@@ -1,207 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bundle\WebProfilerBundle\Tests\Csp;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class ContentSecurityPolicyHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideRequestAndResponses
|
||||
*/
|
||||
public function testGetNonces($nonce, $expectedNonce, Request $request, Response $response)
|
||||
{
|
||||
$cspHandler = new ContentSecurityPolicyHandler($this->mockNonceGenerator($nonce));
|
||||
|
||||
$this->assertSame($expectedNonce, $cspHandler->getNonces($request, $response));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideRequestAndResponsesForOnKernelResponse
|
||||
*/
|
||||
public function testOnKernelResponse($nonce, $expectedNonce, Request $request, Response $response, array $expectedCsp)
|
||||
{
|
||||
$cspHandler = new ContentSecurityPolicyHandler($this->mockNonceGenerator($nonce));
|
||||
|
||||
$this->assertSame($expectedNonce, $cspHandler->updateResponseHeaders($request, $response));
|
||||
|
||||
$this->assertFalse($response->headers->has('X-SymfonyProfiler-Script-Nonce'));
|
||||
$this->assertFalse($response->headers->has('X-SymfonyProfiler-Style-Nonce'));
|
||||
|
||||
foreach ($expectedCsp as $header => $value) {
|
||||
$this->assertSame($value, $response->headers->get($header));
|
||||
}
|
||||
}
|
||||
|
||||
public function provideRequestAndResponses()
|
||||
{
|
||||
$nonce = bin2hex(random_bytes(16));
|
||||
|
||||
$requestScriptNonce = 'request-with-headers-script-nonce';
|
||||
$requestStyleNonce = 'request-with-headers-style-nonce';
|
||||
|
||||
$responseScriptNonce = 'response-with-headers-script-nonce';
|
||||
$responseStyleNonce = 'response-with-headers-style-nonce';
|
||||
|
||||
$requestNonceHeaders = [
|
||||
'X-SymfonyProfiler-Script-Nonce' => $requestScriptNonce,
|
||||
'X-SymfonyProfiler-Style-Nonce' => $requestStyleNonce,
|
||||
];
|
||||
$responseNonceHeaders = [
|
||||
'X-SymfonyProfiler-Script-Nonce' => $responseScriptNonce,
|
||||
'X-SymfonyProfiler-Style-Nonce' => $responseStyleNonce,
|
||||
];
|
||||
|
||||
return [
|
||||
[$nonce, ['csp_script_nonce' => $nonce, 'csp_style_nonce' => $nonce], $this->createRequest(), $this->createResponse()],
|
||||
[$nonce, ['csp_script_nonce' => $requestScriptNonce, 'csp_style_nonce' => $requestStyleNonce], $this->createRequest($requestNonceHeaders), $this->createResponse($responseNonceHeaders)],
|
||||
[$nonce, ['csp_script_nonce' => $requestScriptNonce, 'csp_style_nonce' => $requestStyleNonce], $this->createRequest($requestNonceHeaders), $this->createResponse()],
|
||||
[$nonce, ['csp_script_nonce' => $responseScriptNonce, 'csp_style_nonce' => $responseStyleNonce], $this->createRequest(), $this->createResponse($responseNonceHeaders)],
|
||||
];
|
||||
}
|
||||
|
||||
public function provideRequestAndResponsesForOnKernelResponse()
|
||||
{
|
||||
$nonce = bin2hex(random_bytes(16));
|
||||
|
||||
$requestScriptNonce = 'request-with-headers-script-nonce';
|
||||
$requestStyleNonce = 'request-with-headers-style-nonce';
|
||||
|
||||
$responseScriptNonce = 'response-with-headers-script-nonce';
|
||||
$responseStyleNonce = 'response-with-headers-style-nonce';
|
||||
|
||||
$requestNonceHeaders = [
|
||||
'X-SymfonyProfiler-Script-Nonce' => $requestScriptNonce,
|
||||
'X-SymfonyProfiler-Style-Nonce' => $requestStyleNonce,
|
||||
];
|
||||
$responseNonceHeaders = [
|
||||
'X-SymfonyProfiler-Script-Nonce' => $responseScriptNonce,
|
||||
'X-SymfonyProfiler-Style-Nonce' => $responseStyleNonce,
|
||||
];
|
||||
|
||||
return [
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $nonce, 'csp_style_nonce' => $nonce],
|
||||
$this->createRequest(),
|
||||
$this->createResponse(),
|
||||
['Content-Security-Policy' => null, 'Content-Security-Policy-Report-Only' => null, 'X-Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce, ['csp_script_nonce' => $requestScriptNonce, 'csp_style_nonce' => $requestStyleNonce],
|
||||
$this->createRequest($requestNonceHeaders),
|
||||
$this->createResponse($responseNonceHeaders),
|
||||
['Content-Security-Policy' => null, 'Content-Security-Policy-Report-Only' => null, 'X-Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $requestScriptNonce, 'csp_style_nonce' => $requestStyleNonce],
|
||||
$this->createRequest($requestNonceHeaders),
|
||||
$this->createResponse(),
|
||||
['Content-Security-Policy' => null, 'Content-Security-Policy-Report-Only' => null, 'X-Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $responseScriptNonce, 'csp_style_nonce' => $responseStyleNonce],
|
||||
$this->createRequest(),
|
||||
$this->createResponse($responseNonceHeaders),
|
||||
['Content-Security-Policy' => null, 'Content-Security-Policy-Report-Only' => null, 'X-Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $nonce, 'csp_style_nonce' => $nonce],
|
||||
$this->createRequest(),
|
||||
$this->createResponse(['Content-Security-Policy' => 'frame-ancestors https: ; form-action: https:', 'Content-Security-Policy-Report-Only' => 'frame-ancestors http: ; form-action: http:']),
|
||||
['Content-Security-Policy' => 'frame-ancestors https: ; form-action: https:', 'Content-Security-Policy-Report-Only' => 'frame-ancestors http: ; form-action: http:', 'X-Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $nonce, 'csp_style_nonce' => $nonce],
|
||||
$this->createRequest(),
|
||||
$this->createResponse(['Content-Security-Policy' => 'default-src \'self\' domain.com; script-src \'self\' \'unsafe-inline\'', 'Content-Security-Policy-Report-Only' => 'default-src \'self\' domain-report-only.com; script-src \'self\' \'unsafe-inline\'']),
|
||||
['Content-Security-Policy' => 'default-src \'self\' domain.com; script-src \'self\' \'unsafe-inline\'; style-src \'self\' domain.com \'unsafe-inline\' \'nonce-'.$nonce.'\'', 'Content-Security-Policy-Report-Only' => 'default-src \'self\' domain-report-only.com; script-src \'self\' \'unsafe-inline\'; style-src \'self\' domain-report-only.com \'unsafe-inline\' \'nonce-'.$nonce.'\'', 'X-Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $nonce, 'csp_style_nonce' => $nonce],
|
||||
$this->createRequest(),
|
||||
$this->createResponse(['Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\'']),
|
||||
['Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\'', 'X-Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $nonce, 'csp_style_nonce' => $nonce],
|
||||
$this->createRequest(),
|
||||
$this->createResponse(['Content-Security-Policy' => 'script-src \'self\'; style-src \'self\'']),
|
||||
['Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\' \'nonce-'.$nonce.'\'; style-src \'self\' \'unsafe-inline\' \'nonce-'.$nonce.'\'', 'X-Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $nonce, 'csp_style_nonce' => $nonce],
|
||||
$this->createRequest(),
|
||||
$this->createResponse(['X-Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\'']),
|
||||
['X-Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\'', 'Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $nonce, 'csp_style_nonce' => $nonce],
|
||||
$this->createRequest(),
|
||||
$this->createResponse(['X-Content-Security-Policy' => 'script-src \'self\'']),
|
||||
['X-Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\' \'nonce-'.$nonce.'\'', 'Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $nonce, 'csp_style_nonce' => $nonce],
|
||||
$this->createRequest(),
|
||||
$this->createResponse(['X-Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\' \'sha384-LALALALALAAL\'']),
|
||||
['X-Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\' \'sha384-LALALALALAAL\' \'nonce-'.$nonce.'\'', 'Content-Security-Policy' => null],
|
||||
],
|
||||
[
|
||||
$nonce,
|
||||
['csp_script_nonce' => $nonce, 'csp_style_nonce' => $nonce],
|
||||
$this->createRequest(),
|
||||
$this->createResponse(['Content-Security-Policy' => 'script-src \'self\'; style-src \'self\'', 'X-Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\'; style-src \'self\'']),
|
||||
['Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\' \'nonce-'.$nonce.'\'; style-src \'self\' \'unsafe-inline\' \'nonce-'.$nonce.'\'', 'X-Content-Security-Policy' => 'script-src \'self\' \'unsafe-inline\'; style-src \'self\' \'unsafe-inline\' \'nonce-'.$nonce.'\''],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function createRequest(array $headers = [])
|
||||
{
|
||||
$request = new Request();
|
||||
$request->headers->add($headers);
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
private function createResponse(array $headers = [])
|
||||
{
|
||||
$response = new Response();
|
||||
$response->headers->add($headers);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function mockNonceGenerator($value)
|
||||
{
|
||||
$generator = $this->getMockBuilder('Symfony\Bundle\WebProfilerBundle\Csp\NonceGenerator')->getMock();
|
||||
|
||||
$generator->expects($this->any())
|
||||
->method('generate')
|
||||
->willReturn($value);
|
||||
|
||||
return $generator;
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bundle\WebProfilerBundle\Tests\DependencyInjection;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\WebProfilerBundle\DependencyInjection\Configuration;
|
||||
use Symfony\Component\Config\Definition\Processor;
|
||||
|
||||
class ConfigurationTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getDebugModes
|
||||
*/
|
||||
public function testConfigTree($options, $results)
|
||||
{
|
||||
$processor = new Processor();
|
||||
$configuration = new Configuration();
|
||||
$config = $processor->processConfiguration($configuration, [$options]);
|
||||
|
||||
$this->assertEquals($results, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getPositionConfig
|
||||
* @group legacy
|
||||
* @expectedDeprecation The "web_profiler.position" configuration key has been deprecated in Symfony 3.4 and it will be removed in 4.0.
|
||||
*/
|
||||
public function testPositionConfig($options, $results)
|
||||
{
|
||||
$processor = new Processor();
|
||||
$configuration = new Configuration();
|
||||
$config = $processor->processConfiguration($configuration, [$options]);
|
||||
|
||||
$this->assertEquals($results, $config);
|
||||
}
|
||||
|
||||
public function getDebugModes()
|
||||
{
|
||||
return [
|
||||
[[], ['intercept_redirects' => false, 'toolbar' => false, 'position' => 'bottom', 'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt']],
|
||||
[['intercept_redirects' => true], ['intercept_redirects' => true, 'toolbar' => false, 'position' => 'bottom', 'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt']],
|
||||
[['intercept_redirects' => false], ['intercept_redirects' => false, 'toolbar' => false, 'position' => 'bottom', 'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt']],
|
||||
[['toolbar' => true], ['intercept_redirects' => false, 'toolbar' => true, 'position' => 'bottom', 'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt']],
|
||||
[['excluded_ajax_paths' => 'test'], ['intercept_redirects' => false, 'toolbar' => false, 'position' => 'bottom', 'excluded_ajax_paths' => 'test']],
|
||||
];
|
||||
}
|
||||
|
||||
public function getPositionConfig()
|
||||
{
|
||||
return [
|
||||
[['position' => 'top'], ['intercept_redirects' => false, 'toolbar' => false, 'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt', 'position' => 'top']],
|
||||
[['position' => 'bottom'], ['intercept_redirects' => false, 'toolbar' => false, 'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt', 'position' => 'bottom']],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bundle\WebProfilerBundle\Tests\DependencyInjection;
|
||||
|
||||
use Symfony\Bundle\WebProfilerBundle\DependencyInjection\WebProfilerExtension;
|
||||
use Symfony\Bundle\WebProfilerBundle\Tests\TestCase;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
|
||||
class WebProfilerExtensionTest extends TestCase
|
||||
{
|
||||
private $kernel;
|
||||
/**
|
||||
* @var \Symfony\Component\DependencyInjection\Container
|
||||
*/
|
||||
private $container;
|
||||
|
||||
public static function assertSaneContainer(Container $container, $message = '', $knownPrivates = [])
|
||||
{
|
||||
$errors = [];
|
||||
$knownPrivates[] = 'debug.file_link_formatter.url_format';
|
||||
foreach ($container->getServiceIds() as $id) {
|
||||
if (\in_array($id, $knownPrivates, true)) { // to be removed in 4.0
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$container->get($id);
|
||||
} catch (\Exception $e) {
|
||||
$errors[$id] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
self::assertEquals([], $errors, $message);
|
||||
}
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->kernel = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\KernelInterface')->getMock();
|
||||
|
||||
$this->container = new ContainerBuilder();
|
||||
$this->container->register('event_dispatcher', EventDispatcher::class)->setPublic(true);
|
||||
$this->container->register('router', $this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'))->setPublic(true);
|
||||
$this->container->register('twig', 'Twig\Environment')->setPublic(true);
|
||||
$this->container->register('twig_loader', 'Twig\Loader\ArrayLoader')->addArgument([])->setPublic(true);
|
||||
$this->container->register('twig', 'Twig\Environment')->addArgument(new Reference('twig_loader'))->setPublic(true);
|
||||
$this->container->setParameter('kernel.bundles', []);
|
||||
$this->container->setParameter('kernel.cache_dir', __DIR__);
|
||||
$this->container->setParameter('kernel.debug', false);
|
||||
$this->container->setParameter('kernel.project_dir', __DIR__);
|
||||
$this->container->setParameter('kernel.charset', 'UTF-8');
|
||||
$this->container->setParameter('debug.file_link_format', null);
|
||||
$this->container->setParameter('profiler.class', ['Symfony\\Component\\HttpKernel\\Profiler\\Profiler']);
|
||||
$this->container->register('profiler', $this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'))
|
||||
->setPublic(true)
|
||||
->addArgument(new Definition($this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\ProfilerStorageInterface')));
|
||||
$this->container->setParameter('data_collector.templates', []);
|
||||
$this->container->set('kernel', $this->kernel);
|
||||
$this->container->addCompilerPass(new RegisterListenersPass());
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
$this->container = null;
|
||||
$this->kernel = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getDebugModes
|
||||
*/
|
||||
public function testDefaultConfig($debug)
|
||||
{
|
||||
$this->container->setParameter('kernel.debug', $debug);
|
||||
|
||||
$extension = new WebProfilerExtension();
|
||||
$extension->load([[]], $this->container);
|
||||
|
||||
$this->assertFalse($this->container->has('web_profiler.debug_toolbar'));
|
||||
|
||||
$this->assertSaneContainer($this->getCompiledContainer());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getDebugModes
|
||||
*/
|
||||
public function testToolbarConfig($toolbarEnabled, $interceptRedirects, $listenerInjected, $listenerEnabled)
|
||||
{
|
||||
$extension = new WebProfilerExtension();
|
||||
$extension->load([['toolbar' => $toolbarEnabled, 'intercept_redirects' => $interceptRedirects]], $this->container);
|
||||
|
||||
$this->assertSame($listenerInjected, $this->container->has('web_profiler.debug_toolbar'));
|
||||
|
||||
$this->assertSaneContainer($this->getCompiledContainer(), '', ['web_profiler.csp.handler']);
|
||||
|
||||
if ($listenerInjected) {
|
||||
$this->assertSame($listenerEnabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled());
|
||||
}
|
||||
}
|
||||
|
||||
public function getDebugModes()
|
||||
{
|
||||
return [
|
||||
[false, false, false, false],
|
||||
[true, false, true, true],
|
||||
[false, true, true, false],
|
||||
[true, true, true, true],
|
||||
];
|
||||
}
|
||||
|
||||
private function getCompiledContainer()
|
||||
{
|
||||
if ($this->container->has('web_profiler.debug_toolbar')) {
|
||||
$this->container->getDefinition('web_profiler.debug_toolbar')->setPublic(true);
|
||||
}
|
||||
$this->container->compile();
|
||||
$this->container->set('kernel', $this->kernel);
|
||||
|
||||
return $this->container;
|
||||
}
|
||||
}
|
||||
@@ -1,341 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bundle\WebProfilerBundle\Tests\EventListener;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener;
|
||||
use Symfony\Component\HttpFoundation\HeaderBag;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
class WebDebugToolbarListenerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getInjectToolbarTests
|
||||
*/
|
||||
public function testInjectToolbar($content, $expected)
|
||||
{
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||
$m = new \ReflectionMethod($listener, 'injectToolbar');
|
||||
$m->setAccessible(true);
|
||||
|
||||
$response = new Response($content);
|
||||
|
||||
$m->invoke($listener, $response, Request::create('/'), ['csp_script_nonce' => 'scripto', 'csp_style_nonce' => 'stylo']);
|
||||
$this->assertEquals($expected, $response->getContent());
|
||||
}
|
||||
|
||||
public function getInjectToolbarTests()
|
||||
{
|
||||
return [
|
||||
['<html><head></head><body></body></html>', "<html><head></head><body>\nWDT\n</body></html>"],
|
||||
['<html>
|
||||
<head></head>
|
||||
<body>
|
||||
<textarea><html><head></head><body></body></html></textarea>
|
||||
</body>
|
||||
</html>', "<html>
|
||||
<head></head>
|
||||
<body>
|
||||
<textarea><html><head></head><body></body></html></textarea>
|
||||
\nWDT\n</body>
|
||||
</html>"],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideRedirects
|
||||
*/
|
||||
public function testHtmlRedirectionIsIntercepted($statusCode, $hasSession)
|
||||
{
|
||||
$response = new Response('Some content', $statusCode);
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'html', $hasSession), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock('Redirection'), true);
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('Redirection', $response->getContent());
|
||||
}
|
||||
|
||||
public function testNonHtmlRedirectionIsNotIntercepted()
|
||||
{
|
||||
$response = new Response('Some content', '301');
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'json', true), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock('Redirection'), true);
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals(301, $response->getStatusCode());
|
||||
$this->assertEquals('Some content', $response->getContent());
|
||||
}
|
||||
|
||||
public function testToolbarIsInjected()
|
||||
{
|
||||
$response = new Response('<html><head></head><body></body></html>');
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals("<html><head></head><body>\nWDT\n</body></html>", $response->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testToolbarIsInjected
|
||||
*/
|
||||
public function testToolbarIsNotInjectedOnNonHtmlContentType()
|
||||
{
|
||||
$response = new Response('<html><head></head><body></body></html>');
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
$response->headers->set('Content-Type', 'text/xml');
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testToolbarIsInjected
|
||||
*/
|
||||
public function testToolbarIsNotInjectedOnContentDispositionAttachment()
|
||||
{
|
||||
$response = new Response('<html><head></head><body></body></html>');
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
$response->headers->set('Content-Disposition', 'attachment; filename=test.html');
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'html'), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testToolbarIsInjected
|
||||
* @dataProvider provideRedirects
|
||||
*/
|
||||
public function testToolbarIsNotInjectedOnRedirection($statusCode, $hasSession)
|
||||
{
|
||||
$response = new Response('<html><head></head><body></body></html>', $statusCode);
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'html', $hasSession), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
|
||||
}
|
||||
|
||||
public function provideRedirects()
|
||||
{
|
||||
return [
|
||||
[301, true],
|
||||
[302, true],
|
||||
[301, false],
|
||||
[302, false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testToolbarIsInjected
|
||||
*/
|
||||
public function testToolbarIsNotInjectedWhenThereIsNoNoXDebugTokenResponseHeader()
|
||||
{
|
||||
$response = new Response('<html><head></head><body></body></html>');
|
||||
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testToolbarIsInjected
|
||||
*/
|
||||
public function testToolbarIsNotInjectedWhenOnSubRequest()
|
||||
{
|
||||
$response = new Response('<html><head></head><body></body></html>');
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::SUB_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testToolbarIsInjected
|
||||
*/
|
||||
public function testToolbarIsNotInjectedOnIncompleteHtmlResponses()
|
||||
{
|
||||
$response = new Response('<div>Some content</div>');
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('<div>Some content</div>', $response->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testToolbarIsInjected
|
||||
*/
|
||||
public function testToolbarIsNotInjectedOnXmlHttpRequests()
|
||||
{
|
||||
$response = new Response('<html><head></head><body></body></html>');
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(true), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testToolbarIsInjected
|
||||
*/
|
||||
public function testToolbarIsNotInjectedOnNonHtmlRequests()
|
||||
{
|
||||
$response = new Response('<html><head></head><body></body></html>');
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'json'), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
|
||||
}
|
||||
|
||||
public function testXDebugUrlHeader()
|
||||
{
|
||||
$response = new Response();
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
|
||||
$urlGenerator = $this->getUrlGeneratorMock();
|
||||
$urlGenerator
|
||||
->expects($this->once())
|
||||
->method('generate')
|
||||
->with('_profiler', ['token' => 'xxxxxxxx'], UrlGeneratorInterface::ABSOLUTE_URL)
|
||||
->willReturn('http://mydomain.com/_profiler/xxxxxxxx')
|
||||
;
|
||||
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, 'bottom', $urlGenerator);
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('http://mydomain.com/_profiler/xxxxxxxx', $response->headers->get('X-Debug-Token-Link'));
|
||||
}
|
||||
|
||||
public function testThrowingUrlGenerator()
|
||||
{
|
||||
$response = new Response();
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
|
||||
$urlGenerator = $this->getUrlGeneratorMock();
|
||||
$urlGenerator
|
||||
->expects($this->once())
|
||||
->method('generate')
|
||||
->with('_profiler', ['token' => 'xxxxxxxx'])
|
||||
->willThrowException(new \Exception('foo'))
|
||||
;
|
||||
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, 'bottom', $urlGenerator);
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('Exception: foo', $response->headers->get('X-Debug-Error'));
|
||||
}
|
||||
|
||||
public function testThrowingErrorCleanup()
|
||||
{
|
||||
$response = new Response();
|
||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||
|
||||
$urlGenerator = $this->getUrlGeneratorMock();
|
||||
$urlGenerator
|
||||
->expects($this->once())
|
||||
->method('generate')
|
||||
->with('_profiler', ['token' => 'xxxxxxxx'])
|
||||
->willThrowException(new \Exception("This\nmultiline\r\ntabbed text should\tcome out\r on\n \ta single plain\r\nline"))
|
||||
;
|
||||
|
||||
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||
|
||||
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, 'bottom', $urlGenerator);
|
||||
$listener->onKernelResponse($event);
|
||||
|
||||
$this->assertEquals('Exception: This multiline tabbed text should come out on a single plain line', $response->headers->get('X-Debug-Error'));
|
||||
}
|
||||
|
||||
protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html', $hasSession = true)
|
||||
{
|
||||
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->setMethods(['getSession', 'isXmlHttpRequest', 'getRequestFormat'])->disableOriginalConstructor()->getMock();
|
||||
$request->expects($this->any())
|
||||
->method('isXmlHttpRequest')
|
||||
->willReturn($isXmlHttpRequest);
|
||||
$request->expects($this->any())
|
||||
->method('getRequestFormat')
|
||||
->willReturn($requestFormat);
|
||||
|
||||
$request->headers = new HeaderBag();
|
||||
|
||||
if ($hasSession) {
|
||||
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->disableOriginalConstructor()->getMock();
|
||||
$request->expects($this->any())
|
||||
->method('getSession')
|
||||
->willReturn($session);
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
protected function getTwigMock($render = 'WDT')
|
||||
{
|
||||
$templating = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
$templating->expects($this->any())
|
||||
->method('render')
|
||||
->willReturn($render);
|
||||
|
||||
return $templating;
|
||||
}
|
||||
|
||||
protected function getUrlGeneratorMock()
|
||||
{
|
||||
return $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
|
||||
}
|
||||
|
||||
protected function getKernelMock()
|
||||
{
|
||||
return $this->getMockBuilder('Symfony\Component\HttpKernel\Kernel')->disableOriginalConstructor()->getMock();
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
Tzo0NToiU3ltZm9ueVxDb21wb25lbnRcSHR0cEtlcm5lbFxQcm9maWxlclxQcm9maWxlIjo4OntzOjUyOiIAU3ltZm9ueVxDb21wb25lbnRcSHR0cEtlcm5lbFxQcm9maWxlclxQcm9maWxlAHRva2VuIjtzOjU6IlRPS0VOIjtzOjUzOiIAU3ltZm9ueVxDb21wb25lbnRcSHR0cEtlcm5lbFxQcm9maWxlclxQcm9maWxlAHBhcmVudCI7TjtzOjU1OiIAU3ltZm9ueVxDb21wb25lbnRcSHR0cEtlcm5lbFxQcm9maWxlclxQcm9maWxlAGNoaWxkcmVuIjthOjA6e31zOjU3OiIAU3ltZm9ueVxDb21wb25lbnRcSHR0cEtlcm5lbFxQcm9maWxlclxQcm9maWxlAGNvbGxlY3RvcnMiO2E6MDp7fXM6NDk6IgBTeW1mb255XENvbXBvbmVudFxIdHRwS2VybmVsXFByb2ZpbGVyXFByb2ZpbGUAaXAiO047czo1MzoiAFN5bWZvbnlcQ29tcG9uZW50XEh0dHBLZXJuZWxcUHJvZmlsZXJcUHJvZmlsZQBtZXRob2QiO047czo1MDoiAFN5bWZvbnlcQ29tcG9uZW50XEh0dHBLZXJuZWxcUHJvZmlsZXJcUHJvZmlsZQB1cmwiO047czo1MToiAFN5bWZvbnlcQ29tcG9uZW50XEh0dHBLZXJuZWxcUHJvZmlsZXJcUHJvZmlsZQB0aW1lIjtOO30=
|
||||
@@ -1,171 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bundle\WebProfilerBundle\Tests\Profiler;
|
||||
|
||||
use Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager;
|
||||
use Symfony\Bundle\WebProfilerBundle\Tests\TestCase;
|
||||
use Symfony\Component\HttpKernel\Profiler\Profile;
|
||||
use Twig\Environment;
|
||||
use Twig\Loader\LoaderInterface;
|
||||
use Twig\Loader\SourceContextLoaderInterface;
|
||||
|
||||
/**
|
||||
* Test for TemplateManager class.
|
||||
*
|
||||
* @author Artur Wielogórski <wodor@wodor.net>
|
||||
*/
|
||||
class TemplateManagerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Environment
|
||||
*/
|
||||
protected $twigEnvironment;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\HttpKernel\Profiler\Profiler
|
||||
*/
|
||||
protected $profiler;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager
|
||||
*/
|
||||
protected $templateManager;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$profiler = $this->mockProfiler();
|
||||
$twigEnvironment = $this->mockTwigEnvironment();
|
||||
$templates = [
|
||||
'data_collector.foo' => ['foo', 'FooBundle:Collector:foo'],
|
||||
'data_collector.bar' => ['bar', 'FooBundle:Collector:bar'],
|
||||
'data_collector.baz' => ['baz', 'FooBundle:Collector:baz'],
|
||||
];
|
||||
|
||||
$this->templateManager = new TemplateManager($profiler, $twigEnvironment, $templates);
|
||||
}
|
||||
|
||||
public function testGetNameOfInvalidTemplate()
|
||||
{
|
||||
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
|
||||
$this->templateManager->getName(new Profile('token'), 'notexistingpanel');
|
||||
}
|
||||
|
||||
/**
|
||||
* if template exists in both profile and profiler then its name should be returned.
|
||||
*/
|
||||
public function testGetNameValidTemplate()
|
||||
{
|
||||
$this->profiler->expects($this->any())
|
||||
->method('has')
|
||||
->withAnyParameters()
|
||||
->willReturnCallback([$this, 'profilerHasCallback']);
|
||||
|
||||
$this->assertEquals('FooBundle:Collector:foo.html.twig', $this->templateManager->getName(new ProfileDummy(), 'foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
* template should be loaded for 'foo' because other collectors are
|
||||
* missing in profile or in profiler.
|
||||
*/
|
||||
public function testGetTemplates()
|
||||
{
|
||||
$this->profiler->expects($this->any())
|
||||
->method('has')
|
||||
->withAnyParameters()
|
||||
->willReturnCallback([$this, 'profileHasCollectorCallback']);
|
||||
|
||||
$result = $this->templateManager->getTemplates(new ProfileDummy());
|
||||
$this->assertArrayHasKey('foo', $result);
|
||||
$this->assertArrayNotHasKey('bar', $result);
|
||||
$this->assertArrayNotHasKey('baz', $result);
|
||||
}
|
||||
|
||||
public function profilerHasCallback($panel)
|
||||
{
|
||||
switch ($panel) {
|
||||
case 'foo':
|
||||
case 'bar':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function profileHasCollectorCallback($panel)
|
||||
{
|
||||
switch ($panel) {
|
||||
case 'foo':
|
||||
case 'baz':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function mockProfile()
|
||||
{
|
||||
return $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profile')->disableOriginalConstructor()->getMock();
|
||||
}
|
||||
|
||||
protected function mockTwigEnvironment()
|
||||
{
|
||||
$this->twigEnvironment = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
|
||||
$this->twigEnvironment->expects($this->any())
|
||||
->method('loadTemplate')
|
||||
->willReturn('loadedTemplate');
|
||||
|
||||
if (Environment::MAJOR_VERSION > 1) {
|
||||
$loader = $this->createMock(LoaderInterface::class);
|
||||
$loader
|
||||
->expects($this->any())
|
||||
->method('exists')
|
||||
->willReturn(true);
|
||||
} else {
|
||||
$loader = $this->createMock(SourceContextLoaderInterface::class);
|
||||
}
|
||||
|
||||
$this->twigEnvironment->expects($this->any())->method('getLoader')->willReturn($loader);
|
||||
|
||||
return $this->twigEnvironment;
|
||||
}
|
||||
|
||||
protected function mockProfiler()
|
||||
{
|
||||
$this->profiler = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
return $this->profiler;
|
||||
}
|
||||
}
|
||||
|
||||
class ProfileDummy extends Profile
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('token');
|
||||
}
|
||||
|
||||
public function hasCollector($name)
|
||||
{
|
||||
switch ($name) {
|
||||
case 'foo':
|
||||
case 'bar':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bundle\WebProfilerBundle\Tests\Resources;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class IconTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideIconFilePaths
|
||||
*/
|
||||
public function testIconFileContents($iconFilePath)
|
||||
{
|
||||
$this->assertRegExp('~<svg xmlns="http://www.w3.org/2000/svg" width="\d+" height="\d+" viewBox="0 0 \d+ \d+">.*</svg>~s', file_get_contents($iconFilePath), sprintf('The SVG metadata of the %s icon is different than expected (use the same as the other icons).', $iconFilePath));
|
||||
}
|
||||
|
||||
public function provideIconFilePaths()
|
||||
{
|
||||
return array_map(function ($filePath) { return (array) $filePath; }, glob(__DIR__.'/../../Resources/views/Icon/*.svg'));
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bundle\WebProfilerBundle\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
|
||||
|
||||
class TestCase extends PHPUnitTestCase
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user