Failed Conditions
Pull Request — master (#70)
by Alexander M.
02:45
created

ClassMetadataFactoryTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 60
c 1
b 0
f 0
dl 0
loc 167
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetCachedMetadata() 0 9 1
A testClassIsTransient() 0 3 1
A testCacheGetMetadataFor() 0 8 1
A testGetAliasedMetadata() 0 6 1
A setUp() 0 5 1
A testWillFallbackOnNotLoadedMetadata() 0 12 1
A testGetCacheDriver() 0 12 1
A testCacheGetMetadataForLegacy() 0 8 1
A testWillIgnoreCacheEntriesThatAreNotMetadataInstances() 0 20 1
A testGetInvalidAliasedMetadata() 0 8 1
A testGetParentMetadata() 0 6 1
A testGetMetadataForAbsentClass() 0 4 1
A testWillFailOnFallbackFailureWithNotLoadedMetadata() 0 10 1
A testGetMetadataFor() 0 5 1
A testGetCachedMetadataLegacy() 0 9 1
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());
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Persistence\Map...ctory::getCacheDriver() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

35
        self::assertNull(/** @scrutinizer ignore-deprecated */ $this->cmf->getCacheDriver());
Loading history...
36
37
        $cache = new ArrayCache();
38
39
        $this->cmf->setCacheDriver($cache);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Persistence\Map...ctory::setCacheDriver() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

39
        /** @scrutinizer ignore-deprecated */ $this->cmf->setCacheDriver($cache);
Loading history...
40
41
        /** @var ArrayCache $cacheDriver */
42
        $cacheDriver = $this->cmf->getCacheDriver();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Persistence\Map...ctory::getCacheDriver() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

42
        $cacheDriver = /** @scrutinizer ignore-deprecated */ $this->cmf->getCacheDriver();
Loading history...
43
44
        self::assertSame($cache, $cacheDriver);
45
    }
46
47
    public function testGetMetadataFor() : void
48
    {
49
        $metadata = $this->cmf->getMetadataFor('stdClass');
0 ignored issues
show
Unused Code introduced by
The assignment to $metadata is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $metadata is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Persistence\Map...ctory::setCacheDriver() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

74
        /** @scrutinizer ignore-deprecated */ $this->cmf->setCacheDriver($cache);
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Persistence\Map...ctory::setCacheDriver() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

93
        /** @scrutinizer ignore-deprecated */ $this->cmf->setCacheDriver($cache);
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Persistence\Map...ctory::setCacheDriver() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

173
        /** @scrutinizer ignore-deprecated */ $this->cmf->setCacheDriver($cacheDriver);
Loading history...
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