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