Test Failed
Pull Request — develop (#6867)
by Marco
62:57
created

DDC2231ObjectManagerAwareEntity   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 1
lcom 1
cbo 0
dl 0
loc 13
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A injectObjectManager() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata as CommonMetadata;
8
use Doctrine\Common\Persistence\ObjectManager;
9
use Doctrine\Common\Persistence\ObjectManagerAware;
10
use Doctrine\ORM\Annotation as ORM;
11
use Doctrine\ORM\EntityManagerAware;
12
use Doctrine\ORM\EntityManagerInterface;
13
use Doctrine\ORM\Mapping\ClassMetadata;
14
use Doctrine\ORM\Tools\ToolsException;
15
use ProxyManager\Proxy\GhostObjectInterface;
16
17
/**
18
 * @group DDC-2231
19
 */
20
class DDC2231Test extends \Doctrine\Tests\OrmFunctionalTestCase
21
{
22
    /**
23
     * @var DDC2231EntityManagerAwareEntity
24
     */
25
    private $persistedEntityManagerAwareEntity;
26
27
    /**
28
     * @var DDC2231ObjectManagerAwareEntity
29
     */
30
    private $persistedObjectManagerAwareEntity;
31
32
    protected function setUp()
33
    {
34
        parent::setUp();
35
36
        try {
37
            $this->schemaTool->createSchema([
38
                $this->em->getClassMetadata(DDC2231EntityManagerAwareEntity::class),
39
                $this->em->getClassMetadata(DDC2231ObjectManagerAwareEntity::class),
40
            ]);
41
        } catch (ToolsException $ignored) {
42
            // ignore - schema already exists
43
        }
44
45
        $this->persistedEntityManagerAwareEntity = new DDC2231EntityManagerAwareEntity();
46
        $this->persistedObjectManagerAwareEntity = new DDC2231ObjectManagerAwareEntity();
47
48
        $this->em->persist($this->persistedEntityManagerAwareEntity);
49
        $this->em->persist($this->persistedObjectManagerAwareEntity);
50
        $this->em->flush();
51
        $this->em->clear();
52
    }
53
54 View Code Duplication
    public function testInjectEntityManagerInProxyIfInitializedInUow()
55
    {
56
        /* @var $emAware DDC2231EntityManagerAwareEntity|GhostObjectInterface */
57
        $emAware = $this->em->getReference(
58
            DDC2231EntityManagerAwareEntity::class,
59
            $this->persistedEntityManagerAwareEntity->id
60
        );
61
62
        self::assertInstanceOf(GhostObjectInterface::class, $emAware);
63
        self::assertInstanceOf(DDC2231EntityManagerAwareEntity::class, $emAware);
64
        self::assertInstanceOf(EntityManagerAware::class, $emAware);
65
        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...
66
        self::assertSame($this->em, $emAware->em);
67
68
        $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...
69
70
        self::assertSame($this->em, $emAware->em);
71
    }
72
73 View Code Duplication
    public function testInjectEntityManagerInFetchedInstance()
74
    {
75
        /* @var $emAware DDC2231EntityManagerAwareEntity */
76
        $emAware = $this->em->find(
77
            DDC2231EntityManagerAwareEntity::class,
78
            $this->persistedEntityManagerAwareEntity->id
79
        );
80
81
        self::assertInstanceOf(DDC2231EntityManagerAwareEntity::class, $emAware);
82
        self::assertInstanceOf(EntityManagerAware::class, $emAware);
83
        self::assertNotInstanceOf(GhostObjectInterface::class, $emAware);
84
        self::assertSame($this->em, $emAware->em);
85
    }
86
87 View Code Duplication
    public function testInjectObjectManagerInProxyIfInitializedInUow()
88
    {
89
        /* @var $omAware DDC2231ObjectManagerAwareEntity|GhostObjectInterface */
90
        $omAware = $this->em->getReference(
91
            DDC2231ObjectManagerAwareEntity::class,
92
            $this->persistedObjectManagerAwareEntity->id
93
        );
94
95
        self::assertInstanceOf(GhostObjectInterface::class, $omAware);
96
        self::assertInstanceOf(DDC2231ObjectManagerAwareEntity::class, $omAware);
97
        self::assertInstanceOf(ObjectManagerAware::class, $omAware);
98
        self::assertFalse($omAware->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...bjectManagerAwareEntity.

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...
99
        self::assertSame($this->em, $omAware->om);
100
101
        $omAware->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...bjectManagerAwareEntity.

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...
102
103
        self::assertSame($this->em, $omAware->om);
104
    }
105
106 View Code Duplication
    public function testInjectObjectManagerInFetchedInstance()
107
    {
108
        /* @var $omAware DDC2231ObjectManagerAwareEntity */
109
        $omAware = $this->em->find(
110
            DDC2231ObjectManagerAwareEntity::class,
111
            $this->persistedObjectManagerAwareEntity->id
112
        );
113
114
        self::assertInstanceOf(DDC2231ObjectManagerAwareEntity::class, $omAware);
115
        self::assertInstanceOf(ObjectManagerAware::class, $omAware);
116
        self::assertNotInstanceOf(GhostObjectInterface::class, $omAware);
117
        self::assertSame($this->em, $omAware->om);
118
    }
119
}
120
121
122
/** @ORM\Entity */
123
class DDC2231EntityManagerAwareEntity implements EntityManagerAware
124
{
125
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
126
    public $id;
127
128
    /** @var EntityManagerInterface|null */
129
    public $em;
130
131
    public function injectEntityManager(EntityManagerInterface $entityManager, ClassMetadata $classMetadata) : void
132
    {
133
        $this->em = $entityManager;
134
    }
135
}
136
137
138
/** @ORM\Entity */
139
class DDC2231ObjectManagerAwareEntity implements ObjectManagerAware
140
{
141
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
142
    public $id;
143
144
    /** @var ObjectManager|null */
145
    public $om;
146
147
    public function injectObjectManager(ObjectManager $objectManager, CommonMetadata $classMetadata) : void
148
    {
149
        $this->om = $objectManager;
150
    }
151
}
152