SymfonyExtractorTest::testExtract()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 8
nop 0
dl 0
loc 32
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Incenteev\TranslationCheckerBundle\Tests\Translator\Extractor;
4
5
use Incenteev\TranslationCheckerBundle\Translator\Extractor\SymfonyExtractor;
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Prophecy\PhpUnit\ProphecyTrait;
9
use Symfony\Component\Translation\MessageCatalogue;
10
11
class SymfonyExtractorTest extends TestCase
12
{
13
    use ProphecyTrait;
14
15
    private string $workingDir;
16
17
    public function testExtract()
18
    {
19
        $symfonyExtractor = $this->prophesize('Symfony\Component\Translation\Extractor\ExtractorInterface');
20
21
        $existingPaths = array(
22
            $this->workingDir.'/foo',
23
            $this->workingDir.'/bar',
24
        );
25
26
        foreach ($existingPaths as $dir) {
27
            mkdir($dir);
28
        }
29
        touch($this->workingDir.'/file');
30
31
        $nonDirPaths = array(
32
            $this->workingDir.'/missing',
33
            $this->workingDir.'/file',
34
        );
35
36
        $paths = array_merge($existingPaths, $nonDirPaths);
37
38
        $catalogue = new MessageCatalogue('en');
39
40
        $extractor = new SymfonyExtractor($symfonyExtractor->reveal(), $paths);
41
42
        $extractor->extract($catalogue);
43
44
        foreach ($existingPaths as $dir) {
45
            $symfonyExtractor->extract(Argument::exact($dir), Argument::exact($catalogue))->shouldHaveBeenCalled();
46
        }
47
        foreach ($nonDirPaths as $path) {
48
            $symfonyExtractor->extract(Argument::exact($path), Argument::exact($catalogue))->shouldNotHaveBeenCalled();
49
        }
50
    }
51
52
    protected function setup(): void
53
    {
54
        parent::setup();
55
56
        $this->workingDir = sys_get_temp_dir().'/translation_checker';
57
58
        if (is_dir($this->workingDir)) {
59
            $this->clean();
60
        } else {
61
            mkdir($this->workingDir);
62
        }
63
    }
64
65
    protected function tearDown(): void
66
    {
67
        parent::tearDown();
68
        $this->clean();
69
        rmdir($this->workingDir);
70
    }
71
72
    private function clean(): void
73
    {
74
        foreach (glob($this->workingDir.'/*') ?: [] as $file) {
75
            if (is_dir($file)) {
76
                @rmdir($file);
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

76
                /** @scrutinizer ignore-unhandled */ @rmdir($file);

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...
77
            } else {
78
                @unlink($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). 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

78
                /** @scrutinizer ignore-unhandled */ @unlink($file);

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...
79
            }
80
        }
81
    }
82
}
83