Failed Conditions
Pull Request — develop (#6867)
by Marco
63:20
created

DDC2231Test   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 59
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 18 2
A testInjectEntityManagerInProxyIfInitializedInUow() 0 18 1
A testInjectEntityManagerInFetchedInstance() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\ORM\Annotation as ORM;
8
use Doctrine\ORM\EntityManagerAware;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Doctrine\ORM\Mapping\ClassMetadata;
11
use Doctrine\ORM\Tools\ToolsException;
12
use Doctrine\Tests\OrmFunctionalTestCase;
13
use ProxyManager\Proxy\GhostObjectInterface;
14
15
/**
16
 * @group DDC-2231
17
 */
18
final class DDC2231Test extends OrmFunctionalTestCase
19
{
20
    /**
21
     * @var DDC2231EntityManagerAwareEntity
22
     */
23
    private $persistedEntityManagerAwareEntity;
24
25
    protected function setUp() : void
26
    {
27
        parent::setUp();
28
29
        try {
30
            $this->schemaTool->createSchema([
31
                $this->em->getClassMetadata(DDC2231EntityManagerAwareEntity::class),
32
            ]);
33
        } catch (ToolsException $ignored) {
34
            // ignore - schema already exists
35
        }
36
37
        $this->persistedEntityManagerAwareEntity = new DDC2231EntityManagerAwareEntity();
38
39
        $this->em->persist($this->persistedEntityManagerAwareEntity);
40
        $this->em->flush();
41
        $this->em->clear();
42
    }
43
44
    public function testInjectEntityManagerInProxyIfInitializedInUow() : void
45
    {
46
        /* @var $emAware DDC2231EntityManagerAwareEntity|GhostObjectInterface */
47
        $emAware = $this->em->getReference(
48
            DDC2231EntityManagerAwareEntity::class,
49
            $this->persistedEntityManagerAwareEntity->id
50
        );
51
52
        self::assertInstanceOf(GhostObjectInterface::class, $emAware);
53
        self::assertInstanceOf(DDC2231EntityManagerAwareEntity::class, $emAware);
54
        self::assertInstanceOf(EntityManagerAware::class, $emAware);
55
        self::assertFalse($emAware->isProxyInitialized());
0 ignored issues
show
Bug introduced by
The method isProxyInitialized does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\ORM\Funct...ntityManagerAwareEntity.

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...
56
        self::assertSame($this->em, $emAware->em);
57
58
        $emAware->initializeProxy();
0 ignored issues
show
Bug introduced by
The method initializeProxy does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\ORM\Funct...ntityManagerAwareEntity.

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...
59
60
        self::assertSame($this->em, $emAware->em);
61
    }
62
63
    public function testInjectEntityManagerInFetchedInstance() : void
64
    {
65
        /* @var $emAware DDC2231EntityManagerAwareEntity */
66
        $emAware = $this->em->find(
67
            DDC2231EntityManagerAwareEntity::class,
68
            $this->persistedEntityManagerAwareEntity->id
69
        );
70
71
        self::assertInstanceOf(DDC2231EntityManagerAwareEntity::class, $emAware);
72
        self::assertInstanceOf(EntityManagerAware::class, $emAware);
73
        self::assertNotInstanceOf(GhostObjectInterface::class, $emAware);
74
        self::assertSame($this->em, $emAware->em);
75
    }
76
}
77
78
/** @ORM\Entity */
79
class DDC2231EntityManagerAwareEntity implements EntityManagerAware
80
{
81
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
82
    public $id;
83
84
    /** @var EntityManagerInterface|null */
85
    public $em;
86
87
    public function injectEntityManager(EntityManagerInterface $entityManager, ClassMetadata $classMetadata) : void
88
    {
89
        $this->em = $entityManager;
90
    }
91
}
92