Completed
Push — master ( 33a55e...ca53a0 )
by Nils
07:47
created

XUnitReporter::processResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeReporter\Reporter;
4
5
use phmLabs\XUnitReport\Elements\Failure;
6
use phmLabs\XUnitReport\Elements\TestCase;
7
use phmLabs\XUnitReport\XUnitReport;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use whm\Smoke\Config\Configuration;
10
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\CrawlingRetriever;
11
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever;
12
use whm\Smoke\Rules\CheckResult;
13
use whm\Smoke\Scanner\Result;
14
15
/**
16
 * Class XUnitReporter.
17
 */
18
class XUnitReporter implements Reporter
19
{
20
    private $filename = null;
21
22
    /**
23
     * @var Result[]
24
     */
25
    private $results = array();
26
27
    private $output = null;
28
29
    private $config;
30
31
    /**
32
     * @var Retriever
33
     */
34
    protected $retriever;
35
36
    public function setResponseRetriever(Retriever $retriever)
37
    {
38
        $this->retriever = $retriever;
39
    }
40
41
    public function init($filename, Configuration $_configuration, OutputInterface $_output)
42
    {
43
        $this->filename = $filename;
44
        $this->config = $_configuration;
45
        $this->output = $_output;
46
47
        if (!is_dir(dirname($this->filename))) {
48
            mkdir(dirname($this->filename));
49
        }
50
    }
51
52
    public function processResult(Result $result)
53
    {
54
        $this->results[] = $result;
55
    }
56
57
    public function finish()
58
    {
59
        $failures = 0;
60
61
        if ($this->retriever instanceof CrawlingRetriever) {
62
            $startPage = (string)$this->retriever->getStartPage();
63
        } else {
64
            $startPage = '';
65
        }
66
67
        $xUnitReport = new XUnitReport($startPage);
68
69
        foreach ($this->results as $result) {
70
            $testCase = new TestCase(
71
                $result->getUrl(),
72
                $result->getUrl(),
73
                $result->getDuration()
74
            );
75
76
            if ($result->isFailure()) {
77
                ++$failures;
78
79
                foreach ($result->getMessages() as $ruleName => $message) {
80
                    $testCase->addFailure(new Failure($ruleName, $message));
81
82
                    if ($this->retriever instanceof CrawlingRetriever) {
83
                        $stackTrace = $result->getUrl() . ' coming from ' . (string)$this->retriever->getComingFrom($result->getUrl()) . PHP_EOL;
84
                        $stackTrace .= '    - ' . $message . " [rule: $ruleName]";
85
                        $testCase->setSystemOut($stackTrace);
86
                    }
87
                }
88
            }
89
90
            $xUnitReport->addTestCase($testCase);
91
        }
92
93
        file_put_contents($this->filename, $xUnitReport->toXml());
94
95
        $this->output->writeln('    <info>Writing XUnit Output to file:</info> ' . $this->filename);
96
    }
97
98
    public function processResults($results)
99
    {
100
        // TODO: Implement processResults() method.
101
    }
102
}
103