-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
ProxyReferenceRepositoryTest.php
181 lines (141 loc) · 6.66 KB
/
ProxyReferenceRepositoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
declare(strict_types=1);
namespace Doctrine\Tests\Common\DataFixtures;
use Doctrine\Common\DataFixtures\Event\Listener\ORMReferenceListener;
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Persistence\Proxy;
use Doctrine\Tests\Common\DataFixtures\TestEntity\Link;
use Doctrine\Tests\Common\DataFixtures\TestEntity\Role;
use Doctrine\Tests\Common\DataFixtures\TestTypes\UuidType;
use Doctrine\Tests\Common\DataFixtures\TestValueObjects\Uuid;
/**
* Test ProxyReferenceRepository.
*/
class ProxyReferenceRepositoryTest extends BaseTestCase
{
public const TEST_ENTITY_ROLE = Role::class;
public const TEST_ENTITY_LINK = Link::class;
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
if (Type::hasType('uuid')) {
return;
}
Type::addType('uuid', UuidType::class);
}
public function testReferenceEntry(): void
{
$em = $this->getMockSqliteEntityManager();
$role = new TestEntity\Role();
$role->setName('admin');
$meta = $em->getClassMetadata(self::TEST_ENTITY_ROLE);
$meta->getReflectionProperty('id')->setValue($role, 1);
$referenceRepo = new ProxyReferenceRepository($em);
$referenceRepo->addReference('test', $role);
$references = $referenceRepo->getReferences();
$this->assertCount(1, $references);
$this->assertArrayHasKey('test', $references);
$this->assertInstanceOf(self::TEST_ENTITY_ROLE, $references['test']);
}
public function testReferenceIdentityPopulation(): void
{
$em = $this->getMockSqliteEntityManager();
$referenceRepository = $this->getMockBuilder(ProxyReferenceRepository::class)
->setConstructorArgs([$em])
->getMock();
$em->getEventManager()->addEventSubscriber(
new ORMReferenceListener($referenceRepository),
);
$schemaTool = new SchemaTool($em);
$schemaTool->dropSchema([]);
$schemaTool->createSchema([$em->getClassMetadata(self::TEST_ENTITY_ROLE)]);
$referenceRepository->expects($this->once())
->method('addReference')
->with('admin-role');
$referenceRepository->expects($this->once())
->method('getReferenceNames')
->will($this->returnValue(['admin-role']));
$referenceRepository->expects($this->once())
->method('setReferenceIdentity')
->with('admin-role', ['id' => 1]);
$roleFixture = new TestFixtures\RoleFixture();
$roleFixture->setReferenceRepository($referenceRepository);
$roleFixture->load($em);
}
public function testReferenceReconstruction(): void
{
$em = $this->getMockSqliteEntityManager();
$referenceRepository = new ProxyReferenceRepository($em);
$listener = new ORMReferenceListener($referenceRepository);
$em->getEventManager()->addEventSubscriber($listener);
$schemaTool = new SchemaTool($em);
$schemaTool->dropSchema([]);
$schemaTool->createSchema([$em->getClassMetadata(self::TEST_ENTITY_ROLE)]);
$roleFixture = new TestFixtures\RoleFixture();
$roleFixture->setReferenceRepository($referenceRepository);
$roleFixture->load($em);
// first test against managed state
$ref = $referenceRepository->getReference('admin-role');
$this->assertNotInstanceOf(Proxy::class, $ref);
// test reference reconstruction from serialized data (was managed)
$serializedData = $referenceRepository->serialize();
$proxyReferenceRepository = new ProxyReferenceRepository($em);
$proxyReferenceRepository->unserialize($serializedData);
$ref = $proxyReferenceRepository->getReference('admin-role');
// before clearing, the reference is not yet a proxy
$this->assertNotInstanceOf(Proxy::class, $ref);
$this->assertInstanceOf(self::TEST_ENTITY_ROLE, $ref);
// now test reference reconstruction from identity
$em->clear();
$ref = $referenceRepository->getReference('admin-role');
$this->assertInstanceOf(Proxy::class, $ref);
// test reference reconstruction from serialized data (was identity)
$serializedData = $referenceRepository->serialize();
$proxyReferenceRepository = new ProxyReferenceRepository($em);
$proxyReferenceRepository->unserialize($serializedData);
$ref = $proxyReferenceRepository->getReference('admin-role');
$this->assertInstanceOf(Proxy::class, $ref);
}
public function testReconstructionOfCustomTypedId(): void
{
$em = $this->getMockSqliteEntityManager();
$referenceRepository = new ProxyReferenceRepository($em);
$listener = new ORMReferenceListener($referenceRepository);
$em->getEventManager()->addEventSubscriber($listener);
$schemaTool = new SchemaTool($em);
$schemaTool->dropSchema([]);
$schemaTool->createSchema([$em->getClassMetadata(self::TEST_ENTITY_LINK)]);
$link = new TestEntity\Link(new Uuid('5e48c0d7-78c2-44f5-bed0-e7970b2822b8'));
$link->setUrl('http://example.com');
$referenceRepository->addReference('home-link', $link);
$em->persist($link);
$em->flush();
$em->clear();
$serializedData = $referenceRepository->serialize();
$proxyReferenceRepository = new ProxyReferenceRepository($em);
$proxyReferenceRepository->unserialize($serializedData);
$this->assertInstanceOf(
'Doctrine\Tests\Common\DataFixtures\TestValueObjects\Uuid',
$proxyReferenceRepository->getReference('home-link')->getId(),
);
}
public function testReferenceMultipleEntries(): void
{
$em = $this->getMockSqliteEntityManager();
$referenceRepository = new ProxyReferenceRepository($em);
$em->getEventManager()->addEventSubscriber(new ORMReferenceListener($referenceRepository));
$schemaTool = new SchemaTool($em);
$schemaTool->createSchema([$em->getClassMetadata(self::TEST_ENTITY_ROLE)]);
$role = new TestEntity\Role();
$role->setName('admin');
$em->persist($role);
$referenceRepository->addReference('admin', $role);
$referenceRepository->addReference('duplicate', $role);
$em->flush();
$em->clear();
$this->assertInstanceOf(Proxy::class, $referenceRepository->getReference('admin'));
$this->assertInstanceOf(Proxy::class, $referenceRepository->getReference('duplicate'));
}
}