testGetAllClassNamesFromMappingFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
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
        $locator = $this->newLocator();
73
        $locator->expects(self::any())->method('getAllClassNames')->with('global')->will(self::returnValue(['stdGlobal', 'stdGlobal2']));
74
75
        $driver = $this->createTestFileDriver($locator);
76
        $driver->setGlobalBasename('global');
77
78
        $classNames = $driver->getAllClassNames();
79
80
        self::assertSame(['stdGlobal', 'stdGlobal2'], $classNames);
81
    }
82
83
    public function testGetAllClassNamesFromMappingFile() : void
84
    {
85
        $locator = $this->newLocator();
86
        $locator->expects(self::any())
87
                ->method('getAllClassNames')
88
                ->with(self::equalTo(''))
89
                ->will(self::returnValue(['stdClass']));
90
91
        $driver = $this->createTestFileDriver($locator);
92
93
        $classNames = $driver->getAllClassNames();
94
95
        self::assertSame(['stdClass'], $classNames);
96
    }
97
98
    public function testGetAllClassNamesBothSources() : void
99
    {
100
        $locator = $this->newLocator();
101
        $locator->expects(self::any())
102
                ->method('getAllClassNames')
103
                ->with(self::equalTo('global'))
104
                ->will(self::returnValue(['stdClass']));
105
106
        $driver = $this->createTestFileDriver($locator);
107
        $driver->setGlobalBasename('global');
108
109
        $classNames = $driver->getAllClassNames();
110
111
        self::assertSame(['stdGlobal', 'stdGlobal2', 'stdClass'], $classNames);
112
    }
113
114
    public function testGetAllClassNamesBothSourcesNoDupes() : void
115
    {
116
        $locator = $this->newLocator();
117
        $locator->expects(self::once())
118
                ->method('getAllClassNames')
119
                ->with(self::equalTo('global'))
120
                ->willReturn(['stdClass']);
121
122
        $driver = $this->createTestFileDriver($locator);
123
        $driver->setGlobalBasename('global');
124
125
        $locator->expects(self::once())
126
                ->method('findMappingFile')
127
                ->with('stdClass')
128
                ->willReturn('');
129
130
        $driver->getElement('stdClass');
131
        $classNames = $driver->getAllClassNames();
132
133
        self::assertSame(['stdGlobal', 'stdGlobal2', 'stdClass'], $classNames);
134
    }
135
136
    public function testIsNotTransient() : void
137
    {
138
        $locator = $this->newLocator();
139
        $locator->expects(self::once())
140
                ->method('fileExists')
141
                ->with(self::equalTo('stdClass'))
142
                ->will(self::returnValue(true));
143
144
        $driver = $this->createTestFileDriver($locator);
145
        $driver->setGlobalBasename('global');
146
147
        self::assertFalse($driver->isTransient('stdClass'));
148
        self::assertFalse($driver->isTransient('stdGlobal'));
149
        self::assertFalse($driver->isTransient('stdGlobal2'));
150
    }
151
152
    public function testIsTransient() : void
153
    {
154
        $locator = $this->newLocator();
155
        $locator->expects(self::once())
156
                ->method('fileExists')
157
                ->with(self::equalTo('stdClass2'))
158
                ->will(self::returnValue(false));
159
160
        $driver = $this->createTestFileDriver($locator);
161
162
        self::assertTrue($driver->isTransient('stdClass2'));
163
    }
164
165
    public function testNonLocatorFallback() : void
166
    {
167
        $driver = $this->createTestFileDriver(__DIR__ . '/_files', '.yml');
168
        self::assertTrue($driver->isTransient('stdClass2'));
169
        self::assertFalse($driver->isTransient('stdClass'));
170
    }
171
172
    /**
173
     * @return FileLocator|MockObject
174
     */
175
    private function newLocator() : FileLocator
176
    {
177
        $locator = $this->createMock(FileLocator::class);
178
        $locator->expects(self::any())->method('getFileExtension')->will(self::returnValue('.yml'));
179
        $locator->expects(self::any())->method('getPaths')->will(self::returnValue([__DIR__ . '/_files']));
180
181
        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...
182
    }
183
184
    /**
185
     * @param string|array<int, string>|FileLocator $locator
186
     */
187
    private function createTestFileDriver($locator, ?string $fileExtension = null) : TestFileDriver
188
    {
189
        $driver = new TestFileDriver($locator, $fileExtension);
190
191
        $driver->stdClass   = $this->createMock(ClassMetadata::class);
192
        $driver->stdGlobal  = $this->createMock(ClassMetadata::class);
193
        $driver->stdGlobal2 = $this->createMock(ClassMetadata::class);
194
195
        return $driver;
196
    }
197
}
198
199
class TestFileDriver extends FileDriver
200
{
201
    /** @var ClassMetadata */
202
    public $stdGlobal;
203
204
    /** @var ClassMetadata */
205
    public $stdGlobal2;
206
207
    /** @var ClassMetadata */
208
    public $stdClass;
209
210
    /**
211
     * {@inheritDoc}
212
     */
213
    protected function loadMappingFile(string $file) : array
214
    {
215
        if (strpos($file, 'global.yml') !== false) {
216
            return [
217
                'stdGlobal' => $this->stdGlobal,
218
                'stdGlobal2' => $this->stdGlobal2,
219
            ];
220
        }
221
222
        return ['stdClass' => $this->stdClass];
223
    }
224
225
    public function loadMetadataForClass(string $className, ClassMetadata $metadata) : void
226
    {
227
    }
228
}
229