Completed
Push — master ( 312ed0...b8ea4b )
by Marco
10:51 queued 08:54
created

ProfilerTest   B

Complexity

Total Complexity 2

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
lcom 1
cbo 17
dl 0
loc 64
rs 7.8571
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testRender() 0 31 1
A setUp() 0 22 1
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);
0 ignored issues
show
Documentation introduced by
$registry is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\P...stence\ManagerRegistry>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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()));
0 ignored issues
show
Documentation introduced by
$this->getMockBuilder(\S...face::class)->getMock() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...\UrlGeneratorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$loader is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Twig_RuntimeLoaderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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