Failed Conditions
Push — develop ( ba9041...24f682 )
by Guilherme
64:30
created

DDC2359Test::testIssue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 9.7692
c 0
b 0
f 0
cc 1
eloc 40
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\Common\EventManager;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\ORM\Annotation as ORM;
10
use Doctrine\ORM\Configuration;
11
use Doctrine\ORM\EntityManagerInterface;
12
use Doctrine\ORM\Mapping\ClassMetadata;
13
use Doctrine\ORM\Mapping\ClassMetadataFactory;
14
use Doctrine\ORM\Mapping\Driver\MappingDriver;
15
use Doctrine\Tests\DoctrineTestCase;
16
17
/**
18
 * @group DDC-2359
19
 */
20
class DDC2359Test extends DoctrineTestCase
21
{
22
23
    /**
24
     * Verifies that {@see \Doctrine\ORM\Mapping\ClassMetadataFactory::wakeupReflection} is
25
     * not called when loading metadata from a driver
26
     */
27
    public function testIssue()
28
    {
29
        $mockDriver      = $this->createMock(MappingDriver::class);
30
        $mockMetadata    = $this->createMock(ClassMetadata::class);
31
        $entityManager   = $this->createMock(EntityManagerInterface::class);
32
33
        /* @var $metadataFactory \Doctrine\ORM\Mapping\ClassMetadataFactory|\PHPUnit_Framework_MockObject_MockObject */
34
        $metadataFactory = $this->getMockBuilder(ClassMetadataFactory::class)
35
                                ->setMethods(['doLoadMetadata', 'wakeupReflection'])
36
                                ->getMock();
37
38
        $configuration = $this->getMockBuilder(Configuration::class)
39
                              ->setMethods(['getMetadataDriverImpl'])
40
                              ->getMock();
41
42
        $connection = $this->createMock(Connection::class);
43
44
        $configuration
45
            ->expects($this->any())
46
            ->method('getMetadataDriverImpl')
47
            ->will($this->returnValue($mockDriver));
48
49
        $mockMetadata
50
            ->expects($this->any())
51
            ->method('getDeclaredPropertiesIterator')
52
            ->will($this->returnValue(new \ArrayIterator([])));
53
54
        $entityManager
55
            ->expects($this->any())
56
            ->method('getConfiguration')
57
            ->will($this->returnValue($configuration));
58
59
        $entityManager
60
            ->expects($this->any())
61
            ->method('getConnection')
62
            ->will($this->returnValue($connection));
63
64
        $entityManager
65
            ->expects($this->any())
66
            ->method('getEventManager')
67
            ->will($this->returnValue($this->createMock(EventManager::class)));
68
69
        $metadataFactory
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Mapping\ClassMetadataFactory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
            ->expects($this->any())
71
            ->method('doLoadMetadata')
72
            ->will($this->returnValue($mockMetadata));
73
74
        $metadataFactory
75
            ->expects($this->never())
76
            ->method('wakeupReflection');
77
78
        $metadataFactory->setEntityManager($entityManager);
0 ignored issues
show
Bug introduced by
The method setEntityManager does only exist in Doctrine\ORM\Mapping\ClassMetadataFactory, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
79
80
        self::assertSame($mockMetadata, $metadataFactory->getMetadataFor(DDC2359Foo::class));
0 ignored issues
show
Bug introduced by
The method getMetadataFor does only exist in Doctrine\ORM\Mapping\ClassMetadataFactory, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
81
    }
82
}
83
84
/** @ORM\Entity */
85
class DDC2359Foo
86
{
87
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
88
    public $id;
89
}
90