|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
|
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
|
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
|
7
|
|
|
* @license GPL-3.0 |
|
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Addiks\RDMBundle\Tests\Doctrine; |
|
12
|
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use Addiks\RDMBundle\Doctrine\EventListener; |
|
15
|
|
|
use Addiks\RDMBundle\Hydration\EntityHydratorInterface; |
|
16
|
|
|
use Addiks\RDMBundle\Tests\Hydration\EntityExample; |
|
17
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
|
18
|
|
|
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface; |
|
19
|
|
|
use Addiks\RDMBundle\DataLoader\DataLoaderInterface; |
|
20
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
21
|
|
|
use Doctrine\ORM\Proxy\Proxy; |
|
22
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs; |
|
23
|
|
|
use Doctrine\ORM\UnitOfWork; |
|
24
|
|
|
use Addiks\RDMBundle\Mapping\EntityMappingInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class EventListenerTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var EventListener |
|
31
|
|
|
*/ |
|
32
|
|
|
private $eventListener; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var EntityHydratorInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $entityServiceHydrator; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var MappingDriverInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $mappingDriver; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var DataLoaderInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
private $dbalDataLoader; |
|
48
|
|
|
|
|
49
|
|
|
public function setUp(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->entityServiceHydrator = $this->createMock(EntityHydratorInterface::class); |
|
|
|
|
|
|
52
|
|
|
$this->mappingDriver = $this->createMock(MappingDriverInterface::class); |
|
|
|
|
|
|
53
|
|
|
$this->dbalDataLoader = $this->createMock(DataLoaderInterface::class); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$this->eventListener = new EventListener( |
|
56
|
|
|
$this->entityServiceHydrator, |
|
57
|
|
|
$this->mappingDriver, |
|
58
|
|
|
$this->dbalDataLoader |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
*/ |
|
65
|
|
|
public function shouldHydrateEntityOnPostLoad() |
|
66
|
|
|
{ |
|
67
|
|
|
/** @var LifecycleEventArgs $arguments */ |
|
68
|
|
|
$arguments = $this->createMock(LifecycleEventArgs::class); |
|
69
|
|
|
|
|
70
|
|
|
$entity = new EntityExample(); |
|
71
|
|
|
|
|
72
|
|
|
$arguments->method('getEntity')->willReturn($entity); |
|
|
|
|
|
|
73
|
|
|
$arguments->method('getEntityManager')->willReturn($this->createMock(EntityManagerInterface::class)); |
|
74
|
|
|
|
|
75
|
|
|
$this->entityServiceHydrator->expects($this->once())->method('hydrateEntity')->with($entity); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$this->eventListener->postLoad($arguments); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @test |
|
82
|
|
|
*/ |
|
83
|
|
|
public function shouldAssertHydrationOnPrePersist() |
|
84
|
|
|
{ |
|
85
|
|
|
/** @var LifecycleEventArgs $arguments */ |
|
86
|
|
|
$arguments = $this->createMock(LifecycleEventArgs::class); |
|
87
|
|
|
|
|
88
|
|
|
$entity = new EntityExample(); |
|
89
|
|
|
|
|
90
|
|
|
$arguments->method('getEntity')->willReturn($entity); |
|
91
|
|
|
$arguments->method('getEntityManager')->willReturn($this->createMock(EntityManagerInterface::class)); |
|
92
|
|
|
|
|
93
|
|
|
$this->entityServiceHydrator->expects($this->once())->method('assertHydrationOnEntity')->with($entity); |
|
94
|
|
|
|
|
95
|
|
|
$this->eventListener->prePersist($arguments); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @test |
|
100
|
|
|
*/ |
|
101
|
|
|
public function shouldRemoveEntityProxiesOnlyWhenInitialized() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->mappingDriver->method('loadRDMMetadataForClass')->willReturn( |
|
|
|
|
|
|
104
|
|
|
$this->createMock(EntityMappingInterface::class) |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
/** @var Proxy $proxiedEntity */ |
|
108
|
|
|
$proxiedEntity = $this->createMock(Proxy::class); |
|
109
|
|
|
|
|
110
|
|
|
$proxiedEntity->method('__isInitialized')->willReturn(true); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** @var EntityManagerInterface $entityManager */ |
|
113
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
|
114
|
|
|
|
|
115
|
|
|
/** @var UnitOfWork $unitOfWork */ |
|
116
|
|
|
$unitOfWork = $this->createMock(UnitOfWork::class); |
|
117
|
|
|
|
|
118
|
|
|
$entityManager->method('getUnitOfWork')->willReturn($unitOfWork); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$unitOfWork->method('getIdentityMap')->willReturn([ |
|
|
|
|
|
|
121
|
|
|
EntityExample::class => [$proxiedEntity] |
|
122
|
|
|
]); |
|
123
|
|
|
|
|
124
|
|
|
$unitOfWork->method('isScheduledForDelete')->willReturn(false); |
|
125
|
|
|
|
|
126
|
|
|
$this->dbalDataLoader->expects($this->once())->method('storeDBALDataForEntity')->with( |
|
|
|
|
|
|
127
|
|
|
$this->equalTo($proxiedEntity), |
|
128
|
|
|
$this->equalTo($entityManager) |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
$this->eventListener->postFlush(new PostFlushEventArgs($entityManager)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @test |
|
136
|
|
|
*/ |
|
137
|
|
|
public function shouldIgnoreUninitializedProxies() |
|
138
|
|
|
{ |
|
139
|
|
|
/** @var Proxy $proxiedEntity */ |
|
140
|
|
|
$proxiedEntity = $this->createMock(Proxy::class); |
|
141
|
|
|
|
|
142
|
|
|
$proxiedEntity->method('__isInitialized')->willReturn(false); |
|
143
|
|
|
|
|
144
|
|
|
/** @var EntityManagerInterface $entityManager */ |
|
145
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
|
146
|
|
|
|
|
147
|
|
|
/** @var UnitOfWork $unitOfWork */ |
|
148
|
|
|
$unitOfWork = $this->createMock(UnitOfWork::class); |
|
149
|
|
|
|
|
150
|
|
|
$entityManager->method('getUnitOfWork')->willReturn($unitOfWork); |
|
151
|
|
|
|
|
152
|
|
|
$unitOfWork->method('getIdentityMap')->willReturn([ |
|
153
|
|
|
EntityExample::class => [$proxiedEntity] |
|
154
|
|
|
]); |
|
155
|
|
|
|
|
156
|
|
|
$unitOfWork->method('isScheduledForDelete')->willReturn(false); |
|
157
|
|
|
|
|
158
|
|
|
$this->dbalDataLoader->expects($this->never())->method('storeDBALDataForEntity'); |
|
159
|
|
|
|
|
160
|
|
|
$this->eventListener->postFlush(new PostFlushEventArgs($entityManager)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..