1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\Persistence\Mapping; |
4
|
|
|
|
5
|
|
|
use Doctrine\Persistence\Mapping\Driver\SymfonyFileLocator; |
6
|
|
|
use Doctrine\Persistence\Mapping\MappingException; |
7
|
|
|
use Doctrine\Tests\DoctrineTestCase; |
8
|
|
|
use const DIRECTORY_SEPARATOR; |
9
|
|
|
use function realpath; |
10
|
|
|
use function sort; |
11
|
|
|
|
12
|
|
|
class SymfonyFileLocatorTest extends DoctrineTestCase |
13
|
|
|
{ |
14
|
|
|
public function testGetPaths() |
15
|
|
|
{ |
16
|
|
|
$path = __DIR__ . '/_files'; |
17
|
|
|
$prefix = 'Foo'; |
18
|
|
|
|
19
|
|
|
$locator = new SymfonyFileLocator([$path => $prefix]); |
20
|
|
|
self::assertSame([$path], $locator->getPaths()); |
21
|
|
|
|
22
|
|
|
$locator = new SymfonyFileLocator([$path => $prefix]); |
23
|
|
|
self::assertSame([$path], $locator->getPaths()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testGetPrefixes() |
27
|
|
|
{ |
28
|
|
|
$path = __DIR__ . '/_files'; |
29
|
|
|
$prefix = 'Foo'; |
30
|
|
|
|
31
|
|
|
$locator = new SymfonyFileLocator([$path => $prefix]); |
32
|
|
|
self::assertSame([$path => $prefix], $locator->getNamespacePrefixes()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testGetFileExtension() |
36
|
|
|
{ |
37
|
|
|
$locator = new SymfonyFileLocator([], '.yml'); |
38
|
|
|
self::assertSame('.yml', $locator->getFileExtension()); |
39
|
|
|
$locator->setFileExtension('.xml'); |
40
|
|
|
self::assertSame('.xml', $locator->getFileExtension()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testFileExists() |
44
|
|
|
{ |
45
|
|
|
$path = __DIR__ . '/_files'; |
46
|
|
|
$prefix = 'Foo'; |
47
|
|
|
|
48
|
|
|
$locator = new SymfonyFileLocator([$path => $prefix], '.yml'); |
49
|
|
|
|
50
|
|
|
self::assertTrue($locator->fileExists('Foo\stdClass')); |
51
|
|
|
self::assertTrue($locator->fileExists('Foo\global')); |
52
|
|
|
self::assertFalse($locator->fileExists('Foo\stdClass2')); |
53
|
|
|
self::assertFalse($locator->fileExists('Foo\global2')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetAllClassNames() |
57
|
|
|
{ |
58
|
|
|
$path = __DIR__ . '/_files'; |
59
|
|
|
$prefix = 'Foo'; |
60
|
|
|
|
61
|
|
|
$locator = new SymfonyFileLocator([$path => $prefix], '.yml'); |
62
|
|
|
$allClasses = $locator->getAllClassNames(null); |
63
|
|
|
$globalClasses = $locator->getAllClassNames('global'); |
64
|
|
|
|
65
|
|
|
$expectedAllClasses = ['Foo\\Bar\\subDirClass', 'Foo\\global', 'Foo\\stdClass']; |
66
|
|
|
$expectedGlobalClasses = ['Foo\\Bar\\subDirClass', 'Foo\\stdClass']; |
67
|
|
|
|
68
|
|
|
sort($allClasses); |
69
|
|
|
sort($globalClasses); |
70
|
|
|
sort($expectedAllClasses); |
71
|
|
|
sort($expectedGlobalClasses); |
72
|
|
|
|
73
|
|
|
self::assertSame($expectedAllClasses, $allClasses); |
74
|
|
|
self::assertSame($expectedGlobalClasses, $globalClasses); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @expectedException \InvalidArgumentException |
79
|
|
|
* @expectedExceptionMessage Namespace separator should not be empty |
80
|
|
|
*/ |
81
|
|
|
public function testInvalidCustomNamespaceSeparator() |
82
|
|
|
{ |
83
|
|
|
$path = __DIR__ . '/_files'; |
84
|
|
|
$prefix = 'Foo'; |
85
|
|
|
|
86
|
|
|
new SymfonyFileLocator([$path => $prefix], '.yml', null); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function customNamespaceSeparatorProvider() |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
'directory separator' => [DIRECTORY_SEPARATOR, '/_custom_ns/dir'], |
93
|
|
|
'default dot separator' => ['.', '/_custom_ns/dot'], |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $separator Directory separator to test against |
99
|
|
|
* @param string $dir Path to load mapping data from |
100
|
|
|
* |
101
|
|
|
* @throws MappingException |
102
|
|
|
* |
103
|
|
|
* @dataProvider customNamespaceSeparatorProvider |
104
|
|
|
*/ |
105
|
|
|
public function testGetClassNamesWithCustomNsSeparator($separator, $dir) |
106
|
|
|
{ |
107
|
|
|
$path = __DIR__ . $dir; |
108
|
|
|
$prefix = 'Foo'; |
109
|
|
|
|
110
|
|
|
$locator = new SymfonyFileLocator([$path => $prefix], '.yml', $separator); |
111
|
|
|
$classes = $locator->getAllClassNames(null); |
112
|
|
|
sort($classes); |
113
|
|
|
|
114
|
|
|
self::assertSame(['Foo\\stdClass', 'Foo\\sub\\subClass', 'Foo\\sub\\subsub\\subSubClass'], $classes); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function customNamespaceLookupQueryProvider() |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
'directory separator' => [ |
121
|
|
|
DIRECTORY_SEPARATOR, |
122
|
|
|
'/_custom_ns/dir', |
123
|
|
|
[ |
124
|
|
|
'stdClass.yml' => 'Foo\\stdClass', |
125
|
|
|
'sub/subClass.yml' => 'Foo\\sub\\subClass', |
126
|
|
|
'sub/subsub/subSubClass.yml' => 'Foo\\sub\\subsub\\subSubClass', |
127
|
|
|
], |
128
|
|
|
], |
129
|
|
|
'default dot separator' => [ |
130
|
|
|
'.', |
131
|
|
|
'/_custom_ns/dot', |
132
|
|
|
[ |
133
|
|
|
'stdClass.yml' => 'Foo\\stdClass', |
134
|
|
|
'sub.subClass.yml' => 'Foo\\sub\\subClass', |
135
|
|
|
'sub.subsub.subSubClass.yml' => 'Foo\\sub\\subsub\\subSubClass', |
136
|
|
|
], |
137
|
|
|
], |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $separator Directory separator to test against |
143
|
|
|
* @param string $dir Path to load mapping data from |
144
|
|
|
* @param string[] $files Files to lookup classnames |
145
|
|
|
* |
146
|
|
|
* @throws MappingException |
147
|
|
|
* |
148
|
|
|
* @dataProvider customNamespaceLookupQueryProvider |
149
|
|
|
*/ |
150
|
|
|
public function testFindMappingFileWithCustomNsSeparator($separator, $dir, $files) |
151
|
|
|
{ |
152
|
|
|
$path = __DIR__ . $dir; |
153
|
|
|
$prefix = 'Foo'; |
154
|
|
|
|
155
|
|
|
$locator = new SymfonyFileLocator([$path => $prefix], '.yml', $separator); |
156
|
|
|
|
157
|
|
|
foreach ($files as $filePath => $className) { |
158
|
|
|
self::assertSame(realpath($path . '/' . $filePath), realpath($locator->findMappingFile($className))); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testFindMappingFile() |
163
|
|
|
{ |
164
|
|
|
$path = __DIR__ . '/_files'; |
165
|
|
|
$prefix = 'Foo'; |
166
|
|
|
|
167
|
|
|
$locator = new SymfonyFileLocator([$path => $prefix], '.yml'); |
168
|
|
|
|
169
|
|
|
self::assertSame(__DIR__ . '/_files/stdClass.yml', $locator->findMappingFile('Foo\\stdClass')); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testFindMappingFileNotFound() |
173
|
|
|
{ |
174
|
|
|
$path = __DIR__ . '/_files'; |
175
|
|
|
$prefix = 'Foo'; |
176
|
|
|
|
177
|
|
|
$locator = new SymfonyFileLocator([$path => $prefix], '.yml'); |
178
|
|
|
|
179
|
|
|
$this->expectException(MappingException::class); |
180
|
|
|
$this->expectExceptionMessage("No mapping file found named 'stdClass2.yml' for class 'Foo\stdClass2'."); |
181
|
|
|
$locator->findMappingFile('Foo\\stdClass2'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testFindMappingFileLeastSpecificNamespaceFirst() |
185
|
|
|
{ |
186
|
|
|
// Low -> High |
187
|
|
|
$prefixes = []; |
188
|
|
|
$prefixes[__DIR__ . '/_match_ns'] = 'Foo'; |
189
|
|
|
$prefixes[__DIR__ . '/_match_ns/Bar'] = 'Foo\\Bar'; |
190
|
|
|
|
191
|
|
|
$locator = new SymfonyFileLocator($prefixes, '.yml'); |
192
|
|
|
|
193
|
|
|
self::assertSame( |
194
|
|
|
__DIR__ . '/_match_ns/Bar/barEntity.yml', |
195
|
|
|
$locator->findMappingFile("Foo\\Bar\\barEntity") |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testFindMappingFileMostSpecificNamespaceFirst() |
200
|
|
|
{ |
201
|
|
|
$prefixes = []; |
202
|
|
|
$prefixes[__DIR__ . '/_match_ns/Bar'] = 'Foo\\Bar'; |
203
|
|
|
$prefixes[__DIR__ . '/_match_ns'] = 'Foo'; |
204
|
|
|
|
205
|
|
|
$locator = new SymfonyFileLocator($prefixes, '.yml'); |
206
|
|
|
|
207
|
|
|
self::assertSame( |
208
|
|
|
__DIR__ . '/_match_ns/Bar/barEntity.yml', |
209
|
|
|
$locator->findMappingFile("Foo\\Bar\\barEntity") |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|