Completed
Pull Request — master (#50)
by Jonathan
05:13 queued 03:02
created

FileDriverTest::testGetElementFromGlobalFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
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::assertNull($driver->getGlobalBasename());
21
22
        $driver->setGlobalBasename('global');
23
        self::assertSame('global', $driver->getGlobalBasename());
24
    }
25
26
    public function testGetElementFromGlobalFile() : void
27
    {
28
        $driver = $this->createTestFileDriver($this->newLocator());
29
30
        $driver->setGlobalBasename('global');
31
32
        $element = $driver->getElement('stdGlobal');
33
34
        self::assertSame($driver->stdGlobal, $element);
35
    }
36
37
    public function testGetElementFromFile() : void
38
    {
39
        $locator = $this->newLocator();
40
        $locator->expects(self::once())
41
                ->method('findMappingFile')
42
                ->with(self::equalTo('stdClass'))
43
                ->will(self::returnValue(__DIR__ . '/_files/stdClass.yml'));
44
45
        $driver = $this->createTestFileDriver($locator);
46
47
        self::assertSame($driver->stdClass, $driver->getElement('stdClass'));
48
    }
49
50
    public function testGetElementUpdatesClassCache() : void
51
    {
52
        $locator = $this->newLocator();
53
54
        // findMappingFile should only be called once
55
        $locator->expects(self::once())
56
            ->method('findMappingFile')
57
            ->with(self::equalTo('stdClass'))
58
            ->will(self::returnValue(__DIR__ . '/_files/stdClass.yml'));
59
60
        $driver = $this->createTestFileDriver($locator);
61
62
        // not cached
63
        self::assertSame($driver->stdClass, $driver->getElement('stdClass'));
64
65
        // cached call
66
        self::assertSame($driver->stdClass, $driver->getElement('stdClass'));
67
    }
68
69
    public function testGetAllClassNamesGlobalBasename() : void
70
    {
71
        $driver = $this->createTestFileDriver($this->newLocator());
72
        $driver->setGlobalBasename('global');
73
74
        $classNames = $driver->getAllClassNames();
75
76
        self::assertSame(['stdGlobal', 'stdGlobal2'], $classNames);
77
    }
78
79
    public function testGetAllClassNamesFromMappingFile() : void
80
    {
81
        $locator = $this->newLocator();
82
        $locator->expects(self::any())
83
                ->method('getAllClassNames')
84
                ->with(self::equalTo(null))
85
                ->will(self::returnValue(['stdClass']));
86
87
        $driver = $this->createTestFileDriver($locator);
88
89
        $classNames = $driver->getAllClassNames();
90
91
        self::assertSame(['stdClass'], $classNames);
92
    }
93
94
    public function testGetAllClassNamesBothSources() : void
95
    {
96
        $locator = $this->newLocator();
97
        $locator->expects(self::any())
98
                ->method('getAllClassNames')
99
                ->with(self::equalTo('global'))
100
                ->will(self::returnValue(['stdClass']));
101
102
        $driver = $this->createTestFileDriver($locator);
103
        $driver->setGlobalBasename('global');
104
105
        $classNames = $driver->getAllClassNames();
106
107
        self::assertSame(['stdGlobal', 'stdGlobal2', 'stdClass'], $classNames);
108
    }
109
110
    public function testIsNotTransient() : void
111
    {
112
        $locator = $this->newLocator();
113
        $locator->expects(self::once())
114
                ->method('fileExists')
115
                ->with(self::equalTo('stdClass'))
116
                ->will(self::returnValue(true));
117
118
        $driver = $this->createTestFileDriver($locator);
119
        $driver->setGlobalBasename('global');
120
121
        self::assertFalse($driver->isTransient('stdClass'));
122
        self::assertFalse($driver->isTransient('stdGlobal'));
123
        self::assertFalse($driver->isTransient('stdGlobal2'));
124
    }
125
126
    public function testIsTransient() : void
127
    {
128
        $locator = $this->newLocator();
129
        $locator->expects(self::once())
130
                ->method('fileExists')
131
                ->with(self::equalTo('stdClass2'))
132
                ->will(self::returnValue(false));
133
134
        $driver = $this->createTestFileDriver($locator);
135
136
        self::assertTrue($driver->isTransient('stdClass2'));
137
    }
138
139
    public function testNonLocatorFallback() : void
140
    {
141
        $driver = $this->createTestFileDriver(__DIR__ . '/_files', '.yml');
142
        self::assertTrue($driver->isTransient('stdClass2'));
143
        self::assertFalse($driver->isTransient('stdClass'));
144
    }
145
146
    /**
147
     * @return FileLocator|MockObject
148
     */
149
    private function newLocator() : FileLocator
150
    {
151
        $locator = $this->createMock(FileLocator::class);
152
        $locator->expects(self::any())->method('getFileExtension')->will(self::returnValue('.yml'));
153
        $locator->expects(self::any())->method('getPaths')->will(self::returnValue([__DIR__ . '/_files']));
154
155
        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...
156
    }
157
158
    /**
159
     * @param string|string[]|FileLocator $locator
160
     */
161
    private function createTestFileDriver($locator, ?string $fileExtension = null) : TestFileDriver
162
    {
163
        $driver = new TestFileDriver($locator, $fileExtension);
164
165
        $driver->stdClass   = $this->createMock(ClassMetadata::class);
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...g\ClassMetadata::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\Persistence\Mapping\ClassMetadata of property $stdClass.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
166
        $driver->stdGlobal  = $this->createMock(ClassMetadata::class);
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...g\ClassMetadata::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\Persistence\Mapping\ClassMetadata of property $stdGlobal.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
167
        $driver->stdGlobal2 = $this->createMock(ClassMetadata::class);
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...g\ClassMetadata::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\Persistence\Mapping\ClassMetadata of property $stdGlobal2.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
168
169
        return $driver;
170
    }
171
}
172
173
class TestFileDriver extends FileDriver
174
{
175
    /** @var ClassMetadata */
176
    public $stdGlobal;
177
178
    /** @var ClassMetadata */
179
    public $stdGlobal2;
180
181
    /** @var ClassMetadata */
182
    public $stdClass;
183
184
    /**
185
     * {@inheritDoc}
186
     */
187
    protected function loadMappingFile(string $file) : array
188
    {
189
        if (strpos($file, 'global.yml') !== false) {
190
            return [
191
                'stdGlobal' => $this->stdGlobal,
192
                'stdGlobal2' => $this->stdGlobal2,
193
            ];
194
        }
195
196
        return ['stdClass' => $this->stdClass];
197
    }
198
199
    public function loadMetadataForClass(string $className, ClassMetadata $metadata) : void
200
    {
201
    }
202
}
203