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

BaseUnitTestCase   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 103
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogFromStub() 0 18 4
A getWrongCoverageStubFilePath() 0 7 1
A getLogWithTrace() 0 13 4
A mockTestFormat() 0 8 1
A mockTestResult() 0 4 1
A mockPrintableTestResult() 0 14 2
A removeDirectory() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests;
6
7
use Paraunit\TestResult\Interfaces\PrintableTestResultInterface;
8
use Paraunit\TestResult\Interfaces\TestResultInterface;
9
use Paraunit\TestResult\TestResultFormat;
10
use Paraunit\TestResult\TestResultWithSymbolFormat;
11
use PHPUnit\Framework\AssertionFailedError;
12
use Tests\Stub\PHPUnitJSONLogOutput\JSONLogStub;
13
14
/**
15
 * Class BaseUnitTestCase
16
 * @package Paraunit\Tests
17
 */
18
abstract class BaseUnitTestCase extends BaseTestCase
19
{
20
    /**
21
     * @param string $event
22
     * @param string $status
23
     * @param string|null $testOutput
24
     * @return \stdClass
25
     * @throws AssertionFailedError
26
     * @throws \Exception
27
     */
28
    protected function getLogFromStub(string $event = 'test', string $status = 'fail', string $testOutput = null): \stdClass
29
    {
30
        $jsonLogs = JSONLogStub::getCleanOutputFileContent(JSONLogStub::ONE_ERROR);
31
        /** @var \stdClass[] $logs */
32
        $logs = json_decode($jsonLogs);
33
        foreach ($logs as $log) {
34
            if ($log->event === $event) {
35
                if ($testOutput) {
36
                    $log->status = $status;
37
                    $log->message = $testOutput;
38
                }
39
40
                return $log;
41
            }
42
        }
43
44
        $this->fail('Feasible log message not found for test');
45
    }
46
47
    protected function getWrongCoverageStubFilePath(): string
48
    {
49
        $filename = __DIR__ . '/Stub/CoverageOutput/WrongCoverageStub.php';
50
        $this->assertFileExists($filename, 'WrongCoverageStub file missing!');
51
52
        return $filename;
53
    }
54
55
    protected function getLogWithTrace(): \stdClass
56
    {
57
        $jsonLogs = JSONLogStub::getCleanOutputFileContent(JSONLogStub::ONE_ERROR);
58
        $logs = json_decode($jsonLogs);
59
        /** @var \stdClass $log */
60
        foreach ($logs as $log) {
61
            if (property_exists($log, 'trace') && $log->trace !== '') {
62
                return $log;
63
            }
64
        }
65
66
        $this->fail('Feasible log message not found for test');
67
    }
68
69
    protected function mockTestFormat(): TestResultFormat
70
    {
71
        $format = $this->prophesize(TestResultFormat::class);
72
        $format->getTag()
73
            ->willReturn('tag');
74
75
        return $format->reveal();
76
    }
77
78
    protected function mockTestResult(): TestResultInterface
79
    {
80
        return $this->prophesize(TestResultInterface::class)->reveal();
81
    }
82
83
    protected function mockPrintableTestResult($symbol = null): PrintableTestResultInterface
84
    {
85
        if ($symbol === null) {
86
            $format = $this->prophesize(TestResultFormat::class);
87
        } else {
88
            $format = $this->prophesize(TestResultWithSymbolFormat::class);
89
            $format->getTestResultSymbol()->willReturn($symbol);
90
        }
91
92
        $result = $this->prophesize(PrintableTestResultInterface::class);
93
        $result->getTestResultFormat()->willReturn($format->reveal());
94
95
        return $result->reveal();
96
    }
97
98
    protected function removeDirectory(string $path): bool
99
    {
100
        $files = new \RecursiveIteratorIterator(
101
            new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS),
102
            \RecursiveIteratorIterator::CHILD_FIRST
103
        );
104
105
        /** @var \SplFileInfo $file */
106
        foreach ($files as $file) {
107
            $realPath = $file->getRealPath();
108
            if (! $realPath) {
109
                continue;
110
            }
111
            if ($file->isDir()) {
112
                $this->removeDirectory($realPath);
113
            } else {
114
                unlink($realPath);
115
            }
116
        }
117
118
        return rmdir($path);
119
    }
120
}
121