Passed
Push — master ( 535739...af4710 )
by Matthias
03:18
created

testFromPsr4WithExcludeFromClassmap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace ComposerRequireCheckerTest\FileLocator;
4
5
use ComposerRequireChecker\FileLocator\LocateComposerPackageSourceFiles;
6
use org\bovigo\vfs\vfsStream;
7
use org\bovigo\vfs\vfsStreamDirectory;
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * @covers \ComposerRequireChecker\FileLocator\LocateComposerPackageSourceFiles
12
 */
13
class LocateComposerPackageSourceFilesTest extends TestCase
14
{
15
    /** @var LocateComposerPackageSourceFiles */
16
    private $locator;
17
    /** @var vfsStreamDirectory */
18
    private $root;
19
20
    protected function setUp()
21
    {
22
        parent::setUp();
23
24
        $this->locator = new LocateComposerPackageSourceFiles();
25
        $this->root = vfsStream::setup();
26
    }
27
28
    public function testFromClassmap()
29
    {
30
        vfsStream::create([
31
            'composer.json' => '{"autoload": {"classmap": ["src/MyClassA.php", "MyClassB.php"]}}',
32
            'src' => [
33
                'MyClassA.php' => '<?php class MyClassA {}',
34
            ],
35
            'MyClassB.php' => '<?php class MyClassB {}',
36
        ]);
37
38
        $files = $this->files($this->root->getChild('composer.json')->url());
39
40
        $this->assertCount(2, $files);
41
        $this->assertContains($this->root->getChild('src/MyClassA.php')->url(), $files);
42
        $this->assertContains($this->root->getChild('MyClassB.php')->url(), $files);
43
    }
44
45
    public function testFromFiles()
46
    {
47
        vfsStream::create([
48
            'composer.json' => '{"autoload": {"files": ["foo.php"]}}',
49
            'foo.php' => '<?php class MyClass {}',
50
        ]);
51
52
        $files = $this->files($this->root->getChild('composer.json')->url());
53
54
        $this->assertCount(1, $files);
55
        $this->assertContains($this->root->getChild('foo.php')->url(), $files);
56
    }
57
58
    public function testFromPsr0()
59
    {
60
        vfsStream::create([
61
            'composer.json' => '{"autoload": {"psr-0": ["src"]}}',
62
            'src' => [
63
                'MyNamespace' => [
64
                    'MyClass.php' => '<?php namespace MyNamespace; class MyClass {}',
65
                ],
66
            ],
67
        ]);
68
69
        $files = $this->files($this->root->getChild('composer.json')->url());
70
71
        $this->assertCount(1, $files);
72
    }
73
74
    public function testFromPsr4()
75
    {
76
        vfsStream::create([
77
            'composer.json' => '{"autoload": {"psr-4": {"MyNamespace\\\\": "src"}}}',
78
            'src' => [
79
                'MyClass.php' => '<?php namespace MyNamespace; class MyClass {}',
80
            ],
81
        ]);
82
83
        $files = $this->files($this->root->getChild('composer.json')->url());
84
85
        $this->assertCount(1, $files);
86
    }
87
88
    public function testFromPsr0WithMultipleDirectories()
89
    {
90
        vfsStream::create([
91
            'composer.json' => '{"autoload": {"psr-0": {"MyNamespace\\\\": ["src", "lib"]}}}',
92
            'src' => ['MyNamespace' => ['MyClassA.php' => '<?php namespace MyNamespace; class MyClassA {}']],
93
            'lib' => ['MyNamespace' => ['MyClassB.php' => '<?php namespace MyNamespace; class MyClassB {}']],
94
        ]);
95
96
        $files = $this->files($this->root->getChild('composer.json')->url());
97
98
        $this->assertCount(2, $files);
99
        $this->assertContains($this->root->getChild('src/MyNamespace/MyClassA.php')->url(), $files);
100
        $this->assertContains($this->root->getChild('lib/MyNamespace/MyClassB.php')->url(), $files);
101
    }
102
103
    public function testFromPsr4WithMultipleDirectories()
104
    {
105
        vfsStream::create([
106
            'composer.json' => '{"autoload": {"psr-4": {"MyNamespace\\\\": ["src", "lib"]}}}',
107
            'src' => ['MyClassA.php' => '<?php namespace MyNamespace; class MyClassA {}'],
108
            'lib' => ['MyClassB.php' => '<?php namespace MyNamespace; class MyClassB {}'],
109
        ]);
110
111
        $files = $this->files($this->root->getChild('composer.json')->url());
112
113
        $this->assertCount(2, $files);
114
        $this->assertContains($this->root->getChild('src/MyClassA.php')->url(), $files);
115
        $this->assertContains($this->root->getChild('lib/MyClassB.php')->url(), $files);
116
    }
117
118
    public function testFromPsr4WithNestedDirectory()
119
    {
120
        vfsStream::create([
121
            'composer.json' => '{"autoload": {"psr-4": {"MyNamespace\\\\": ["src/MyNamespace"]}}}',
122
            'src' => [
123
                'MyNamespace' => ['MyClassA.php' => '<?php namespace MyNamespace; class MyClassA {}']
124
            ],
125
        ]);
126
127
        $files = $this->files($this->root->getChild('composer.json')->url());
128
129
        $this->assertCount(1, $files);
130
        $this->assertContains($this->root->getChild('src/MyNamespace/MyClassA.php')->url(), $files);
131
    }
132
133
    public function testFromPsr4WithNestedDirectoryAlternativeDirectorySeparator()
134
    {
135
        vfsStream::create([
136
            'composer.json' => '{"autoload": {"psr-4": {"MyNamespace\\\\": ["src\\\\MyNamespace"]}}}',
137
            'src' => [
138
                'MyNamespace' => ['MyClassA.php' => '<?php namespace MyNamespace; class MyClassA {}']
139
            ],
140
        ]);
141
142
        $files = $this->files($this->root->getChild('composer.json')->url());
143
144
        $this->assertCount(1, $files);
145
        $this->assertContains($this->root->getChild('src/MyNamespace/MyClassA.php')->url(), $files);
146
    }
147
148
    public function testFromPsr4WithExcludeFromClassmap()
149
    {
150
        vfsStream::create([
151
            'composer.json' => '{"autoload": {"psr-4": {"MyNamespace\\\\": "./"}, "exclude-from-classmap": ["/tests/"]}}',
152
            'MyClass.php' => '<?php namespace MyNamespace; class MyClass {}',
153
            'tests' => [
154
                'MyClassTest.php' => '<?php namespace MyNamespace; class MyClassTest {}',
155
            ]
156
        ]);
157
158
        $files = $this->files($this->root->getChild('composer.json')->url());
159
160
        $this->assertCount(1, $files);
161
    }
162
163
    /**
164
     * @return string[]
165
     */
166
    private function files(string $composerJson): array
167
    {
168
        $files = [];
169
        $filesGenerator = ($this->locator)($composerJson);
170
        foreach ($filesGenerator as $file) {
171
            $files[] = $file;
172
        }
173
        return $files;
174
    }
175
}
176