ProfilerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests;
4
5
use Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector;
6
use Doctrine\Bundle\DoctrineBundle\Twig\DoctrineExtension;
7
use Doctrine\DBAL\Logging\DebugStack;
8
use Doctrine\Persistence\ManagerRegistry;
9
use PHPUnit\Framework\TestCase as BaseTestCase;
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\Profile;
19
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
20
use Twig\Environment;
21
use Twig\Loader\FilesystemLoader;
22
use Twig\RuntimeLoader\RuntimeLoaderInterface;
23
24
class ProfilerTest extends BaseTestCase
25
{
26
    /** @var DebugStack */
27
    private $logger;
28
29
    /** @var Environment */
30
    private $twig;
31
32
    /** @var DoctrineDataCollector */
33
    private $collector;
34
35
    public function setUp() : void
36
    {
37
        $this->logger = new DebugStack();
38
        $registry     = $this->getMockBuilder(ManagerRegistry::class)->getMock();
39
        $registry->expects($this->once())->method('getManagers')->willReturn([]);
40
        $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\Persistence\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...
41
        $this->collector->addLogger('foo', $this->logger);
42
43
        $twigLoaderFilesystem = new FilesystemLoader(__DIR__ . '/../Resources/views/Collector');
44
        $twigLoaderFilesystem->addPath(__DIR__ . '/../vendor/symfony/web-profiler-bundle/Resources/views', 'WebProfiler');
45
        $this->twig = new Environment($twigLoaderFilesystem, ['debug' => true, 'strict_variables' => true]);
46
47
        $fragmentHandler = $this->getMockBuilder(FragmentHandler::class)->disableOriginalConstructor()->getMock();
48
        $fragmentHandler->method('render')->willReturn('');
49
50
        $kernelRuntime = new HttpKernelRuntime($fragmentHandler);
0 ignored issues
show
Documentation introduced by
$fragmentHandler is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...agment\FragmentHandler>.

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...
51
52
        $urlGenerator = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
53
        $urlGenerator->method('generate')->willReturn('');
54
55
        $this->twig->addExtension(new CodeExtension('', '', ''));
56
        $this->twig->addExtension(new RoutingExtension($urlGenerator));
0 ignored issues
show
Documentation introduced by
$urlGenerator 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...
57
        $this->twig->addExtension(new HttpKernelExtension($fragmentHandler));
0 ignored issues
show
Unused Code introduced by
The call to HttpKernelExtension::__construct() has too many arguments starting with $fragmentHandler.

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...
58
        $this->twig->addExtension(new WebProfilerExtension());
59
        $this->twig->addExtension(new DoctrineExtension());
60
61
        $loader = $this->getMockBuilder(RuntimeLoaderInterface::class)->getMock();
62
        $loader->method('load')->willReturn($kernelRuntime);
63
        $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\RuntimeLoader\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...
64
    }
65
66
    public function testRender() : void
67
    {
68
        $this->logger->queries = [
69
            [
70
                'sql' => 'SELECT * FROM foo WHERE bar IN (?, ?) AND "" >= ""',
71
                'params' => ['foo', 'bar'],
72
                'types' => null,
73
                'executionMS' => 1,
74
            ],
75
        ];
76
77
        $this->collector->collect($request = new Request(['group' => '0']), $response = new Response());
78
79
        $profile = new Profile('foo');
80
81
        $output = $this->twig->render('db.html.twig', [
82
            'request' => $request,
83
            'token' => 'foo',
84
            'page' => 'foo',
85
            'profile' => $profile,
86
            'collector' => $this->collector,
87
            'queries' => $this->logger->queries,
88
        ]);
89
90
        $expectedEscapedSql = 'SELECT&#x0A;&#x20;&#x20;&#x2A;&#x0A;FROM&#x0A;&#x20;&#x20;foo&#x0A;WHERE&#x0A;&#x20;&#x20;bar&#x20;IN&#x20;&#x28;&#x3F;,&#x20;&#x3F;&#x29;&#x0A;&#x20;&#x20;AND&#x20;&quot;&quot;&#x20;&gt;&#x3D;&#x20;&quot;&quot;';
91
        $this->assertSame(
92
            "SELECT\n  *\nFROM\n  foo\nWHERE\n  bar IN (?, ?)\n  AND \"\" >= \"\"",
93
            html_entity_decode($expectedEscapedSql)
94
        );
95
96
        $this->assertContains($expectedEscapedSql, $output);
97
98
        $this->assertSame(1, preg_match('/' . str_replace(
99
            ' ',
100
            '.*',
101
            preg_quote('SELECT * FROM foo WHERE bar IN ( ? , ? )')
102
        ) . '/', $output));
103
    }
104
}
105