Completed
Pull Request — master (#714)
by Gocha
02:24
created

DoctrineDataCollectorTest::createEntityMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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 = [];
0 ignored issues
show
Bug introduced by
Accessing queries on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
57
        $logger->queries[] = ['sql' => 'SELECT * FROM foo WHERE bar = :bar', 'params' => [':bar' => 1]];
0 ignored issues
show
Bug introduced by
Accessing queries on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
58
        $logger->queries[] = ['sql' => 'SELECT * FROM foo WHERE bar = :bar', 'params' => [':bar' => 2]];
0 ignored issues
show
Bug introduced by
Accessing queries on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
59
        $collector = $this->createCollector([]);
60
        $collector->addLogger('default', $logger);
0 ignored issues
show
Documentation introduced by
$logger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Logging\DebugStack>.

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...
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' => []];
0 ignored issues
show
Bug introduced by
Accessing queries on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$registry is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\P...stence\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...
111
112
        return $collector;
113
    }
114
}
115