testLocateFromASingleDirectory()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 4
nop 0
1
<?php
2
3
namespace ComposerRequireCheckerTest\FileLocator;
4
5
use ArrayObject;
6
use ComposerRequireChecker\FileLocator\LocateAllFilesByExtension;
7
use org\bovigo\vfs\vfsStream;
8
use org\bovigo\vfs\vfsStreamDirectory;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * @covers \ComposerRequireChecker\FileLocator\LocateAllFilesByExtension
13
 */
14
final class LocateAllFilesByExtensionTest extends TestCase
15
{
16
    /** @var LocateAllFilesByExtension */
17
    private $locator;
18
    /** @var vfsStreamDirectory */
19
    private $root;
20
21
    protected function setUp(): void
22
    {
23
        parent::setUp();
24
25
        $this->locator = new LocateAllFilesByExtension();
26
        $this->root = vfsStream::setup();
27
    }
28
29
    public function testLocateFromNoDirectories(): void
30
    {
31
        $files = $this->locate([], '.php', null);
32
33
        $this->assertCount(0, $files);
34
    }
35
36
    public function testLocateFromASingleDirectory(): void
37
    {
38
        $dir = vfsStream::newDirectory('MyNamespaceA')->at($this->root);
39
        $files = [];
40
        for ($i = 0; $i < 3; $i++) {
41
            $files[] = vfsStream::newFile("MyClass$i.php")->at($dir);
42
        }
43
44
        $foundFiles = $this->locate([$dir->url()], '.php', null);
45
46
        $this->assertCount(count($files), $foundFiles);
47
        foreach ($files as $file) {
48
            $this->assertContains($file->url(), str_replace('\\', '/', $foundFiles));
49
        }
50
    }
51
52
    public function testLocateWithFilenameBlackList(): void
53
    {
54
        $dir = vfsStream::newDirectory('MyNamespaceA')->at($this->root);
55
        for ($i = 0; $i < 10; $i++) {
56
            vfsStream::newFile("MyClass$i.php")->at($dir);
57
        }
58
59
        $foundFiles = $this->locate([$dir->url()], '.php', ['MyClass6']);
60
61
        $this->assertCount(9, $foundFiles);
62
        $this->assertNotContains(vfsStream::url('MyClass6.php'), $foundFiles);
63
    }
64
65
    public function testLocateWithDirectoryBlackList(): void
66
    {
67
        $dir = vfsStream::newDirectory('MyNamespaceA')->at($this->root);
68
        for ($i = 0; $i < 10; $i++) {
69
            vfsStream::newFile("Directory$i/MyClass.php")->at($dir);
70
        }
71
72
        $foundFiles = $this->locate([$dir->url()], '.php', ['Directory5/']);
73
74
        $this->assertCount(9, $foundFiles);
75
        $this->assertNotContains(vfsStream::url('Directory5/MyClass.php'), $foundFiles);
76
    }
77
78
    /**
79
     * @param array $blacklist
80
     * @param array $expectedFiles
81
     *
82
     * @dataProvider provideBlacklists
83
     */
84
    public function testLocateWithBlackList(array $blacklist, array $expectedFiles): void
85
    {
86
        $this->root = vfsStream::create([
87
            'MyNamespaceA' => [
88
                'MyClass.php' => '<?php class MyClass {}',
89
                'Foo' => [
90
                    'FooClass.php' => '<?php class FooCalls {}',
91
                    'Bar' => [
92
                        'BarClass.php' => '<?php class BarClass {}',
93
                    ]
94
                ],
95
                'Bar' => [
96
                    'AnotherBarClass.php' => '<?php class AnotherBarClass {}',
97
                ]
98
            ]
99
        ]);
100
101
        $foundFiles = $this->locate([$this->root->url()], '.php', $blacklist);
102
103
        $this->assertCount(count($expectedFiles), $foundFiles);
104
        foreach ($expectedFiles as $file) {
105
            $this->assertContains($this->root->getChild($file)->url(), $foundFiles);
106
        }
107
        $this->assertContains($this->root->getChild('MyNamespaceA/Foo/FooClass.php')->url(), $foundFiles);
108
    }
109
110
    public function provideBlacklists(): array {
111
        return [
112
            'No blacklist' => [
113
                [],
114
                [
115
                    'MyNamespaceA/MyClass.php',
116
                    'MyNamespaceA/Foo/FooClass.php',
117
                    'MyNamespaceA/Foo/Bar/BarClass.php',
118
                    'MyNamespaceA/Bar/AnotherBarClass.php',
119
                ]
120
            ],
121
            '* wildcard' => [
122
                ['Another*.php'],
123
                [
124
                    'MyNamespaceA/MyClass.php',
125
                    'MyNamespaceA/Foo/FooClass.php',
126
                    'MyNamespaceA/Foo/Bar/BarClass.php',
127
                ]
128
            ],
129
            '** wildcard' => [
130
                ['**/Bar'],
131
                [
132
                    'MyNamespaceA/MyClass.php',
133
                    'MyNamespaceA/Foo/FooClass.php',
134
                ]
135
            ],
136
            'Combined patterns' => [
137
                ['My*.php', 'Bar/'],
138
                [
139
                    'MyNamespaceA/Foo/FooClass.php',
140
                ]
141
            ],
142
        ];
143
    }
144
145
    /**
146
     * @param string[] $directories
147
     * @param string $fileExtension
148
     * @param array|null $blacklist
149
     * @return array|\string[]
150
     */
151
    private function locate(array $directories, string $fileExtension, ?array $blacklist): array
152
    {
153
        $files = [];
154
        foreach (($this->locator)(new ArrayObject($directories), $fileExtension, $blacklist) as $file) {
155
            $files[] = str_replace('\\', '/', $file);
156
        }
157
        return $files;
158
    }
159
}
160