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