1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests\DataCollector; |
4
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector; |
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
class DoctrineDataCollectorTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
const FIRST_ENTITY = 'TestBundle\Test\Entity\Test1'; |
14
|
|
|
const SECOND_ENTITY = 'TestBundle\Test\Entity\Test2'; |
15
|
|
|
|
16
|
|
|
public function testCollectEntities() |
17
|
|
|
{ |
18
|
|
|
$manager = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
19
|
|
|
$config = $this->getMockBuilder('Doctrine\ORM\Configuration')->getMock(); |
20
|
|
|
$factory = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory') |
21
|
|
|
->setMethods(['getLoadedMetadata'])->getMockForAbstractClass(); |
22
|
|
|
$collector = $this->createCollector(['default' => $manager]); |
23
|
|
|
|
24
|
|
|
$manager->expects($this->any()) |
25
|
|
|
->method('getMetadataFactory') |
26
|
|
|
->will($this->returnValue($factory)); |
27
|
|
|
$manager->expects($this->any()) |
28
|
|
|
->method('getConfiguration') |
29
|
|
|
->will($this->returnValue($config)); |
30
|
|
|
|
31
|
|
|
if (method_exists($config, 'isSecondLevelCacheEnabled')) { |
32
|
|
|
$config->expects($this->once()) |
33
|
|
|
->method('isSecondLevelCacheEnabled') |
34
|
|
|
->will($this->returnValue(false)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$metadatas = [ |
38
|
|
|
$this->createEntityMetadata(self::FIRST_ENTITY), |
39
|
|
|
$this->createEntityMetadata(self::SECOND_ENTITY), |
40
|
|
|
$this->createEntityMetadata(self::FIRST_ENTITY), |
41
|
|
|
]; |
42
|
|
|
$factory->expects($this->once()) |
43
|
|
|
->method('getLoadedMetadata') |
44
|
|
|
->will($this->returnValue($metadatas)); |
45
|
|
|
|
46
|
|
|
$collector->collect(new Request(), new Response()); |
47
|
|
|
|
48
|
|
|
$entities = $collector->getEntities(); |
49
|
|
|
$this->assertArrayHasKey('default', $entities); |
50
|
|
|
$this->assertCount(2, $entities['default']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testGetGroupedQueries() |
54
|
|
|
{ |
55
|
|
|
$logger = $this->getMockBuilder('Doctrine\DBAL\Logging\DebugStack')->getMock(); |
56
|
|
|
$logger->queries = []; |
|
|
|
|
57
|
|
|
$logger->queries[] = ['sql' => 'SELECT * FROM foo WHERE bar = :bar', 'params' => [':bar' => 1]]; |
|
|
|
|
58
|
|
|
$logger->queries[] = ['sql' => 'SELECT * FROM foo WHERE bar = :bar', 'params' => [':bar' => 2]]; |
|
|
|
|
59
|
|
|
$collector = $this->createCollector([]); |
60
|
|
|
$collector->addLogger('default', $logger); |
|
|
|
|
61
|
|
|
$collector->collect(new Request(), new Response()); |
62
|
|
|
$groupedQueries = $collector->getGroupedQueries(); |
63
|
|
|
$this->assertCount(1, $groupedQueries['default']); |
64
|
|
|
$this->assertSame('SELECT * FROM foo WHERE bar = :bar', $groupedQueries['default'][0]['sql']); |
65
|
|
|
$this->assertSame(2, $groupedQueries['default'][0]['count']); |
66
|
|
|
|
67
|
|
|
$logger->queries[] = ['sql' => 'SELECT * FROM bar', 'params' => []]; |
|
|
|
|
68
|
|
|
$collector->collect(new Request(), new Response()); |
69
|
|
|
$groupedQueries = $collector->getGroupedQueries(); |
70
|
|
|
$this->assertCount(2, $groupedQueries['default']); |
71
|
|
|
$this->assertSame('SELECT * FROM bar', $groupedQueries['default'][1]['sql']); |
72
|
|
|
$this->assertSame(1, $groupedQueries['default'][1]['count']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $entityFQCN |
77
|
|
|
* |
78
|
|
|
* @return ClassMetadataInfo |
79
|
|
|
*/ |
80
|
|
|
private function createEntityMetadata($entityFQCN) |
81
|
|
|
{ |
82
|
|
|
$metadata = new ClassMetadataInfo($entityFQCN); |
83
|
|
|
$metadata->name = $entityFQCN; |
84
|
|
|
$metadata->reflClass = new \ReflectionClass('stdClass'); |
85
|
|
|
|
86
|
|
|
return $metadata; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $managers |
91
|
|
|
* |
92
|
|
|
* @return DoctrineDataCollector |
93
|
|
|
*/ |
94
|
|
|
private function createCollector(array $managers) |
95
|
|
|
{ |
96
|
|
|
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock(); |
97
|
|
|
$registry |
98
|
|
|
->expects($this->any()) |
99
|
|
|
->method('getConnectionNames') |
100
|
|
|
->will($this->returnValue(['default' => 'doctrine.dbal.default_connection'])); |
101
|
|
|
$registry |
102
|
|
|
->expects($this->any()) |
103
|
|
|
->method('getManagerNames') |
104
|
|
|
->will($this->returnValue(['default' => 'doctrine.orm.default_entity_manager'])); |
105
|
|
|
$registry |
106
|
|
|
->expects($this->any()) |
107
|
|
|
->method('getManagers') |
108
|
|
|
->will($this->returnValue($managers)); |
109
|
|
|
|
110
|
|
|
$collector = new DoctrineDataCollector($registry); |
|
|
|
|
111
|
|
|
|
112
|
|
|
return $collector; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: