Completed
Branch master (1f9106)
by Nils
02:44
created

XUnitReporter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 13
Bugs 3 Features 3
Metric Value
wmc 10
c 13
b 3
f 3
lcom 1
cbo 6
dl 0
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setResponseRetriever() 0 4 1
A processResult() 0 4 1
A init() 0 10 2
B finish() 0 40 6
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\Scanner\Result;
13
14
/**
15
 * Class XUnitReporter.
16
 */
17
class XUnitReporter implements Reporter
18
{
19
    private $filename = null;
20
21
    /**
22
     * @var Result[]
23
     */
24
    private $results = array();
25
26
    private $output = null;
27
28
    private $config;
29
30
    /**
31
     * @var Retriever
32
     */
33
    protected $retriever;
34
35
    public function setResponseRetriever(Retriever $retriever)
36
    {
37
        $this->retriever = $retriever;
38
    }
39
40
    public function init($filename, Configuration $_configuration, OutputInterface $_output)
41
    {
42
        $this->filename = $filename;
43
        $this->config = $_configuration;
44
        $this->output = $_output;
45
46
        if (!is_dir(dirname($this->filename))) {
47
            mkdir(dirname($this->filename));
48
        }
49
    }
50
51
    public function processResult(Result $result)
52
    {
53
        $this->results[] = $result;
54
    }
55
56
    /**
57
     *
58
     */
59
    public function finish()
60
    {
61
        $failures = 0;
62
63
        if ($this->retriever instanceof CrawlingRetriever) {
64
            $startPage = (string) $this->retriever->getStartPage();
65
        } else {
66
            $startPage = '';
67
        }
68
69
        $xUnitReport = new XUnitReport($startPage);
70
71
        foreach ($this->results as $result) {
72
            $testCase = new TestCase(
73
                $result->getUrl(),
74
                $result->getUrl(),
75
                $result->getDuration()
76
            );
77
78
            if ($result->isFailure()) {
79
                ++$failures;
80
81
                foreach ($result->getMessages() as $ruleName => $message) {
82
                    $testCase->addFailure(new Failure($ruleName, $message));
83
84
                    if ($this->retriever instanceof CrawlingRetriever) {
85
                        $stackTrace = $result->getUrl() . ' coming from ' . (string) $this->retriever->getComingFrom($result->getUrl()) . PHP_EOL;
86
                        $stackTrace .= '    - ' . $message . " [rule: $ruleName]";
87
                        $testCase->setSystemOut($stackTrace);
88
                    }
89
                }
90
            }
91
92
            $xUnitReport->addTestCase($testCase);
93
        }
94
95
        file_put_contents($this->filename, $xUnitReport->toXml());
96
97
        $this->output->writeln('    <info>Writing XUnit Output to file:</info> ' . $this->filename);
98
    }
99
}
100