Completed
Pull Request — master (#50)
by Jonathan
04:15 queued 01:55
created

FileDriverTest::createTestFileDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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