-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathFileDriverTest.php
162 lines (127 loc) · 4.98 KB
/
FileDriverTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Doctrine\Tests\Common\Persistence\Mapping;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
use Doctrine\Common\Persistence\Mapping\Driver\FileLocator;
use Doctrine\Tests\DoctrineTestCase;
use function strpos;
class FileDriverTest extends DoctrineTestCase
{
public function testGlobalBasename()
{
$driver = new TestFileDriver([]);
self::assertNull($driver->getGlobalBasename());
$driver->setGlobalBasename('global');
self::assertSame('global', $driver->getGlobalBasename());
}
public function testGetElementFromGlobalFile()
{
$driver = new TestFileDriver($this->newLocator());
$driver->setGlobalBasename('global');
$element = $driver->getElement('stdGlobal');
self::assertSame('stdGlobal', $element);
}
public function testGetElementFromFile()
{
$locator = $this->newLocator();
$locator->expects($this->once())
->method('findMappingFile')
->with($this->equalTo('stdClass'))
->will($this->returnValue(__DIR__ . '/_files/stdClass.yml'));
$driver = new TestFileDriver($locator);
self::assertSame('stdClass', $driver->getElement('stdClass'));
}
public function testGetElementUpdatesClassCache()
{
$locator = $this->newLocator();
// findMappingFile should only be called once
$locator->expects($this->once())
->method('findMappingFile')
->with($this->equalTo('stdClass'))
->will($this->returnValue(__DIR__ . '/_files/stdClass.yml'));
$driver = new TestFileDriver($locator);
// not cached
self::assertSame('stdClass', $driver->getElement('stdClass'));
// cached call
self::assertSame('stdClass', $driver->getElement('stdClass'));
}
public function testGetAllClassNamesGlobalBasename()
{
$driver = new TestFileDriver($this->newLocator());
$driver->setGlobalBasename('global');
$classNames = $driver->getAllClassNames();
self::assertSame(['stdGlobal', 'stdGlobal2'], $classNames);
}
public function testGetAllClassNamesFromMappingFile()
{
$locator = $this->newLocator();
$locator->expects($this->any())
->method('getAllClassNames')
->with($this->equalTo(null))
->will($this->returnValue(['stdClass']));
$driver = new TestFileDriver($locator);
$classNames = $driver->getAllClassNames();
self::assertSame(['stdClass'], $classNames);
}
public function testGetAllClassNamesBothSources()
{
$locator = $this->newLocator();
$locator->expects($this->any())
->method('getAllClassNames')
->with($this->equalTo('global'))
->will($this->returnValue(['stdClass']));
$driver = new TestFileDriver($locator);
$driver->setGlobalBasename('global');
$classNames = $driver->getAllClassNames();
self::assertSame(['stdGlobal', 'stdGlobal2', 'stdClass'], $classNames);
}
public function testIsNotTransient()
{
$locator = $this->newLocator();
$locator->expects($this->once())
->method('fileExists')
->with($this->equalTo('stdClass'))
->will($this->returnValue(true));
$driver = new TestFileDriver($locator);
$driver->setGlobalBasename('global');
self::assertFalse($driver->isTransient('stdClass'));
self::assertFalse($driver->isTransient('stdGlobal'));
self::assertFalse($driver->isTransient('stdGlobal2'));
}
public function testIsTransient()
{
$locator = $this->newLocator();
$locator->expects($this->once())
->method('fileExists')
->with($this->equalTo('stdClass2'))
->will($this->returnValue(false));
$driver = new TestFileDriver($locator);
self::assertTrue($driver->isTransient('stdClass2'));
}
public function testNonLocatorFallback()
{
$driver = new TestFileDriver(__DIR__ . '/_files', '.yml');
self::assertTrue($driver->isTransient('stdClass2'));
self::assertFalse($driver->isTransient('stdClass'));
}
private function newLocator()
{
$locator = $this->createMock(FileLocator::class);
$locator->expects($this->any())->method('getFileExtension')->will($this->returnValue('.yml'));
$locator->expects($this->any())->method('getPaths')->will($this->returnValue([__DIR__ . '/_files']));
return $locator;
}
}
class TestFileDriver extends FileDriver
{
protected function loadMappingFile($file)
{
if (strpos($file, 'global.yml') !== false) {
return ['stdGlobal' => 'stdGlobal', 'stdGlobal2' => 'stdGlobal2'];
}
return ['stdClass' => 'stdClass'];
}
public function loadMetadataForClass($className, ClassMetadata $metadata)
{
}
}