Failed Conditions
Pull Request — master (#1)
by Jonathan
13:22 queued 10:46
created

TestClassMetadataFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
dl 0
loc 61
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getFqcnFromAlias() 0 3 1
A isEntity() 0 3 1
A isTransient() 0 3 1
A getDriver() 0 3 1
A initialize() 0 2 1
A initializeReflection() 0 2 1
A newClassMetadataInstance() 0 3 1
A onNotFoundMetadata() 0 7 2
A wakeupReflection() 0 2 1
A doLoadMetadata() 0 2 1
A __construct() 0 4 1
1
<?php
2
3
namespace Doctrine\Tests\Common\Persistence\Mapping;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
7
use Doctrine\Common\Persistence\Mapping\MappingException;
8
use Doctrine\Tests\DoctrineTestCase;
9
use Doctrine\Common\Persistence\Mapping\ReflectionService;
10
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
11
use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
12
use Doctrine\Common\Cache\ArrayCache;
13
14
/**
15
 * @covers \Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory
16
 */
17
class ClassMetadataFactoryTest extends DoctrineTestCase
18
{
19
    /**
20
     * @var TestClassMetadataFactory
21
     */
22
    private $cmf;
23
24
    public function setUp()
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()
32
    {
33
        self::assertNull($this->cmf->getCacheDriver());
34
        $cache = new ArrayCache();
35
        $this->cmf->setCacheDriver($cache);
36
        self::assertSame($cache, $this->cmf->getCacheDriver());
37
    }
38
39
    public function testGetMetadataFor()
40
    {
41
        $metadata = $this->cmf->getMetadataFor('stdClass');
42
43
        self::assertInstanceOf(ClassMetadata::class, $metadata);
44
        self::assertTrue($this->cmf->hasMetadataFor('stdClass'));
45
    }
46
47
    public function testGetMetadataForAbsentClass()
48
    {
49
        $this->expectException(MappingException::class);
50
        $this->cmf->getMetadataFor(__NAMESPACE__ . '\AbsentClass');
51
    }
52
53
    public function testGetParentMetadata()
54
    {
55
        $metadata = $this->cmf->getMetadataFor(ChildEntity::class);
56
57
        self::assertInstanceOf(ClassMetadata::class, $metadata);
58
        self::assertTrue($this->cmf->hasMetadataFor(ChildEntity::class));
59
        self::assertTrue($this->cmf->hasMetadataFor(RootEntity::class));
60
    }
61
62
    public function testGetCachedMetadata()
63
    {
64
        $metadata = $this->createMock(ClassMetadata::class);
65
        $cache    = new ArrayCache();
66
        $cache->save(ChildEntity::class . '$CLASSMETADATA', $metadata);
67
68
        $this->cmf->setCacheDriver($cache);
69
70
        self::assertSame($metadata, $this->cmf->getMetadataFor(ChildEntity::class));
71
    }
72
73
    public function testCacheGetMetadataFor()
74
    {
75
        $cache = new ArrayCache();
76
        $this->cmf->setCacheDriver($cache);
77
78
        $loadedMetadata = $this->cmf->getMetadataFor(ChildEntity::class);
79
80
        self::assertSame($loadedMetadata, $cache->fetch(ChildEntity::class . '$CLASSMETADATA'));
81
    }
82
83
    public function testGetAliasedMetadata()
84
    {
85
        $this->cmf->getMetadataFor('prefix:ChildEntity');
86
87
        self::assertTrue($this->cmf->hasMetadataFor(__NAMESPACE__ . '\ChildEntity'));
88
        self::assertTrue($this->cmf->hasMetadataFor('prefix:ChildEntity'));
89
    }
90
91
    /**
92
     * @group DCOM-270
93
     */
94
    public function testGetInvalidAliasedMetadata()
95
    {
96
        $this->expectException(MappingException::class);
97
        $this->expectExceptionMessage(
98
            'Class \'Doctrine\Tests\Common\Persistence\Mapping\ChildEntity:Foo\' does not exist'
99
        );
100
101
        $this->cmf->getMetadataFor('prefix:ChildEntity:Foo');
102
    }
103
104
    /**
105
     * @group DCOM-270
106
     */
107
    public function testClassIsTransient()
108
    {
109
        self::assertTrue($this->cmf->isTransient('prefix:ChildEntity:Foo'));
110
    }
111
112
    public function testWillFallbackOnNotLoadedMetadata()
113
    {
114
        $classMetadata = $this->createMock(ClassMetadata::class);
115
116
        $this->cmf->fallbackCallback = function () use ($classMetadata) {
117
            return $classMetadata;
118
        };
119
120
        $this->cmf->metadata = null;
121
122
        self::assertSame($classMetadata, $this->cmf->getMetadataFor('Foo'));
123
    }
124
125
    public function testWillFailOnFallbackFailureWithNotLoadedMetadata()
126
    {
127
        $this->cmf->fallbackCallback = function () {
128
            return null;
129
        };
130
131
        $this->cmf->metadata = null;
132
133
        $this->expectException(MappingException::class);
134
135
        $this->cmf->getMetadataFor('Foo');
136
    }
137
138
    /**
139
     * @group 717
140
     */
141
    public function testWillIgnoreCacheEntriesThatAreNotMetadataInstances()
142
    {
143
        /* @var $cacheDriver Cache|\PHPUnit_Framework_MockObject_MockObject */
144
        $cacheDriver = $this->createMock(Cache::class);
145
146
        $this->cmf->setCacheDriver($cacheDriver);
147
148
        $cacheDriver->expects(self::once())->method('fetch')->with('Foo$CLASSMETADATA')->willReturn(new \stdClass());
149
150
        /* @var $metadata ClassMetadata */
151
        $metadata = $this->createMock(ClassMetadata::class);
152
153
        /** @var \PHPUnit_Framework_MockObject_MockObject|\stdClass|callable $fallbackCallback */
154
        $fallbackCallback = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();
155
156
        $fallbackCallback->expects(self::any())->method('__invoke')->willReturn($metadata);
157
158
        $this->cmf->fallbackCallback = $fallbackCallback;
159
160
        self::assertSame($metadata, $this->cmf->getMetadataFor('Foo'));
161
    }
162
}
163
164
class TestClassMetadataFactory extends AbstractClassMetadataFactory
165
{
166
    public $driver;
167
    public $metadata;
168
169
    /** @var callable|null */
170
    public $fallbackCallback;
171
172
    public function __construct($driver, $metadata)
173
    {
174
        $this->driver   = $driver;
175
        $this->metadata = $metadata;
176
    }
177
178
    protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents)
179
    {
180
    }
181
182
    protected function getFqcnFromAlias($namespaceAlias, $simpleClassName)
183
    {
184
        return __NAMESPACE__ . '\\' . $simpleClassName;
185
    }
186
187
    protected function initialize()
188
    {
189
    }
190
191
    protected function newClassMetadataInstance($className)
192
    {
193
        return $this->metadata;
194
    }
195
196
    protected function getDriver()
197
    {
198
        return $this->driver;
199
    }
200
    protected function wakeupReflection(ClassMetadata $class, ReflectionService $reflService)
201
    {
202
    }
203
204
    protected function initializeReflection(ClassMetadata $class, ReflectionService $reflService)
205
    {
206
    }
207
208
    protected function isEntity(ClassMetadata $class)
209
    {
210
        return true;
211
    }
212
213
    protected function onNotFoundMetadata($className)
214
    {
215
        if ( ! $fallback = $this->fallbackCallback) {
216
            return null;
217
        }
218
219
        return $fallback();
220
    }
221
222
    public function isTransient($class)
223
    {
224
        return true;
225
    }
226
}
227
228
class RootEntity
229
{
230
231
}
232
233
class ChildEntity extends RootEntity
234
{
235
236
}
237