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

JSONLogStub   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogs() 0 4 1
A getCleanOutputFileContent() 0 11 2
A cleanLog() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\Stub\PHPUnitJSONLogOutput;
6
7
/**
8
 * Class JSONLogStub
9
 * @package Tests\Stub\PHPUnitJSONLogOutput
10
 */
11
class JSONLogStub
12
{
13
    const TWO_ERRORS_TWO_FAILURES = '2Errors2Failures';
14
    const ALL_GREEN = 'AllGreen';
15
    const FATAL_ERROR = 'FatalError';
16
    const SEGFAULT = 'SegFault';
17
    const ONE_ERROR = 'SingleError';
18
    const ONE_INCOMPLETE = 'SingleIncomplete';
19
    const ONE_RISKY = 'SingleRisky';
20
    const ONE_SKIP = 'SingleSkip';
21
    const ONE_WARNING = 'SingleWarning';
22
    const UNKNOWN = 'Unknown';
23
    const PARSE_ERROR = 'ParseError';
24
25
    /**
26
     * @param string $filename
27
     * @return string
28
     * @throws \Exception
29
     */
30
    public static function getLogs(string $filename): string
31
    {
32
        return json_decode(self::getCleanOutputFileContent($filename));
33
    }
34
35
    /**
36
     * @param string $filename
37
     * @return string
38
     * @throws \Exception
39
     */
40
    public static function getCleanOutputFileContent(string $filename): string
41
    {
42
        $fullFilename = __DIR__ . DIRECTORY_SEPARATOR . $filename . '.json';
43
        if (! file_exists($fullFilename)) {
44
            throw new \Exception('Unknown file stub: ' . $filename);
45
        }
46
        /** @var string $rawLog */
47
        $rawLog = file_get_contents($fullFilename);
48
49
        return self::cleanLog($rawLog);
50
    }
51
52
    /**
53
     * @param string $jsonString The dirty output
54
     * @return string            The normalized log, as an array of JSON objects
55
     */
56
    private static function cleanLog($jsonString)
57
    {
58
        $splitted = preg_replace('/\}\{/', '},{', $jsonString);
59
60
        return '[' . $splitted . ']';
61
    }
62
}
63