Completed
Push — output_parsers_refactor ( b5c5df...ea70d9 )
by Alessandro
10:48
created

FinalPrinterTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 8
dl 0
loc 97
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testOnEngineEndPrintsTheRightCountSummary() 0 32 3
B testOnEngineEndPrintsInTheRightOrder() 0 44 3
A assertOutputOrder() 0 16 2
1
<?php
2
3
namespace Paraunit\Tests\Functional\Printer;
4
5
6
use Paraunit\Lifecycle\EngineEvent;
7
use Paraunit\Parser\JSONLogParser;
8
use Paraunit\Parser\OutputContainerBearerInterface;
9
use Paraunit\Printer\FinalPrinter;
10
use Paraunit\Tests\BaseFunctionalTestCase;
11
use Paraunit\Tests\Stub\ConsoleOutputStub;
12
use Paraunit\Tests\Stub\StubbedParaProcess;
13
14
/**
15
 * Class FinalPrinterTest
16
 * @package Paraunit\Tests\Functional\Printer
17
 */
18
class FinalPrinterTest extends BaseFunctionalTestCase
19
{
20
    public function testOnEngineEndPrintsTheRightCountSummary()
21
    {
22
        $process = new StubbedParaProcess();
23
        $process->addTestResult('.');
24
        $process->addTestResult('.');
25
        $process->addTestResult('.');
26
27
        $output = new ConsoleOutputStub();
28
        $context = array(
29
            'start' => new \DateTime('-1 minute'),
30
            'end' => new \DateTime(),
31
            'process_completed' => array_fill(0, 15, $process),
32
        );
33
        $engineEvent = new EngineEvent($output, $context);
34
35
        /** @var JSONLogParser $logParser */
36
        $logParser = $this->container->get('paraunit.parser.json_log_parser');
37
38
        foreach ($logParser->getParsersForPrinting() as $parser) {
39
            if ($parser instanceof OutputContainerBearerInterface) {
40
                $parser->getOutputContainer()->addToOutputBuffer($process, 'Test');
41
            }
42
        }
43
44
        /** @var FinalPrinter $printer */
45
        $printer = $this->container->get('paraunit.printer.final_printer');
46
47
        $printer->onEngineEnd($engineEvent);
48
49
        $this->assertContains('Execution time -- 00:01:00', $output->getOutput());
50
        $this->assertContains('Executed: 15 test classes, 45 tests', $output->getOutput());
51
    }
52
53
    public function testOnEngineEndPrintsInTheRightOrder()
54
    {
55
        $output = new ConsoleOutputStub();
56
        $process = new StubbedParaProcess();
57
        $context = array(
58
            'start' => new \DateTime('-1 minute'),
59
            'end' => new \DateTime(),
60
            'process_completed' => array($process),
61
        );
62
        $engineEvent = new EngineEvent($output, $context);
63
64
        /** @var JSONLogParser $logParser */
65
        $logParser = $this->container->get('paraunit.parser.json_log_parser');
66
67
        $logParser->getAbnormalTerminatedOutputContainer()->addToOutputBuffer($process, 'Test');
68
        foreach ($logParser->getParsersForPrinting() as $parser) {
69
            if ($parser instanceof OutputContainerBearerInterface) {
70
                $parser->getOutputContainer()->addToOutputBuffer($process, 'Test');
71
            }
72
        }
73
74
        /** @var FinalPrinter $printer */
75
        $printer = $this->container->get('paraunit.printer.final_printer');
76
77
        $printer->onEngineEnd($engineEvent);
78
79
        $this->assertNotEmpty($output->getOutput());
80
        $this->assertOutputOrder($output, array(
81
            'Abnormal Terminations (fatal Errors, Segfaults) output:',
82
            'Errors output:',
83
            'Failures output:',
84
            'Warnings output:',
85
            'Risky Outcome output:',
86
            'Skipped Outcome output:',
87
            'Incomplete Outcome output:',
88
            'files with ABNORMAL TERMINATIONS (FATAL ERRORS, SEGFAULTS)',
89
            'files with ERRORS',
90
            'files with FAILURES',
91
            'files with WARNING',
92
            'files with RISKY',
93
            'files with SKIP',
94
            'files with INCOMPLETE'
95
        ));
96
    }
97
98
    private function assertOutputOrder(ConsoleOutputStub $output, array $strings)
99
    {
100
        $previousPosition = 0;
101
        $previousString = '<beginning of output>';
102
        foreach ($strings as $string) {
103
            $position = strpos($output->getOutput(), $string);
104
            $this->assertNotSame(false, $position, 'String not found: ' . $string . $output->getOutput());
105
            $this->assertGreaterThan(
106
                $previousPosition,
107
                $position,
108
                'Failed asserting that "' . $string . '" comes before "' . $previousString . '"'
109
            );
110
            $previousString = $string;
111
            $previousPosition = $position;
112
        }
113
    }
114
}
115