Completed
Push — master ( 81250e...eb6e4f )
by Mike
02:26
created

ProfilerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests;
4
5
6
use Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector;
7
use Doctrine\Bundle\DoctrineBundle\Twig\DoctrineExtension;
8
use Doctrine\Common\Persistence\ManagerRegistry;
9
use Doctrine\DBAL\Logging\DebugStack;
10
use PHPUnit\Framework\TestCase as BaseTestCase;
11
use Symfony\Bridge\Twig\Extension\CodeExtension;
12
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
13
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
14
use Symfony\Bridge\Twig\Extension\RoutingExtension;
15
use Symfony\Bundle\WebProfilerBundle\Twig\WebProfilerExtension;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
19
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
20
use Symfony\Component\HttpKernel\Profiler\Profile;
21
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22
23
class ProfilerTest extends BaseTestCase
24
{
25
    /** @var DebugStack */
26
    private $logger;
27
    /** @var \Twig_Environment */
28
    private $twig;
29
    /** @var DoctrineDataCollector */
30
    private $collector;
31
32
    public function setUp()
33
    {
34
        $this->logger = new DebugStack();
35
        $registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
36
        $registry->expects($this->once())->method('getManagers')->willReturn([]);
37
        $this->collector = new DoctrineDataCollector($registry);
38
        $this->collector->addLogger('foo', $this->logger);
39
40
        $twigLoaderFilesystem = new \Twig_Loader_Filesystem(__DIR__.'/../Resources/views/Collector');
41
        $twigLoaderFilesystem->addPath(__DIR__.'/../vendor/symfony/web-profiler-bundle/Resources/views', 'WebProfiler');
42
        $this->twig = new \Twig_Environment($twigLoaderFilesystem, ['debug' => true, 'strict_variables' => true]);
43
44
        $this->twig->addExtension(new CodeExtension('', '', ''));
45
        $this->twig->addExtension(new RoutingExtension($this->getMockBuilder(UrlGeneratorInterface::class)->getMock()));
46
        $this->twig->addExtension(new HttpKernelExtension($this->getMockBuilder(FragmentHandler::class)->disableOriginalConstructor()->getMock()));
0 ignored issues
show
Unused Code introduced by
The call to HttpKernelExtension::__construct() has too many arguments starting with $this->getMockBuilder(\S...onstructor()->getMock().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
        $this->twig->addExtension(new WebProfilerExtension());
48
        $this->twig->addExtension(new DoctrineExtension());
49
50
        $loader = $this->getMockBuilder(\Twig_RuntimeLoaderInterface::class)->getMock();
51
        $loader->method('load')->willReturn($this->getMockBuilder(HttpKernelRuntime::class)->disableOriginalConstructor()->getMock());
52
        $this->twig->addRuntimeLoader($loader);
53
    }
54
55
    public function testRender()
56
    {
57
        $this->logger->queries = [
58
            [
59
                'sql' => 'SELECT * FROM foo WHERE bar IN (?, ?)',
60
                'params' => ['foo', 'bar'],
61
                'executionMS' => 1,
62
            ],
63
        ];
64
65
        $this->collector->collect($request = new Request(['group' => '0']), $response = new Response());
66
67
        $profile = new Profile('foo');
68
69
        // This is only needed for WebProfilerBundle=3.2, remove when support for it is dropped
70
        $requestCollector = new RequestDataCollector();
71
        $requestCollector->collect($request, $response);
72
        $profile->addCollector($requestCollector);
73
74
        $output = $this->twig->render('db.html.twig', [
75
            'request' => $request,
76
            'token' => 'foo',
77
            'page' => 'foo',
78
            'profile' => $profile,
79
            'collector' => $this->collector,
80
            'queries' => $this->logger->queries,
81
        ]);
82
83
        $output = str_replace(["\e[37m", "\e[0m", "\e[32;1m", "\e[34;1m"], "", $output);
84
        $this->assertContains("SELECT * FROM foo WHERE bar IN ('foo', 'bar');", $output);
85
    }
86
}
87