Passed
Pull Request — master (#70)
by Alexander M.
06:58
created

testCacheGetMetadataForLegacy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
17
/**
18
 * @covers \Doctrine\Persistence\Mapping\AbstractClassMetadataFactory
19
 */
20
class ClassMetadataFactoryTest extends DoctrineTestCase
21
{
22
    /** @var TestClassMetadataFactory */
23
    private $cmf;
24
25
    protected function setUp() : void
26
    {
27
        $driver    = $this->createMock(MappingDriver::class);
28
        $metadata  = $this->createMock(ClassMetadata::class);
29
        $this->cmf = new TestClassMetadataFactory($driver, $metadata);
30
    }
31
32
    public function testGetCacheDriver() : void
33
    {
34
        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

34
        self::assertNull(/** @scrutinizer ignore-deprecated */ $this->cmf->getCacheDriver());
Loading history...
35
36
        $cache = new ArrayCache();
37
38
        $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

38
        /** @scrutinizer ignore-deprecated */ $this->cmf->setCacheDriver($cache);
Loading history...
39
40
        /** @var ArrayCache $cacheDriver */
41
        $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

41
        $cacheDriver = /** @scrutinizer ignore-deprecated */ $this->cmf->getCacheDriver();
Loading history...
42
43
        self::assertSame($cache, $cacheDriver);
44
    }
45
46
    public function testGetMetadataFor() : void
47
    {
48
        $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...
49
50
        self::assertTrue($this->cmf->hasMetadataFor('stdClass'));
51
    }
52
53
    public function testGetMetadataForAbsentClass() : void
54
    {
55
        $this->expectException(MappingException::class);
56
        $this->cmf->getMetadataFor(__NAMESPACE__ . '\AbsentClass');
57
    }
58
59
    public function testGetParentMetadata() : void
60
    {
61
        $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...
62
63
        self::assertTrue($this->cmf->hasMetadataFor(ChildEntity::class));
64
        self::assertTrue($this->cmf->hasMetadataFor(RootEntity::class));
65
    }
66
67
    public function testGetCachedMetadataLegacy() : void
68
    {
69
        $metadata = $this->createMock(ClassMetadata::class);
70
        $cache    = new ArrayCache();
71
        $cache->save(str_replace('\\', '.', ChildEntity::class) . '$CLASSMETADATA', $metadata);
0 ignored issues
show
introduced by
Function str_replace() should not be referenced via a fallback global name, but via a use statement.
Loading history...
72
73
        $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

73
        /** @scrutinizer ignore-deprecated */ $this->cmf->setCacheDriver($cache);
Loading history...
74
75
        self::assertEquals($metadata, $this->cmf->getMetadataFor(ChildEntity::class));
76
    }
77
78
    public function testGetCachedMetadata() : void
79
    {
80
        $metadata = $this->createMock(ClassMetadata::class);
81
        $cache    = new ArrayCachePool();
82
        $cache->set(str_replace('\\', '.', ChildEntity::class) . '$CLASSMETADATA', $metadata);
0 ignored issues
show
introduced by
Function str_replace() should not be referenced via a fallback global name, but via a use statement.
Loading history...
83
84
        $this->cmf->setCache($cache);
85
86
        self::assertEquals($metadata, $this->cmf->getMetadataFor(ChildEntity::class));
87
    }
88
89
    public function testCacheGetMetadataForLegacy() : void
90
    {
91
        $cache = new ArrayCache();
92
        $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

92
        /** @scrutinizer ignore-deprecated */ $this->cmf->setCacheDriver($cache);
Loading history...
93
94
        $loadedMetadata = $this->cmf->getMetadataFor(ChildEntity::class);
95
96
        self::assertEquals($loadedMetadata, $cache->fetch(str_replace('\\', '.', ChildEntity::class) . '$CLASSMETADATA'));
0 ignored issues
show
introduced by
Function str_replace() should not be referenced via a fallback global name, but via a use statement.
Loading history...
97
    }
98
99
    public function testCacheGetMetadataFor() : void
100
    {
101
        $cache = new ArrayCachePool();
102
        $this->cmf->setCache($cache);
103
104
        $loadedMetadata = $this->cmf->getMetadataFor(ChildEntity::class);
105
106
        self::assertEquals($loadedMetadata, $cache->get(str_replace('\\', '.', ChildEntity::class) . '$CLASSMETADATA'));
0 ignored issues
show
introduced by
Function str_replace() should not be referenced via a fallback global name, but via a use statement.
Loading history...
107
    }
108
109
    public function testGetAliasedMetadata() : void
110
    {
111
        $this->cmf->getMetadataFor('prefix:ChildEntity');
112
113
        self::assertTrue($this->cmf->hasMetadataFor(__NAMESPACE__ . '\ChildEntity'));
114
        self::assertTrue($this->cmf->hasMetadataFor('prefix:ChildEntity'));
115
    }
116
117
    /**
118
     * @group DCOM-270
119
     */
120
    public function testGetInvalidAliasedMetadata() : void
121
    {
122
        $this->expectException(MappingException::class);
123
        $this->expectExceptionMessage(
124
            'Class \'Doctrine\Tests\Persistence\Mapping\ChildEntity:Foo\' does not exist'
125
        );
126
127
        $this->cmf->getMetadataFor('prefix:ChildEntity:Foo');
128
    }
129
130
    /**
131
     * @group DCOM-270
132
     */
133
    public function testClassIsTransient() : void
134
    {
135
        self::assertTrue($this->cmf->isTransient('prefix:ChildEntity:Foo'));
136
    }
137
138
    public function testWillFallbackOnNotLoadedMetadata() : void
139
    {
140
        $classMetadata = $this->createMock(ClassMetadata::class);
141
142
        $this->cmf->fallbackCallback = static function () use ($classMetadata) {
143
            return $classMetadata;
144
        };
145
146
        /** @var ClassMetadata $fooClassMetadata */
147
        $fooClassMetadata = $this->cmf->getMetadataFor('Foo');
148
149
        self::assertSame($classMetadata, $fooClassMetadata);
150
    }
151
152
    public function testWillFailOnFallbackFailureWithNotLoadedMetadata() : void
153
    {
154
        $this->cmf->fallbackCallback = static function () {
155
            return null;
156
        };
157
158
        $this->expectException(MappingException::class);
159
        $this->expectExceptionMessage("Class 'Foo' does not exist");
160
161
        $this->cmf->getMetadataFor('Foo');
162
    }
163
164
    /**
165
     * @group 717
166
     */
167
    public function testWillIgnoreCacheEntriesThatAreNotMetadataInstances() : void
168
    {
169
        /** @var Cache|MockObject $cacheDriver */
170
        $cacheDriver = $this->createMock(Cache::class);
171
172
        $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

172
        /** @scrutinizer ignore-deprecated */ $this->cmf->setCacheDriver($cacheDriver);
Loading history...
173
174
        $cacheDriver->expects(self::once())->method('fetch')->with('Foo$CLASSMETADATA')->willReturn(new stdClass());
175
176
        /** @var ClassMetadata $metadata */
177
        $metadata = $this->createMock(ClassMetadata::class);
178
179
        /** @var MockObject|stdClass|callable $fallbackCallback */
180
        $fallbackCallback = $this->getMockBuilder(stdClass::class)->setMethods(['__invoke'])->getMock();
181
182
        $fallbackCallback->expects(self::any())->method('__invoke')->willReturn($metadata);
183
184
        $this->cmf->fallbackCallback = $fallbackCallback;
185
186
        self::assertSame($metadata, $this->cmf->getMetadataFor('Foo'));
187
    }
188
}
189
190
class RootEntity
191
{
192
}
193
194
class ChildEntity extends RootEntity
195
{
196
}
197