Completed
Push — output_parsers_refactor ( 755930...b5c5df )
by Alessandro
07:17
created

JSONLogStub::getLogs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Paraunit\Tests\Stub\PHPUnitOutput\JSONLogs;
4
5
/**
6
 * Class JSONLogStub
7
 * @package Paraunit\Tests\Stub\PHPUnitOutput\JSONLogs
8
 */
9
class JSONLogStub
10
{
11
    const TWO_ERRORS_TWO_FAILURES = '2Errors2Failures';
12
    const ALL_GREEN = 'AllGreen';
13
    const FATAL_ERROR = 'FatalError';
14
    const SEGFAULT = 'SegFault';
15
    const ONE_ERROR = 'SingleError';
16
    const ONE_INCOMPLETE = 'SingleIncomplete';
17
    const ONE_RISKY = 'SingleRisky';
18
    const ONE_SKIP = 'SingleSkip';
19
    const ONE_WARNING = 'SingleWarning';
20
21
    /**
22
     * @param $filename
23
     * @return string
24
     * @throws \Exception
25
     */
26
    public static function getLogs($filename)
27
    {
28
        return json_decode(self::getCleanOutputFileContent($filename));
29
    }
30
31
    /**
32
     * @param $filename
33
     * @return string
34
     * @throws \Exception
35
     */
36
    public static function getCleanOutputFileContent($filename)
37
    {
38
        $fullFilename =  __DIR__ . DIRECTORY_SEPARATOR . $filename . '.json';
39
        if ( ! file_exists($fullFilename)) {
40
            throw new \Exception('Unknown file stub: ' . $filename);
41
        }
42
43
        return self::cleanLog(file_get_contents($fullFilename));
44
    }
45
46
    /**
47
     * @param string $jsonString The dirty output
48
     * @return string            The normalized log, as an array of JSON objects
49
     */
50
    private static function cleanLog($jsonString)
51
    {
52
        $splitted = preg_replace('/\}\{/', '},{', $jsonString);
53
54
        return '[' . $splitted . ']';
55
    }
56
}
57