File: ReferenceRepositoryTest.php

package info (click to toggle)
php-doctrine-data-fixtures 2.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 564 kB
  • sloc: php: 3,148; makefile: 20
file content (266 lines) | stat: -rw-r--r-- 10,278 bytes parent folder | download | duplicates (2)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\DataFixtures;

use BadMethodCallException;
use Doctrine\Common\DataFixtures\Event\Listener\ORMReferenceListener;
use Doctrine\Common\DataFixtures\ReferenceRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Persistence\Proxy;
use Doctrine\Tests\Common\DataFixtures\TestEntity\Role;
use Doctrine\Tests\Common\DataFixtures\TestEntity\User;
use Doctrine\Tests\Mock\ForwardCompatibleEntityManager;
use OutOfBoundsException;

use function sprintf;

class ReferenceRepositoryTest extends BaseTestCase
{
    public function testReferenceEntry(): void
    {
        $em = $this->getMockSqliteEntityManager();

        $role = new TestEntity\Role();
        $role->setName('admin');

        $meta = $em->getClassMetadata(Role::class);
        $meta->getReflectionProperty('id')->setValue($role, 1);

        $referenceRepo = new ReferenceRepository($em);
        $this->assertSame($em, $referenceRepo->getManager());

        $referenceRepo->addReference('test', $role);

        $referencesByClass = $referenceRepo->getReferencesByClass();
        $this->assertCount(1, $referencesByClass);
        $this->assertArrayHasKey(Role::class, $referencesByClass);
        $this->assertCount(1, $referencesByClass[Role::class]);
        $this->assertArrayHasKey('test', $referencesByClass[Role::class]);
        $this->assertInstanceOf(Role::class, $referencesByClass[Role::class]['test']);
    }

