Test Failed
Pull Request — master (#132)
by Alessandro
03:15
created

BaseTestCase   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 4
dl 0
loc 74
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getCoverageStubFilePath() 0 7 1
A getConfigForStubs() 0 4 1
A getConfigForDeprecationListener() 0 4 1
A getStubPath() 0 4 1
A createRandomTmpDir() 0 10 1
A getRandomTempDir() 0 6 1
A tearDown() 0 11 3
A createCodeCoverage() 0 4 1
A getFileContent() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests;
6
7
use Paraunit\Configuration\EnvVariables;
8
use Paraunit\File\Cleaner;
9
use Paraunit\Proxy\Coverage\FakeDriver;
10
use PHPUnit\Framework\TestCase;
11
use SebastianBergmann\CodeCoverage\CodeCoverage;
12
13
/**
14
 * Class BaseTestCase
15
 * @package Tests
16
 */
17
class BaseTestCase extends TestCase
18
{
19
    /** @var string|null */
20
    private $randomTempDir;
21
22
    protected function getCoverageStubFilePath(): string
23
    {
24
        $filename = __DIR__ . '/Stub/CoverageOutput/Coverage4Stub.php';
25
        static::assertFileExists($filename, 'CoverageStub file missing!');
26
27
        return $filename;
28
    }
29
30
    protected function getConfigForStubs(): string
31
    {
32
        return $this->getStubPath() . 'phpunit_for_stubs.xml';
33
    }
34
35
    protected function getConfigForDeprecationListener(): string
36
    {
37
        return $this->getStubPath() . 'phpunit_with_deprecations.xml';
38
    }
39
40
    protected function getStubPath(): string
41
    {
42
        return realpath(__DIR__ . DIRECTORY_SEPARATOR . 'Stub') . DIRECTORY_SEPARATOR;
43
    }
44
45
    protected function createRandomTmpDir()
46
    {
47
        $this->randomTempDir = uniqid(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'paraunit-test-', true);
48
        $this->randomTempDir .= DIRECTORY_SEPARATOR;
49
50
        $this->assertTrue(
51
            putenv(EnvVariables::LOG_DIR . '=' . $this->randomTempDir),
52
            'Failed setting env variable for log dir'
53
        );
54
    }
55
56
    protected function getRandomTempDir(): string
57
    {
58
        $this->assertNotNull($this->randomTempDir, 'Tmp dir not initialized');
59
60
        return $this->randomTempDir;
61
    }
62
63
    protected function tearDown()
64
    {
65
        putenv(EnvVariables::LOG_DIR);
66
        putenv(EnvVariables::PROCESS_UNIQUE_ID);
67
68
        if ($this->randomTempDir && is_dir($this->randomTempDir)) {
69
            Cleaner::cleanUpDir($this->randomTempDir);
70
        }
71
72
        parent::tearDown();
73
    }
74
75
    protected function createCodeCoverage(): CodeCoverage
76
    {
77
        return new CodeCoverage(new FakeDriver());
78
    }
79
80
    protected function getFileContent(string $filePath): string
81
    {
82
        $this->assertFileExists($filePath);
83
        $content = file_get_contents($filePath);
84
        if (! \is_string($content)) {
85
            $this->fail('Unable to retrieve file content from ' . $filePath);
86
        }
87
88
        return $content;
89
    }
90
}
91