Completed
Pull Request — master (#735)
by
unknown
01:59
created

ProfilerTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 8.8571
cc 1
eloc 19
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 Symfony\Bridge\Twig\Extension\CodeExtension;
11
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
12
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
13
use Symfony\Bridge\Twig\Extension\RoutingExtension;
14
use Symfony\Bundle\WebProfilerBundle\Twig\WebProfilerExtension;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
18
use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
19
use Symfony\Component\HttpKernel\Profiler\Profile;
20
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21
22
class ProfilerTest extends \PHPUnit\Framework\TestCase
23
{
24
    /** @var DebugStack */
25
    private $logger;
26
    /** @var \Twig_Environment */
27
    private $twig;
28
    /** @var DoctrineDataCollector */
29
    private $collector;
30
31
    public function setUp()
32
    {
33
        $storage = new FileProfilerStorage('file:'.sys_get_temp_dir().'/sf2_profiler_file_storage');
34
        $storage->purge();
35
36
        $this->logger = new DebugStack();
37
        $registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
38
        $registry->expects($this->once())->method('getManagers')->willReturn([]);
39
        $this->collector = new DoctrineDataCollector($registry);
40
        $this->collector->addLogger('test', $this->logger);
41
42
        $twigLoaderFilesystem = new \Twig_Loader_Filesystem(__DIR__.'/../Resources/views/Collector');
43
        $twigLoaderFilesystem->addPath(__DIR__.'/../vendor/symfony/web-profiler-bundle/Resources/views', 'WebProfiler');
44
        $this->twig = new \Twig_Environment($twigLoaderFilesystem, ['debug' => true, 'strict_variables' => true]);
45
46
        $this->twig->addExtension(new CodeExtension('', '', ''));
47
        $this->twig->addExtension(new RoutingExtension($this->getMockBuilder(UrlGeneratorInterface::class)->getMock()));
48
        $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...
49
        $this->twig->addExtension(new WebProfilerExtension());
50
        $this->twig->addExtension(new DoctrineExtension());
51
52
        $loader = $this->getMockBuilder(\Twig_RuntimeLoaderInterface::class)->getMock();
53
        $loader->method('load')->willReturn($this->getMockBuilder(HttpKernelRuntime::class)->disableOriginalConstructor()->getMock());
54
        $this->twig->addRuntimeLoader($loader);
55
    }
56
57
    public function testRender()
58
    {
59
        $this->logger->queries = [
60
            [
61
                'sql' => 'SELECT * FROM foo WHERE bar IN (?, ?)',
62
                'params' => ['foo', 'bar'],
63
                'executionMS' => 1,
64
            ],
65
        ];
66
67
        $this->collector->collect($request = new Request(['group' => false]), new Response());
68
69
        $output = $this->twig->render('db.html.twig', [
70
            'request' => $request,
71
            'token' => 'foo',
72
            'page' => 'foo',
73
            'profile' => new Profile('foo'),
74
            'collector' => $this->collector,
75
            'queries' => $this->logger->queries,
76
        ]);
77
78
        $output = str_replace(["\e[37m", "\e[0m", "\e[32;1m", "\e[34;1m"], "", $output);
79
        $this->assertContains("SELECT * FROM foo WHERE bar IN ('foo', 'bar');", $output);
80
    }
81
}