Passed
Push — master ( 508821...a286ac )
by Andreas
03:48 queued 03:24
created

FileDriverTest::testGetElementUpdatesClassCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 17
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 Doctrine\Persistence\Mapping\ClassMetadata;
8
use Doctrine\Persistence\Mapping\Driver\FileDriver;
9
use Doctrine\Persistence\Mapping\Driver\FileLocator;
10
use Doctrine\Tests\DoctrineTestCase;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use function strpos;
13
14
class FileDriverTest extends DoctrineTestCase
15
{
16
    public function testGlobalBasename() : void
17
    {
18
        $driver = $this->createTestFileDriver([]);
19
20
        self::assertSame('', $driver->getGlobalBasename());
21
22
        $driver->setGlobalBasename('global');
23
24
        self::assertSame('global', $driver->getGlobalBasename());
25
    }
26
27
    public function testGetElementFromGlobalFile() : void
28
    {
29
        $driver = $this->createTestFileDriver($this->newLocator());
30
31
        $driver->setGlobalBasename('global');
32
33
        $element = $driver->getElement('stdGlobal');
34
35
        self::assertSame($driver->stdGlobal, $element);
36
    }
37
38
    public function testGetElementFromFile() : void
39
    {
40
        $locator = $this->newLocator();
41
        $locator->expects(self::once())
42
                ->method('findMappingFile')
43
                ->with(self::equalTo('stdClass'))
44
                ->will(self::returnValue(__DIR__ . '/_files/stdClass.yml'));
45
46
        $driver = $this->createTestFileDriver($locator);
47
48
        self::assertSame($driver->stdClass, $driver->getElement('stdClass'));
49
    }
50
51
    public function testGetElementUpdatesClassCache() : void
52
    {
53
        $locator = $this->newLocator();
54
55
        // findMappingFile should only be called once
56
        $locator->expects(self::once())
57
            ->method('findMappingFile')
58
            ->with(self::equalTo('stdClass'))
59
            ->will(self::returnValue(__DIR__ . '/_files/stdClass.yml'));
60
61
        $driver = $this->createTestFileDriver($locator);
62
63
        // not cached
64
        self::assertSame($driver->stdClass, $driver->getElement('stdClass'));
65
66
        // cached call
67
        self::assertSame($driver->stdClass, $driver->getElement('stdClass'));
68
    }
69
70
    public function testGetAllClassNamesGlobalBasename() : void
71
    {
72
        $driver = $this->createTestFileDriver($this->newLocator());
73
        $driver->setGlobalBasename('global');
74
75
        $classNames = $driver->getAllClassNames();
76
77
        self::assertSame(['stdGlobal', 'stdGlobal2'], $classNames);
78
    }
79
80
    public function testGetAllClassNamesFromMappingFile() : void
81
    {
82
        $locator = $this->newLocator();
83
        $locator->expects(self::any())
84
                ->method('getAllClassNames')
85
                ->with(self::equalTo(''))
86
                ->will(self::returnValue(['stdClass']));
87
88
        $driver = $this->createTestFileDriver($locator);
89
90
        $classNames = $driver->getAllClassNames();
91
92
        self::assertSame(['stdClass'], $classNames);
93
    }
94
95
    public function testGetAllClassNamesBothSources() : void
96
    {
97
        $locator = $this->newLocator();
98
        $locator->expects(self::any())
99
                ->method('getAllClassNames')
100
                ->with(self::equalTo('global'))
101
                ->will(self::returnValue(['stdClass']));
102
103
        $driver = $this->createTestFileDriver($locator);
104
        $driver->setGlobalBasename('global');
105
106
        $classNames = $driver->getAllClassNames();
107
108
        self::assertSame(['stdGlobal', 'stdGlobal2', 'stdClass'], $classNames);
109
    }
110
111
    public function testGetAllClassNamesBothSourcesNoDupes() : void
112
    {
113
        $locator = $this->newLocator();
114
        $locator->expects(self::once())
115
                ->method('getAllClassNames')
116
                ->with(self::equalTo('global'))
117
                ->willReturn(['stdClass']);
118
119
        $driver = $this->createTestFileDriver($locator);
120
        $driver->setGlobalBasename('global');
121
122
        $driver->getElement('stdClass');
123
        $classNames = $driver->getAllClassNames();
124
125
        self::assertSame(['stdGlobal', 'stdGlobal2', 'stdClass'], $classNames);
126
    }
127
128
    public function testIsNotTransient() : void
129
    {
130
        $locator = $this->newLocator();
131
        $locator->expects(self::once())
132
                ->method('fileExists')
133
                ->with(self::equalTo('stdClass'))
134
                ->will(self::returnValue(true));
135
136
        $driver = $this->createTestFileDriver($locator);
137
        $driver->setGlobalBasename('global');
138
139
        self::assertFalse($driver->isTransient('stdClass'));
140
        self::assertFalse($driver->isTransient('stdGlobal'));
141
        self::assertFalse($driver->isTransient('stdGlobal2'));
142
    }
143
144
    public function testIsTransient() : void
145
    {
146
        $locator = $this->newLocator();
147
        $locator->expects(self::once())
148
                ->method('fileExists')
149
                ->with(self::equalTo('stdClass2'))
150
                ->will(self::returnValue(false));
151
152
        $driver = $this->createTestFileDriver($locator);
153
154
        self::assertTrue($driver->isTransient('stdClass2'));
155
    }
156
157
    public function testNonLocatorFallback() : void
158
    {
159
        $driver = $this->createTestFileDriver(__DIR__ . '/_files', '.yml');
160
        self::assertTrue($driver->isTransient('stdClass2'));
161
        self::assertFalse($driver->isTransient('stdClass'));
162
    }
163
164
    /**
165
     * @return FileLocator|MockObject
166
     */
167
    private function newLocator() : FileLocator
168
    {
169
        $locator = $this->createMock(FileLocator::class);
170
        $locator->expects(self::any())->method('getFileExtension')->will(self::returnValue('.yml'));
171
        $locator->expects(self::any())->method('getPaths')->will(self::returnValue([__DIR__ . '/_files']));
172
173
        return $locator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $locator returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Doctrine\Persistence\Mapping\Driver\FileLocator.
Loading history...
174
    }
175
176
    /**
177
     * @param string|array<int, string>|FileLocator $locator
178
     */
179
    private function createTestFileDriver($locator, ?string $fileExtension = null) : TestFileDriver
180
    {
181
        $driver = new TestFileDriver($locator, $fileExtension);
182
183
        $driver->stdClass   = $this->createMock(ClassMetadata::class);
184
        $driver->stdGlobal  = $this->createMock(ClassMetadata::class);
185
        $driver->stdGlobal2 = $this->createMock(ClassMetadata::class);
186
187
        return $driver;
188
    }
189
}
190
191
class TestFileDriver extends FileDriver
192
{
193
    /** @var ClassMetadata */
194
    public $stdGlobal;
195
196
    /** @var ClassMetadata */
197
    public $stdGlobal2;
198
199
    /** @var ClassMetadata */
200
    public $stdClass;
201
202
    /**
203
     * {@inheritDoc}
204
     */
205
    protected function loadMappingFile(string $file) : array
206
    {
207
        if (strpos($file, 'global.yml') !== false) {
208
            return [
209
                'stdGlobal' => $this->stdGlobal,
210
                'stdGlobal2' => $this->stdGlobal2,
211
            ];
212
        }
213
214
        return ['stdClass' => $this->stdClass];
215
    }
216
217
    public function loadMetadataForClass(string $className, ClassMetadata $metadata) : void
218
    {
219
    }
220
}
221