Passed
Push — main ( 12657a...b476ea )
by Chema
03:58
created

AllAppModulesFinderTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Unit\Console\Domain\AllAppModules;
6
7
use ArrayIterator;
8
use FilesystemIterator;
9
use Gacela\Console\Domain\AllAppModules\AllAppModulesFinder;
10
use Gacela\Console\Domain\AllAppModules\AppModuleCreator;
11
use Gacela\Framework\ClassResolver\Config\ConfigResolver;
12
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
13
use Gacela\Framework\ClassResolver\Provider\ProviderResolver;
14
use Gacela\Framework\Gacela;
15
use IteratorIterator;
16
use PHPUnit\Framework\TestCase;
17
use RecursiveDirectoryIterator;
18
use RecursiveIteratorIterator;
19
use SplFileInfo;
20
21
use function dirname;
22
23
final class AllAppModulesFinderTest extends TestCase
24
{
25
    protected function setUp(): void
26
    {
27
        Gacela::bootstrap(dirname(__DIR__, 5));
28
    }
29
30
    public function test_skips_entries_marked_as_directory(): void
31
    {
32
        $fileInfo = $this->createMock(SplFileInfo::class);
33
        $fileInfo->method('isFile')->willReturn(false);
34
        $fileInfo->method('getExtension')->willReturn('php');
35
        $fileInfo->method('getRealPath')->willReturn($this->module1FacadePath());
36
        $fileInfo->method('getFilename')->willReturn('Module1Facade.php');
37
38
        $finder = new AllAppModulesFinder(
39
            $this->iteratorFor($fileInfo),
40
            $this->createAppModuleCreator(),
41
        );
42
43
        self::assertSame([], $finder->findAllAppModules(''));
44
    }
45
46
    public function test_skips_vendor_directories_only_when_segment_matches(): void
47
    {
48
        $tempDir = $this->createTempModuleDirectory('vendormodule');
49
        $filePath = $tempDir . '/TempFacade.php';
50
        $className = 'TempAllAppModulesVendor\\TempFacade';
51
52
        $this->writeTempFacadeFile($filePath, $className);
53
        require_once $filePath;
54
55
        $fileInfo = $this->createMock(SplFileInfo::class);
56
        $fileInfo->method('isFile')->willReturn(true);
57
        $fileInfo->method('getExtension')->willReturn('php');
58
        $fileInfo->method('getRealPath')->willReturn($filePath);
59
        $fileInfo->method('getFilename')->willReturn('TempFacade.php');
60
61
        $finder = new AllAppModulesFinder(
62
            $this->iteratorFor($fileInfo),
63
            $this->createAppModuleCreator(),
64
        );
65
66
        try {
67
            $modules = $finder->findAllAppModules('');
68
            self::assertCount(1, $modules);
69
            self::assertSame($className, $modules[0]->facadeClass());
70
        } finally {
71
            $this->removeDirectory($tempDir);
72
        }
73
    }
74
75
    private function iteratorFor(SplFileInfo ...$files): IteratorIterator
76
    {
77
        return new IteratorIterator(new ArrayIterator($files));
78
    }
79
80
    private function module1FacadePath(): string
81
    {
82
        return dirname(__DIR__, 4) . '/Integration/Console/AllAppModules/Domain/Module1/Module1Facade.php';
83
    }
84
85
    private function createTempModuleDirectory(string $directoryName): string
86
    {
87
        $tempDir = sys_get_temp_dir() . '/gacela_all_modules_' . uniqid('', true);
88
        $targetDir = $tempDir . '/' . $directoryName;
89
        mkdir($targetDir, 0777, true);
90
91
        return $targetDir;
92
    }
93
94
    private function writeTempFacadeFile(string $filePath, string $className): void
95
    {
96
        $namespace = substr($className, 0, strrpos($className, '\\'));
97
        $classBasename = substr($className, strrpos($className, '\\') + 1);
98
        $template = <<<PHP
99
<?php
100
101
declare(strict_types=1);
102
103
namespace {$namespace};
104
105
use Gacela\\Framework\\AbstractFacade;
106
107
final class {$classBasename} extends AbstractFacade
108
{
109
}
110
PHP;
111
112
        file_put_contents($filePath, $template);
113
    }
114
115
    private function removeDirectory(string $directory): void
116
    {
117
        if (!is_dir($directory)) {
118
            return;
119
        }
120
121
        $files = new RecursiveIteratorIterator(
122
            new RecursiveDirectoryIterator(dirname($directory), FilesystemIterator::SKIP_DOTS),
123
            RecursiveIteratorIterator::CHILD_FIRST,
124
        );
125
126
        foreach ($files as $file) {
127
            if ($file->isDir()) {
128
                rmdir($file->getPathname());
129
            } else {
130
                unlink($file->getPathname());
131
            }
132
        }
133
134
        @rmdir($directory);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

134
        /** @scrutinizer ignore-unhandled */ @rmdir($directory);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
135
        @rmdir(dirname($directory));
136
    }
137
138
    private function createAppModuleCreator(): AppModuleCreator
139
    {
140
        return new AppModuleCreator(
141
            new FactoryResolver(),
142
            new ConfigResolver(),
143
            new ProviderResolver(),
144
        );
145
    }
146
}
147