These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Doctrine\Tests\ORM\Cache; |
||
4 | |||
5 | use Doctrine\ORM\Cache\AssociationCacheEntry; |
||
6 | use Doctrine\ORM\Cache\CacheEntry; |
||
7 | use Doctrine\ORM\Cache\DefaultEntityHydrator; |
||
8 | use Doctrine\ORM\Cache\EntityCacheEntry; |
||
9 | use Doctrine\ORM\Cache\EntityCacheKey; |
||
10 | use Doctrine\ORM\UnitOfWork; |
||
11 | use Doctrine\Tests\Models\Cache\Country; |
||
12 | use Doctrine\Tests\Models\Cache\State; |
||
13 | use Doctrine\Tests\OrmTestCase; |
||
14 | |||
15 | /** |
||
16 | * @group DDC-2183 |
||
17 | */ |
||
18 | class DefaultEntityHydratorTest extends OrmTestCase |
||
19 | { |
||
20 | /** |
||
21 | * @var \Doctrine\ORM\Cache\EntityHydrator |
||
22 | */ |
||
23 | private $structure; |
||
24 | |||
25 | /** |
||
26 | * @var \Doctrine\ORM\EntityManager |
||
27 | */ |
||
28 | private $em; |
||
29 | |||
30 | protected function setUp() |
||
31 | { |
||
32 | parent::setUp(); |
||
33 | |||
34 | $this->em = $this->_getTestEntityManager(); |
||
35 | $this->structure = new DefaultEntityHydrator($this->em); |
||
36 | } |
||
37 | |||
38 | public function testImplementsEntityEntryStructure() |
||
39 | { |
||
40 | $this->assertInstanceOf('\Doctrine\ORM\Cache\EntityHydrator', $this->structure); |
||
41 | } |
||
42 | |||
43 | public function testCreateEntity() |
||
44 | { |
||
45 | $metadata = $this->em->getClassMetadata(Country::class); |
||
46 | $key = new EntityCacheKey($metadata->name, ['id'=>1]); |
||
47 | $entry = new EntityCacheEntry($metadata->name, ['id'=>1, 'name'=>'Foo']); |
||
48 | $entity = $this->structure->loadCacheEntry($metadata, $key, $entry); |
||
49 | |||
50 | $this->assertInstanceOf($metadata->name, $entity); |
||
51 | |||
52 | $this->assertEquals(1, $entity->getId()); |
||
53 | $this->assertEquals('Foo', $entity->getName()); |
||
54 | $this->assertEquals(UnitOfWork::STATE_MANAGED, $this->em->getUnitOfWork()->getEntityState($entity)); |
||
55 | } |
||
56 | |||
57 | public function testLoadProxy() |
||
58 | { |
||
59 | $metadata = $this->em->getClassMetadata(Country::class); |
||
60 | $key = new EntityCacheKey($metadata->name, ['id'=>1]); |
||
61 | $entry = new EntityCacheEntry($metadata->name, ['id'=>1, 'name'=>'Foo']); |
||
62 | $proxy = $this->em->getReference($metadata->name, $key->identifier); |
||
63 | $entity = $this->structure->loadCacheEntry($metadata, $key, $entry, $proxy); |
||
64 | |||
65 | $this->assertInstanceOf($metadata->name, $entity); |
||
66 | $this->assertSame($proxy, $entity); |
||
67 | |||
68 | $this->assertEquals(1, $entity->getId()); |
||
69 | $this->assertEquals('Foo', $entity->getName()); |
||
70 | $this->assertEquals(UnitOfWork::STATE_MANAGED, $this->em->getUnitOfWork()->getEntityState($proxy)); |
||
71 | } |
||
72 | |||
73 | public function testBuildCacheEntry() |
||
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->name, ['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 | $this->assertInstanceOf(CacheEntry::class, $cache); |
||
87 | $this->assertInstanceOf(EntityCacheEntry::class, $cache); |
||
88 | |||
89 | $this->assertArrayHasKey('id', $cache->data); |
||
90 | $this->assertArrayHasKey('name', $cache->data); |
||
91 | $this->assertEquals( |
||
92 | [ |
||
93 | 'id' => 1, |
||
94 | 'name' => 'Foo', |
||
95 | ], $cache->data); |
||
96 | } |
||
97 | |||
98 | public function testBuildCacheEntryAssociation() |
||
99 | { |
||
100 | $country = new Country('Foo'); |
||
101 | $state = new State('Bat', $country); |
||
102 | $uow = $this->em->getUnitOfWork(); |
||
103 | $countryData = ['id'=>11, 'name'=>'Foo']; |
||
104 | $stateData = ['id'=>12, 'name'=>'Bar', 'country' => $country]; |
||
105 | $metadata = $this->em->getClassMetadata(State::class); |
||
106 | $key = new EntityCacheKey($metadata->name, ['id'=>11]); |
||
107 | |||
108 | $country->setId(11); |
||
109 | $state->setId(12); |
||
110 | |||
111 | $uow->registerManaged($country, ['id'=>11], $countryData); |
||
112 | $uow->registerManaged($state, ['id'=>12], $stateData); |
||
113 | |||
114 | $cache = $this->structure->buildCacheEntry($metadata, $key, $state); |
||
115 | |||
116 | $this->assertInstanceOf(CacheEntry::class, $cache); |
||
117 | $this->assertInstanceOf(EntityCacheEntry::class, $cache); |
||
118 | |||
119 | $this->assertArrayHasKey('id', $cache->data); |
||
120 | $this->assertArrayHasKey('name', $cache->data); |
||
121 | $this->assertArrayHasKey('country', $cache->data); |
||
122 | $this->assertEquals( |
||
123 | [ |
||
124 | 'id' => 12, |
||
125 | 'name' => 'Bar', |
||
126 | 'country' => new AssociationCacheEntry(Country::class, ['id' => 11]), |
||
127 | ], $cache->data); |
||
128 | } |
||
129 | |||
130 | public function testBuildCacheEntryNonInitializedAssocProxy() |
||
131 | { |
||
132 | $proxy = $this->em->getReference(Country::class, 11); |
||
133 | $entity = new State('Bat', $proxy); |
||
134 | $uow = $this->em->getUnitOfWork(); |
||
135 | $entityData = ['id'=>12, 'name'=>'Bar', 'country' => $proxy]; |
||
136 | $metadata = $this->em->getClassMetadata(State::class); |
||
137 | $key = new EntityCacheKey($metadata->name, ['id'=>11]); |
||
138 | |||
139 | $entity->setId(12); |
||
140 | |||
141 | $uow->registerManaged($entity, ['id'=>12], $entityData); |
||
142 | |||
143 | $cache = $this->structure->buildCacheEntry($metadata, $key, $entity); |
||
144 | |||
145 | $this->assertInstanceOf(CacheEntry::class, $cache); |
||
146 | $this->assertInstanceOf(EntityCacheEntry::class, $cache); |
||
147 | |||
148 | $this->assertArrayHasKey('id', $cache->data); |
||
149 | $this->assertArrayHasKey('name', $cache->data); |
||
150 | $this->assertArrayHasKey('country', $cache->data); |
||
151 | $this->assertEquals( |
||
152 | [ |
||
153 | 'id' => 12, |
||
154 | 'name' => 'Bar', |
||
155 | 'country' => new AssociationCacheEntry(Country::class, ['id' => 11]), |
||
156 | ], $cache->data); |
||
157 | } |
||
158 | |||
159 | public function testCacheEntryWithWrongIdentifierType() |
||
160 | { |
||
161 | $proxy = $this->em->getReference(Country::class, 11); |
||
162 | $entity = new State('Bat', $proxy); |
||
0 ignored issues
–
show
|
|||
163 | $uow = $this->em->getUnitOfWork(); |
||
164 | $entityData = ['id'=> 12, 'name'=>'Bar', 'country' => $proxy]; |
||
165 | $metadata = $this->em->getClassMetadata(State::class); |
||
166 | $key = new EntityCacheKey($metadata->name, ['id'=>'12']); |
||
167 | |||
168 | $entity->setId(12); |
||
169 | |||
170 | $uow->registerManaged($entity, ['id'=>12], $entityData); |
||
171 | |||
172 | $cache = $this->structure->buildCacheEntry($metadata, $key, $entity); |
||
173 | |||
174 | $this->assertInstanceOf(CacheEntry::class, $cache); |
||
175 | $this->assertInstanceOf(EntityCacheEntry::class, $cache); |
||
176 | |||
177 | $this->assertArrayHasKey('id', $cache->data); |
||
178 | $this->assertArrayHasKey('name', $cache->data); |
||
179 | $this->assertArrayHasKey('country', $cache->data); |
||
180 | $this->assertSame($entity->getId(), $cache->data['id']); |
||
181 | $this->assertEquals( |
||
182 | [ |
||
183 | 'id' => 12, |
||
184 | 'name' => 'Bar', |
||
185 | 'country' => new AssociationCacheEntry(Country::class, ['id' => 11]), |
||
186 | ], $cache->data); |
||
187 | } |
||
188 | |||
189 | } |
||
190 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.