/
ProfilerTest.php
84 lines (70 loc) · 3.4 KB
/
ProfilerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace Doctrine\Bundle\DoctrineBundle\Tests;
use Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector;
use Doctrine\Bundle\DoctrineBundle\Twig\DoctrineExtension;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Logging\DebugStack;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Symfony\Bridge\Twig\Extension\CodeExtension;
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bundle\WebProfilerBundle\Twig\WebProfilerExtension;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\RuntimeLoader\RuntimeLoaderInterface;
class ProfilerTest extends BaseTestCase
{
/** @var DebugStack */
private $logger;
/** @var Environment */
private $twig;
/** @var DoctrineDataCollector */
private $collector;
public function setUp()
{
$this->logger = new DebugStack();
$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
$registry->expects($this->once())->method('getManagers')->willReturn([]);
$this->collector = new DoctrineDataCollector($registry);
$this->collector->addLogger('foo', $this->logger);
$twigLoaderFilesystem = new FilesystemLoader(__DIR__ . '/../Resources/views/Collector');
$twigLoaderFilesystem->addPath(__DIR__ . '/../vendor/symfony/web-profiler-bundle/Resources/views', 'WebProfiler');
$this->twig = new Environment($twigLoaderFilesystem, ['debug' => true, 'strict_variables' => true]);
$this->twig->addExtension(new CodeExtension('', '', ''));
$this->twig->addExtension(new RoutingExtension($this->getMockBuilder(UrlGeneratorInterface::class)->getMock()));
$this->twig->addExtension(new HttpKernelExtension($this->getMockBuilder(FragmentHandler::class)->disableOriginalConstructor()->getMock()));
$this->twig->addExtension(new WebProfilerExtension());
$this->twig->addExtension(new DoctrineExtension());
$loader = $this->getMockBuilder(RuntimeLoaderInterface::class)->getMock();
$loader->method('load')->willReturn($this->getMockBuilder(HttpKernelRuntime::class)->disableOriginalConstructor()->getMock());
$this->twig->addRuntimeLoader($loader);
}
public function testRender()
{
$this->logger->queries = [
[
'sql' => 'SELECT * FROM foo WHERE bar IN (?, ?)',
'params' => ['foo', 'bar'],
'executionMS' => 1,
],
];
$this->collector->collect($request = new Request(['group' => '0']), $response = new Response());
$profile = new Profile('foo');
$output = $this->twig->render('db.html.twig', [
'request' => $request,
'token' => 'foo',
'page' => 'foo',
'profile' => $profile,
'collector' => $this->collector,
'queries' => $this->logger->queries,
]);
$output = str_replace(["\e[37m", "\e[0m", "\e[32;1m", "\e[34;1m"], '', $output);
$this->assertContains("SELECT * FROM foo WHERE bar IN ('foo', 'bar');", $output);
}
}