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); |
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); |
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)); |
57
|
|
|
$this->twig->addExtension(new HttpKernelExtension($fragmentHandler)); |
|
|
|
|
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); |
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
  *
FROM
  foo
WHERE
  bar IN (?, ?)
  AND "" >= ""'; |
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
|
|
|
|
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.