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