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()); |
|
|
|
|
56
|
|
|
self::assertSame($this->em, $emAware->em); |
57
|
|
|
|
58
|
|
|
$emAware->initializeProxy(); |
|
|
|
|
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
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: