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