/
DoctrineDataCollectorTest.php
153 lines (132 loc) · 5.78 KB
/
DoctrineDataCollectorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace Doctrine\Bundle\DoctrineBundle\Tests\DataCollector;
use Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DoctrineDataCollectorTest extends TestCase
{
const FIRST_ENTITY = 'TestBundle\Test\Entity\Test1';
const SECOND_ENTITY = 'TestBundle\Test\Entity\Test2';
public function testCollectEntities()
{
$manager = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock();
$config = $this->getMockBuilder('Doctrine\ORM\Configuration')->getMock();
$factory = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory')
->setMethods(['getLoadedMetadata'])->getMockForAbstractClass();
$collector = $this->createCollector(['default' => $manager]);
$manager->expects($this->any())
->method('getMetadataFactory')
->will($this->returnValue($factory));
$manager->expects($this->any())
->method('getConfiguration')
->will($this->returnValue($config));
$config->expects($this->once())
->method('isSecondLevelCacheEnabled')
->will($this->returnValue(false));
$metadatas = [
$this->createEntityMetadata(self::FIRST_ENTITY),
$this->createEntityMetadata(self::SECOND_ENTITY),
$this->createEntityMetadata(self::FIRST_ENTITY),
];
$factory->expects($this->once())
->method('getLoadedMetadata')
->will($this->returnValue($metadatas));
$collector->collect(new Request(), new Response());
$entities = $collector->getEntities();
$this->assertArrayHasKey('default', $entities);
$this->assertCount(2, $entities['default']);
}
public function testDoNotCollectEntities()
{
$manager = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock();
$config = $this->getMockBuilder('Doctrine\ORM\Configuration')->getMock();
$factory = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory')
->setMethods(['getLoadedMetadata'])->getMockForAbstractClass();
$collector = $this->createCollector(['default' => $manager], false);
$manager->expects($this->never())
->method('getMetadataFactory');
$manager->expects($this->any())
->method('getConfiguration')
->will($this->returnValue($config));
$config->expects($this->once())
->method('isSecondLevelCacheEnabled')
->will($this->returnValue(false));
$factory->expects($this->never())
->method('getLoadedMetadata');
$collector->collect(new Request(), new Response());
$entities = $collector->getEntities();
$this->assertEmpty($collector->getEntities());
}
public function testGetGroupedQueries()
{
$logger = $this->getMockBuilder('Doctrine\DBAL\Logging\DebugStack')->getMock();
$logger->queries = [];
$logger->queries[] = [
'sql' => 'SELECT * FROM foo WHERE bar = :bar',
'params' => [':bar' => 1],
'types' => null,
'executionMS' => 32,
];
$logger->queries[] = [
'sql' => 'SELECT * FROM foo WHERE bar = :bar',
'params' => [':bar' => 2],
'types' => null,
'executionMS' => 25,
];
$collector = $this->createCollector([]);
$collector->addLogger('default', $logger);
$collector->collect(new Request(), new Response());
$groupedQueries = $collector->getGroupedQueries();
$this->assertCount(1, $groupedQueries['default']);
$this->assertSame('SELECT * FROM foo WHERE bar = :bar', $groupedQueries['default'][0]['sql']);
$this->assertSame(2, $groupedQueries['default'][0]['count']);
$logger->queries[] = [
'sql' => 'SELECT * FROM bar',
'params' => [],
'types' => null,
'executionMS' => 25,
];
$collector->collect(new Request(), new Response());
$groupedQueries = $collector->getGroupedQueries();
$this->assertCount(2, $groupedQueries['default']);
$this->assertSame('SELECT * FROM bar', $groupedQueries['default'][1]['sql']);
$this->assertSame(1, $groupedQueries['default'][1]['count']);
}
/**
* @param string $entityFQCN
*
* @return ClassMetadataInfo
*/
private function createEntityMetadata($entityFQCN)
{
$metadata = new ClassMetadataInfo($entityFQCN);
$metadata->name = $entityFQCN;
$metadata->reflClass = new ReflectionClass('stdClass');
return $metadata;
}
/**
* @param array $managers
*
* @return DoctrineDataCollector
*/
private function createCollector(array $managers, bool $shouldValidateSchema = true)
{
$registry = $this->getMockBuilder('Doctrine\Persistence\ManagerRegistry')->getMock();
$registry
->expects($this->any())
->method('getConnectionNames')
->will($this->returnValue(['default' => 'doctrine.dbal.default_connection']));
$registry
->expects($this->any())
->method('getManagerNames')
->will($this->returnValue(['default' => 'doctrine.orm.default_entity_manager']));
$registry
->expects($this->any())
->method('getManagers')
->will($this->returnValue($managers));
return new DoctrineDataCollector($registry, $shouldValidateSchema);
}
}