Passed
Push — types ( 30d5cd )
by Jonathan
04:47
created

FileDriverTest::testIsTransient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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