1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Extensions\SmokeReporter\Reporter; |
4
|
|
|
|
5
|
|
|
use Koalamon\Client\Reporter\Event; |
6
|
|
|
use Koalamon\Client\Reporter\Reporter as KoalaReporter; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use whm\Smoke\Config\Configuration; |
9
|
|
|
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever; |
10
|
|
|
use whm\Smoke\Scanner\Result; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class XUnitReporter. |
14
|
|
|
*/ |
15
|
|
|
class KoalamonReporter implements Reporter |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Result[] |
19
|
|
|
*/ |
20
|
|
|
private $results; |
21
|
|
|
|
22
|
|
|
private $koaloMon = 'http://www.koalamon.com/app_dev.php/webhook/'; |
|
|
|
|
23
|
|
|
|
24
|
|
|
private $config; |
25
|
|
|
private $system; |
26
|
|
|
private $collect; |
27
|
|
|
private $identifier; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var KoalaReporter |
31
|
|
|
*/ |
32
|
|
|
private $reporter; |
33
|
|
|
|
34
|
|
|
/* |
35
|
|
|
* @var Retriever |
36
|
|
|
*/ |
37
|
|
|
private $retriever; |
38
|
|
|
|
39
|
|
|
private $output; |
40
|
|
|
|
41
|
|
|
const STATUS_SUCCESS = 'success'; |
42
|
|
|
const STATUS_FAILURE = 'failure'; |
43
|
|
|
|
44
|
|
|
public function init($apiKey, $system = '', $identifier = '', $collect = true, Configuration $_configuration, OutputInterface $_output) |
45
|
|
|
{ |
46
|
|
|
$httpClient = new \GuzzleHttp\Client(); |
47
|
|
|
$this->reporter = new KoalaReporter('', $apiKey, $httpClient); |
48
|
|
|
|
49
|
|
|
$this->config = $_configuration; |
50
|
|
|
|
51
|
|
|
$this->system = $system; |
52
|
|
|
$this->collect = $collect; |
53
|
|
|
$this->identifier = $identifier; |
54
|
|
|
|
55
|
|
|
$this->output = $_output; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setResponseRetriever(Retriever $retriever) |
59
|
|
|
{ |
60
|
|
|
$this->retriever = $retriever; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Rule []; |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
private function getRuleKeys() |
69
|
|
|
{ |
70
|
|
|
$keys = array(); |
71
|
|
|
foreach ($this->config->getRules() as $key => $rule) { |
72
|
|
|
$keys[] = $key; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $keys; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function processResult(Result $result) |
79
|
|
|
{ |
80
|
|
|
$this->results[] = $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function finish() |
84
|
|
|
{ |
85
|
|
|
$this->output->writeln("Sending results to www.koalamon.com ... \n"); |
86
|
|
|
|
87
|
|
|
if ($this->collect) { |
88
|
|
|
$this->sendCollected(); |
89
|
|
|
} else { |
90
|
|
|
$this->sendSingle(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function sendSingle() |
95
|
|
|
{ |
96
|
|
|
$rules = $this->getRuleKeys(); |
97
|
|
|
foreach ($this->results as $result) { |
98
|
|
|
$failedTests = array(); |
99
|
|
|
if ($result->isFailure()) { |
100
|
|
|
foreach ($result->getMessages() as $ruleLKey => $message) { |
101
|
|
|
$identifier = 'smoke_' . $ruleLKey . '_' . $result->getUrl(); |
102
|
|
|
|
103
|
|
View Code Duplication |
if ($this->system === '') { |
|
|
|
|
104
|
|
|
$system = str_replace('http://', '', $result->getUrl()); |
105
|
|
|
} else { |
106
|
|
|
$system = $this->system; |
107
|
|
|
} |
108
|
|
|
$this->send($identifier, $system, 'smoke', $message, self::STATUS_FAILURE, (string) $result->getUrl()); |
109
|
|
|
$failedTests[] = $ruleLKey; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
foreach ($rules as $rule) { |
113
|
|
|
if (!in_array($rule, $failedTests, true)) { |
114
|
|
|
$identifier = 'smoke_' . $rule . '_' . $result->getUrl(); |
115
|
|
|
|
116
|
|
View Code Duplication |
if ($this->system === '') { |
|
|
|
|
117
|
|
|
$system = str_replace('http://', '', $result->getUrl()); |
118
|
|
|
} else { |
119
|
|
|
$system = $this->system; |
120
|
|
|
} |
121
|
|
|
$this->send($identifier, $system, 'smoke_' . $rule, '', self::STATUS_SUCCESS, (string) $result->getUrl()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function sendCollected() |
128
|
|
|
{ |
129
|
|
|
$failureMessages = array(); |
130
|
|
|
$counter = array(); |
131
|
|
|
|
132
|
|
|
foreach ($this->getRuleKeys() as $rule) { |
133
|
|
|
$failureMessages[$rule] = ''; |
134
|
|
|
$counter[$rule] = 0; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($this->results as $result) { |
138
|
|
|
if ($result->isFailure()) { |
139
|
|
|
foreach ($result->getMessages() as $ruleLKey => $message) { |
140
|
|
|
if ($failureMessages[$ruleLKey] === '') { |
141
|
|
|
$failureMessages[$ruleLKey] = ' The smoke test for ' . $this->system . ' failed (Rule: ' . $ruleLKey . ').<ul>'; |
142
|
|
|
} |
143
|
|
|
++$counter[$ruleLKey]; |
144
|
|
|
$failureMessages[$ruleLKey] .= '<li>' . $message . '(url: ' . $result->getUrl() . ', coming from: ' . $this->retriever->getComingFrom($result->getUrl()) . ')</li>'; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
foreach ($failureMessages as $key => $failureMessage) { |
150
|
|
|
if ($failureMessage !== '') { |
151
|
|
|
$this->send($this->identifier . '_' . $key, $this->system, 'smoke', $failureMessage . '</ul>', self::STATUS_FAILURE, '', $counter[$key]); |
152
|
|
|
} else { |
153
|
|
|
$this->send($this->identifier . '_' . $key, $this->system, 'smoke', '', self::STATUS_SUCCESS, '', 0); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function send($identifier, $system, $tool, $message, $status, $url = '', $value = 0) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$event = new Event($identifier, $system, $status, $tool, $message, $value); |
161
|
|
|
$this->reporter->sendEvent($event); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.