    public function testReferenceIdentityPopulation(): void
    {
        $em                  = $this->getMockSqliteEntityManager();
        $referenceRepository = $this->getMockBuilder(ReferenceRepository::class)
            ->setConstructorArgs([$em])
            ->getMock();
        $em->getEventManager()->addEventSubscriber(
            new ORMReferenceListener($referenceRepository),
        );
        $schemaTool = new SchemaTool($em);
        $schemaTool->dropSchema([]);
        $schemaTool->createSchema([$em->getClassMetadata(Role::class)]);

        $referenceRepository->expects($this->once())
            ->method('addReference')
            ->with('admin-role');

        $referenceRepository->expects($this->once())
            ->method('getReferenceNames')
            ->willReturn(['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 ReferenceRepository($em);
        $em->getEventManager()->addEventSubscriber(
            new ORMReferenceListener($referenceRepository),
        );
        $schemaTool = new SchemaTool($em);
        $schemaTool->dropSchema([]);
        $schemaTool->createSchema([$em->getClassMetadata(Role::class)]);
        $roleFixture = new TestFixtures\RoleFixture();
        $roleFixture->setReferenceRepository($referenceRepository);

        $roleFixture->load($em);
        // first test against managed state
        $ref = $referenceRepository->getReference('admin-role', Role::class);

        $this->assertNotInstanceOf(Proxy::class, $ref);

        // now test reference reconstruction from identity
        $em->clear();
        $ref = $referenceRepository->getReference('admin-role', Role::class);

        $this->assertInstanceOf(Proxy::class, $ref);
    }

    public function testReferenceMultipleEntries(): void
    {
        $em                  = $this->getMockSqliteEntityManager();
        $referenceRepository = new ReferenceRepository($em);
        $em->getEventManager()->addEventSubscriber(new ORMReferenceListener($referenceRepository));
        $schemaTool = new SchemaTool($em);
        $schemaTool->createSchema([$em->getClassMetadata(Role::class)]);

        $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', Role::class));
        $this->assertInstanceOf(Proxy::class, $referenceRepository->getReference('duplicate', Role::class));
    }

    public function testUndefinedReference(): void
    {
        $referenceRepository = new ReferenceRepository($this->getMockSqliteEntityManager());

        $this->expectException(OutOfBoundsException::class);
        $this->expectExceptionMessage(sprintf('Reference to "foo" for class "%s" does not exist', Role::class));

        $referenceRepository->getReference('foo', Role::class);
    }

    /** @group legacy */
    public function testLegacyUndefinedReference(): void
    {
        $referenceRepository = new ReferenceRepository($this->getMockSqliteEntityManager());

        $this->expectException(OutOfBoundsException::class);
        $this->expectExceptionMessage(sprintf('Reference to "foo" for class "%s" does not exist', Role::class));

        $referenceRepository->getReference('foo', Role::class);
    }

    public function testThrowsExceptionAddingDuplicatedReference(): void
    {
        $em                  = $this->getMockSqliteEntityManager();
        $referenceRepository = new ReferenceRepository($em);

        $referenceRepository->addReference('duplicated_reference', new Role());

        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessage(sprintf(
            'Reference to "duplicated_reference" for class "%s" already exists, use method setReference() in order to override it',
            Role::class,
        ));

        $referenceRepository->addReference('duplicated_reference', new Role());
    }

    public function testThereIsNoDuplicateWithDifferentClasses(): void
    {
        $em                  = $this->getMockSqliteEntityManager();
        $referenceRepository = new ReferenceRepository($em);

        $referenceRepository->addReference('not_duplicated_reference', new Role());
        $referenceRepository->addReference('not_duplicated_reference', new User());

        static::assertCount(2, $referenceRepository->getReferencesByClass());
    }

    public function testThrowsExceptionTryingToGetWrongReference(): void
    {
        $referenceRepository = new ReferenceRepository($this->getMockSqliteEntityManager());

        $this->expectException(OutOfBoundsException::class);
        $this->expectExceptionMessage(sprintf('Reference to "missing_reference" for class "%s" does not exist', Role::class));

        $referenceRepository->getReference('missing_reference', Role::class);
    }

    public function testHasIdentityCheck(): void
    {
        $referenceRepository = new ReferenceRepository($this->getMockSqliteEntityManager());
        $referenceRepository->setReferenceIdentity('entity', 1, Role::class);

        $this->assertTrue($referenceRepository->hasIdentity('entity', Role::class));
        $this->assertFalse($referenceRepository->hasIdentity('invalid_entity', Role::class));
        $this->assertEquals(['entity' => 1], $referenceRepository->getIdentitiesByClass()[Role::class] ?? []);
    }

    public function testSetReferenceHavingIdentifier(): void
    {
        $em                  = $this->getMockSqliteEntityManager();
        $referenceRepository = new ReferenceRepository($em);

        $schemaTool = new SchemaTool($em);
        $schemaTool->dropSchema([]);
        $schemaTool->createSchema([$em->getClassMetadata(Role::class)]);

        $role = new Role();
        $role->setName('role_name');
        $em->persist($role);
        $em->flush();

        $referenceRepository->setReference('entity', $role);
        $identities = $referenceRepository->getIdentitiesByClass()[Role::class] ?? [];
        $this->assertCount(1, $identities);
        $this->assertArrayHasKey('entity', $identities);
    }

    public function testGetIdentifierWhenHasNotBeenManagedYetByUnitOfWork(): void
    {
        $role               = new Role();
        $identitiesExpected = ['id' => 1];

        $uow = $this->createMock(UnitOfWork::class);
        $uow->expects($this->exactly(2))
            ->method('isInIdentityMap')
            ->with($role)
            ->willReturn(true, false);

        $classMetadata = $this->createMock(ClassMetadata::class);
        $classMetadata->expects($this->once())
            ->method('getIdentifierValues')
            ->with($role)
            ->willReturn($identitiesExpected);
        $classMetadata->expects($this->once())
            ->method('getName')
            ->willReturn(Role::class);

        $em = $this->createMock(ForwardCompatibleEntityManager::class);
        $em->method('getUnitOfWork')
           ->willReturn($uow);
        $em->method('getClassMetadata')
            ->with(Role::class)
            ->willReturn($classMetadata);

        $referenceRepository = new ReferenceRepository($em);
        $referenceRepository->setReference('entity', $role);
        $identities = $referenceRepository->getIdentitiesByClass()[Role::class] ?? [];

        $this->assertEquals($identitiesExpected, $identities['entity']);
    }

    public function testGivenAReferenceWithNameWithIntegerValueWhenGetReferenceNamesOfEntitiesThenReturnNamesInStringType(): void
    {
        $em                  = $this->getMockSqliteEntityManager();
        $referenceRepository = new ReferenceRepository($em);

        $schemaTool = new SchemaTool($em);
        $schemaTool->dropSchema([]);
        $schemaTool->createSchema([$em->getClassMetadata(Role::class)]);

        $role = new Role();
        $role->setName('role_name');
        $em->persist($role);
        $em->flush();

        $name = (string) $role->getId();
        $referenceRepository->setReference($name, $role);
        $names = $referenceRepository->getReferenceNames($role);
        $this->assertCount(1, $names);
        $this->assertSame('1', $names[0]);
    }
}