Passed
Push — master ( a46388...64d40b )
by Jonathan
50s queued 10s
created

testGetAllClassNamesBothSourcesNoDupes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
172
        }
173
174
        return ['stdClass' => 'stdClass'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('stdClass' => 'stdClass') returns the type array<string,string> which is incompatible with the return type mandated by Doctrine\Persistence\Map...iver::loadMappingFile() of Doctrine\Persistence\Mapping\ClassMetadata[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
175
    }
176
177
    public function loadMetadataForClass($className, ClassMetadata $metadata)
178
    {
179
    }
180
}
181