Failed Conditions
Pull Request — master (#1)
by Jonathan
03:48
created

SymfonyFileLocatorTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 197
rs 10
c 0
b 0
f 0

12 Methods

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