1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\Persistence\Mapping; |
6
|
|
|
|
7
|
|
|
use Cache\Adapter\PHPArray\ArrayCachePool; |
8
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
9
|
|
|
use Doctrine\Common\Cache\Cache; |
10
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata; |
11
|
|
|
use Doctrine\Persistence\Mapping\Driver\MappingDriver; |
12
|
|
|
use Doctrine\Persistence\Mapping\MappingException; |
13
|
|
|
use Doctrine\Tests\DoctrineTestCase; |
14
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
15
|
|
|
use stdClass; |
16
|
|
|
use function str_replace; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @covers \Doctrine\Persistence\Mapping\AbstractClassMetadataFactory |
20
|
|
|
*/ |
21
|
|
|
class ClassMetadataFactoryTest extends DoctrineTestCase |
22
|
|
|
{ |
23
|
|
|
/** @var TestClassMetadataFactory */ |
24
|
|
|
private $cmf; |
25
|
|
|
|
26
|
|
|
protected function setUp() : void |
27
|
|
|
{ |
28
|
|
|
$driver = $this->createMock(MappingDriver::class); |
29
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
30
|
|
|
$this->cmf = new TestClassMetadataFactory($driver, $metadata); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testGetCacheDriver() : void |
34
|
|
|
{ |
35
|
|
|
self::assertNull($this->cmf->getCacheDriver()); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$cache = new ArrayCache(); |
38
|
|
|
|
39
|
|
|
$this->cmf->setCacheDriver($cache); |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** @var ArrayCache $cacheDriver */ |
42
|
|
|
$cacheDriver = $this->cmf->getCacheDriver(); |
|
|
|
|
43
|
|
|
|
44
|
|
|
self::assertSame($cache, $cacheDriver); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testGetMetadataFor() : void |
48
|
|
|
{ |
49
|
|
|
$metadata = $this->cmf->getMetadataFor('stdClass'); |
|
|
|
|
50
|
|
|
|
51
|
|
|
self::assertTrue($this->cmf->hasMetadataFor('stdClass')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGetMetadataForAbsentClass() : void |
55
|
|
|
{ |
56
|
|
|
$this->expectException(MappingException::class); |
57
|
|
|
$this->cmf->getMetadataFor(__NAMESPACE__ . '\AbsentClass'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testGetParentMetadata() : void |
61
|
|
|
{ |
62
|
|
|
$metadata = $this->cmf->getMetadataFor(ChildEntity::class); |
|
|
|
|
63
|
|
|
|
64
|
|
|
self::assertTrue($this->cmf->hasMetadataFor(ChildEntity::class)); |
65
|
|
|
self::assertTrue($this->cmf->hasMetadataFor(RootEntity::class)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGetCachedMetadataLegacy() : void |
69
|
|
|
{ |
70
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
71
|
|
|
$cache = new ArrayCache(); |
72
|
|
|
$cache->save(str_replace('\\', '.', ChildEntity::class) . '$CLASSMETADATA', $metadata); |
73
|
|
|
|
74
|
|
|
$this->cmf->setCacheDriver($cache); |
|
|
|
|
75
|
|
|
|
76
|
|
|
self::assertEquals($metadata, $this->cmf->getMetadataFor(ChildEntity::class)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testGetCachedMetadata() : void |
80
|
|
|
{ |
81
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
82
|
|
|
$cache = new ArrayCachePool(); |
83
|
|
|
$cache->set(str_replace('\\', '.', ChildEntity::class) . '$CLASSMETADATA', $metadata); |
84
|
|
|
|
85
|
|
|
$this->cmf->setCache($cache); |
86
|
|
|
|
87
|
|
|
self::assertEquals($metadata, $this->cmf->getMetadataFor(ChildEntity::class)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testCacheGetMetadataForLegacy() : void |
91
|
|
|
{ |
92
|
|
|
$cache = new ArrayCache(); |
93
|
|
|
$this->cmf->setCacheDriver($cache); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$loadedMetadata = $this->cmf->getMetadataFor(ChildEntity::class); |
96
|
|
|
|
97
|
|
|
self::assertEquals($loadedMetadata, $cache->fetch(str_replace('\\', '.', ChildEntity::class) . '$CLASSMETADATA')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testCacheGetMetadataFor() : void |
101
|
|
|
{ |
102
|
|
|
$cache = new ArrayCachePool(); |
103
|
|
|
$this->cmf->setCache($cache); |
104
|
|
|
|
105
|
|
|
$loadedMetadata = $this->cmf->getMetadataFor(ChildEntity::class); |
106
|
|
|
|
107
|
|
|
self::assertEquals($loadedMetadata, $cache->get(str_replace('\\', '.', ChildEntity::class) . '$CLASSMETADATA')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testGetAliasedMetadata() : void |
111
|
|
|
{ |
112
|
|
|
$this->cmf->getMetadataFor('prefix:ChildEntity'); |
113
|
|
|
|
114
|
|
|
self::assertTrue($this->cmf->hasMetadataFor(__NAMESPACE__ . '\ChildEntity')); |
115
|
|
|
self::assertTrue($this->cmf->hasMetadataFor('prefix:ChildEntity')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @group DCOM-270 |
120
|
|
|
*/ |
121
|
|
|
public function testGetInvalidAliasedMetadata() : void |
122
|
|
|
{ |
123
|
|
|
$this->expectException(MappingException::class); |
124
|
|
|
$this->expectExceptionMessage( |
125
|
|
|
'Class \'Doctrine\Tests\Persistence\Mapping\ChildEntity:Foo\' does not exist' |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->cmf->getMetadataFor('prefix:ChildEntity:Foo'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @group DCOM-270 |
133
|
|
|
*/ |
134
|
|
|
public function testClassIsTransient() : void |
135
|
|
|
{ |
136
|
|
|
self::assertTrue($this->cmf->isTransient('prefix:ChildEntity:Foo')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testWillFallbackOnNotLoadedMetadata() : void |
140
|
|
|
{ |
141
|
|
|
$classMetadata = $this->createMock(ClassMetadata::class); |
142
|
|
|
|
143
|
|
|
$this->cmf->fallbackCallback = static function () use ($classMetadata) { |
144
|
|
|
return $classMetadata; |
145
|
|
|
}; |
146
|
|
|
|
147
|
|
|
/** @var ClassMetadata $fooClassMetadata */ |
148
|
|
|
$fooClassMetadata = $this->cmf->getMetadataFor('Foo'); |
149
|
|
|
|
150
|
|
|
self::assertSame($classMetadata, $fooClassMetadata); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testWillFailOnFallbackFailureWithNotLoadedMetadata() : void |
154
|
|
|
{ |
155
|
|
|
$this->cmf->fallbackCallback = static function () { |
156
|
|
|
return null; |
157
|
|
|
}; |
158
|
|
|
|
159
|
|
|
$this->expectException(MappingException::class); |
160
|
|
|
$this->expectExceptionMessage("Class 'Foo' does not exist"); |
161
|
|
|
|
162
|
|
|
$this->cmf->getMetadataFor('Foo'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @group 717 |
167
|
|
|
*/ |
168
|
|
|
public function testWillIgnoreCacheEntriesThatAreNotMetadataInstances() : void |
169
|
|
|
{ |
170
|
|
|
/** @var Cache|MockObject $cacheDriver */ |
171
|
|
|
$cacheDriver = $this->createMock(Cache::class); |
172
|
|
|
|
173
|
|
|
$this->cmf->setCacheDriver($cacheDriver); |
|
|
|
|
174
|
|
|
|
175
|
|
|
$cacheDriver->expects(self::once())->method('fetch')->with('Foo$CLASSMETADATA')->willReturn(new stdClass()); |
176
|
|
|
|
177
|
|
|
/** @var ClassMetadata $metadata */ |
178
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
179
|
|
|
|
180
|
|
|
/** @var MockObject|stdClass|callable $fallbackCallback */ |
181
|
|
|
$fallbackCallback = $this->getMockBuilder(stdClass::class)->setMethods(['__invoke'])->getMock(); |
182
|
|
|
|
183
|
|
|
$fallbackCallback->expects(self::any())->method('__invoke')->willReturn($metadata); |
184
|
|
|
|
185
|
|
|
$this->cmf->fallbackCallback = $fallbackCallback; |
186
|
|
|
|
187
|
|
|
self::assertSame($metadata, $this->cmf->getMetadataFor('Foo')); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
class RootEntity |
192
|
|
|
{ |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
class ChildEntity extends RootEntity |
196
|
|
|
{ |
197
|
|
|
} |
198
|
|
|
|