1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\Tests\ORM\Cache; |
||
6 | |||
7 | use Doctrine\ORM\Cache\AssociationCacheEntry; |
||
8 | use Doctrine\ORM\Cache\CacheEntry; |
||
9 | use Doctrine\ORM\Cache\DefaultEntityHydrator; |
||
10 | use Doctrine\ORM\Cache\EntityCacheEntry; |
||
11 | use Doctrine\ORM\Cache\EntityCacheKey; |
||
12 | use Doctrine\ORM\Cache\EntityHydrator; |
||
13 | use Doctrine\ORM\EntityManagerInterface; |
||
14 | use Doctrine\ORM\UnitOfWork; |
||
15 | use Doctrine\Tests\Models\Cache\Country; |
||
16 | use Doctrine\Tests\Models\Cache\State; |
||
17 | use Doctrine\Tests\OrmTestCase; |
||
18 | |||
19 | /** |
||
20 | * @group DDC-2183 |
||
21 | */ |
||
22 | class DefaultEntityHydratorTest extends OrmTestCase |
||
23 | { |
||
24 | /** @var EntityHydrator */ |
||
25 | private $structure; |
||
26 | |||
27 | /** @var EntityManagerInterface */ |
||
28 | private $em; |
||
29 | |||
30 | protected function setUp() : void |
||
31 | { |
||
32 | parent::setUp(); |
||
33 | |||
34 | $this->em = $this->getTestEntityManager(); |
||
35 | $this->structure = new DefaultEntityHydrator($this->em); |
||
36 | } |
||
37 | |||
38 | public function testImplementsEntityEntryStructure() : void |
||
39 | { |
||
40 | self::assertInstanceOf('Doctrine\ORM\Cache\EntityHydrator', $this->structure); |
||
41 | } |
||
42 | |||
43 | public function testCreateEntity() : void |
||
44 | { |
||
45 | $metadata = $this->em->getClassMetadata(Country::class); |
||
46 | $key = new EntityCacheKey($metadata->getClassName(), ['id' => 1]); |
||
47 | $entry = new EntityCacheEntry($metadata->getClassName(), ['id' => 1, 'name' => 'Foo']); |
||
48 | $entity = $this->structure->loadCacheEntry($metadata, $key, $entry); |
||
49 | |||
50 | self::assertInstanceOf($metadata->getClassName(), $entity); |
||
51 | |||
52 | self::assertEquals(1, $entity->getId()); |
||
53 | self::assertEquals('Foo', $entity->getName()); |
||
54 | self::assertEquals(UnitOfWork::STATE_MANAGED, $this->em->getUnitOfWork()->getEntityState($entity)); |
||
55 | } |
||
56 | |||
57 | public function testLoadProxy() : void |
||
58 | { |
||
59 | $metadata = $this->em->getClassMetadata(Country::class); |
||
60 | $key = new EntityCacheKey($metadata->getClassName(), ['id' => 1]); |
||
61 | $entry = new EntityCacheEntry($metadata->getClassName(), ['id' => 1, 'name' => 'Foo']); |
||
62 | $proxy = $this->em->getReference($metadata->getClassName(), $key->identifier); |
||
63 | $entity = $this->structure->loadCacheEntry($metadata, $key, $entry, $proxy); |
||
64 | |||
65 | self::assertInstanceOf($metadata->getClassName(), $entity); |
||
66 | self::assertSame($proxy, $entity); |
||
67 | |||
68 | self::assertEquals(1, $entity->getId()); |
||
69 | self::assertEquals('Foo', $entity->getName()); |
||
70 | self::assertEquals(UnitOfWork::STATE_MANAGED, $this->em->getUnitOfWork()->getEntityState($proxy)); |
||
71 | } |
||
72 | |||
73 | public function testBuildCacheEntry() : void |
||
74 | { |
||
75 | $entity = new Country('Foo'); |
||
76 | $uow = $this->em->getUnitOfWork(); |
||
77 | $data = ['id' => 1, 'name' => 'Foo']; |
||
78 | $metadata = $this->em->getClassMetadata(Country::class); |
||
79 | $key = new EntityCacheKey($metadata->getClassName(), ['id' => 1]); |
||
80 | |||
81 | $entity->setId(1); |
||
82 | $uow->registerManaged($entity, $key->identifier, $data); |
||
83 | |||
84 | $cache = $this->structure->buildCacheEntry($metadata, $key, $entity); |
||
85 | |||
86 | self::assertInstanceOf(CacheEntry::class, $cache); |
||
87 | self::assertInstanceOf(EntityCacheEntry::class, $cache); |
||
88 | |||
89 | self::assertArrayHasKey('id', $cache->data); |
||
90 | self::assertArrayHasKey('name', $cache->data); |
||
91 | self::assertEquals( |
||
92 | [ |
||
93 | 'id' => 1, |
||
94 | 'name' => 'Foo', |
||
95 | ], |
||
96 | $cache->data |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | public function testBuildCacheEntryAssociation() : void |
||
101 | { |
||
102 | $country = new Country('Foo'); |
||
103 | $state = new State('Bat', $country); |
||
104 | $uow = $this->em->getUnitOfWork(); |
||
105 | $countryData = ['id' => 11, 'name' => 'Foo']; |
||
106 | $stateData = ['id' => 12, 'name' => 'Bar', 'country' => $country]; |
||
107 | $metadata = $this->em->getClassMetadata(State::class); |
||
108 | $key = new EntityCacheKey($metadata->getClassName(), ['id' => 11]); |
||
109 | |||
110 | $country->setId(11); |
||
111 | $state->setId(12); |
||
112 | |||
113 | $uow->registerManaged($country, ['id' => 11], $countryData); |
||
114 | $uow->registerManaged($state, ['id' => 12], $stateData); |
||
115 | |||
116 | $cache = $this->structure->buildCacheEntry($metadata, $key, $state); |
||
117 | |||
118 | self::assertInstanceOf(CacheEntry::class, $cache); |
||
119 | self::assertInstanceOf(EntityCacheEntry::class, $cache); |
||
120 | |||
121 | self::assertArrayHasKey('id', $cache->data); |
||
122 | self::assertArrayHasKey('name', $cache->data); |
||
123 | self::assertArrayHasKey('country', $cache->data); |
||
124 | self::assertEquals( |
||
125 | [ |
||
126 | 'id' => 12, |
||
127 | 'name' => 'Bar', |
||
128 | 'country' => new AssociationCacheEntry(Country::class, ['id' => 11]), |
||
129 | ], |
||
130 | $cache->data |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | public function testBuildCacheEntryNonInitializedAssocProxy() : void |
||
135 | { |
||
136 | $proxy = $this->em->getReference(Country::class, 11); |
||
137 | $entity = new State('Bat', $proxy); |
||
138 | $uow = $this->em->getUnitOfWork(); |
||
139 | $entityData = ['id' => 12, 'name' => 'Bar', 'country' => $proxy]; |
||
140 | $metadata = $this->em->getClassMetadata(State::class); |
||
141 | $key = new EntityCacheKey($metadata->getClassName(), ['id' => 11]); |
||
142 | |||
143 | $entity->setId(12); |
||
144 | |||
145 | $uow->registerManaged($entity, ['id' => 12], $entityData); |
||
146 | |||
147 | $cache = $this->structure->buildCacheEntry($metadata, $key, $entity); |
||
148 | |||
149 | self::assertInstanceOf(CacheEntry::class, $cache); |
||
150 | self::assertInstanceOf(EntityCacheEntry::class, $cache); |
||
151 | |||
152 | self::assertArrayHasKey('id', $cache->data); |
||
153 | self::assertArrayHasKey('name', $cache->data); |
||
154 | self::assertArrayHasKey('country', $cache->data); |
||
155 | self::assertEquals( |
||
156 | [ |
||
157 | 'id' => 12, |
||
158 | 'name' => 'Bar', |
||
159 | 'country' => new AssociationCacheEntry(Country::class, ['id' => 11]), |
||
160 | ], |
||
161 | $cache->data |
||
162 | ); |
||
163 | } |
||
164 | |||
165 | public function testCacheEntryWithWrongIdentifierType() : void |
||
166 | { |
||
167 | $proxy = $this->em->getReference(Country::class, 11); |
||
168 | $entity = new State('Bat', $proxy); |
||
169 | $uow = $this->em->getUnitOfWork(); |
||
170 | $entityData = ['id' => 12, 'name' => 'Bar', 'country' => $proxy]; |
||
171 | $metadata = $this->em->getClassMetadata(State::class); |
||
172 | $key = new EntityCacheKey($metadata->getClassName(), ['id' => '12']); |
||
173 | |||
174 | $entity->setId(12); |
||
175 | |||
176 | $uow->registerManaged($entity, ['id' => 12], $entityData); |
||
177 | |||
178 | $cache = $this->structure->buildCacheEntry($metadata, $key, $entity); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
179 | |||
180 | self::assertInstanceOf(CacheEntry::class, $cache); |
||
181 | self::assertInstanceOf(EntityCacheEntry::class, $cache); |
||
182 | |||
183 | self::assertArrayHasKey('id', $cache->data); |
||
184 | self::assertArrayHasKey('name', $cache->data); |
||
185 | self::assertArrayHasKey('country', $cache->data); |
||
186 | self::assertSame($entity->getId(), $cache->data['id']); |
||
187 | self::assertEquals( |
||
188 | [ |
||
189 | 'id' => 12, |
||
190 | 'name' => 'Bar', |
||
191 | 'country' => new AssociationCacheEntry(Country::class, ['id' => 11]), |
||
192 | ], |
||
193 | $cache->data |
||
194 | ); |
||
195 | } |
||
196 | } |
||
197 |