1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Extensions\SmokeReporter\Reporter; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
6
|
|
|
use whm\Smoke\Config\Configuration; |
7
|
|
|
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever; |
8
|
|
|
use whm\Smoke\Scanner\Result; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class XUnitReporter. |
12
|
|
|
*/ |
13
|
|
|
class KoalamonReporter implements Reporter |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Result[] |
17
|
|
|
*/ |
18
|
|
|
private $results; |
19
|
|
|
|
20
|
|
|
private $koaloMon = 'http://www.koalamon.com/app_dev.php/webhook/'; |
21
|
|
|
private $apiKey; |
22
|
|
|
private $config; |
23
|
|
|
private $system; |
24
|
|
|
private $collect; |
25
|
|
|
private $identifier; |
26
|
|
|
|
27
|
|
|
/* |
28
|
|
|
* @var Retriever |
29
|
|
|
*/ |
30
|
|
|
private $retriever; |
31
|
|
|
|
32
|
|
|
private $output; |
33
|
|
|
|
34
|
|
|
const STATUS_SUCCESS = 'success'; |
35
|
|
|
const STATUS_FAILURE = 'failure'; |
36
|
|
|
|
37
|
|
|
public function init($apiKey, $system = '', $identifier = '', $collect = true, Configuration $_configuration, OutputInterface $_output) |
38
|
|
|
{ |
39
|
|
|
$this->config = $_configuration; |
40
|
|
|
$this->apiKey = $apiKey; |
41
|
|
|
$this->system = $system; |
42
|
|
|
$this->collect = $collect; |
43
|
|
|
$this->identifier = $identifier; |
44
|
|
|
|
45
|
|
|
$this->output = $_output; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setResponseRetriever(Retriever $retriever) |
49
|
|
|
{ |
50
|
|
|
$this->retriever = $retriever; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Rule []; |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
private function getRuleKeys() |
59
|
|
|
{ |
60
|
|
|
$keys = array(); |
61
|
|
|
foreach ($this->config->getRules() as $key => $rule) { |
62
|
|
|
$keys[] = $key; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $keys; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function processResult(Result $result) |
69
|
|
|
{ |
70
|
|
|
$this->results[] = $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function finish() |
74
|
|
|
{ |
75
|
|
|
$this->output->writeln("Sending results to www.koalamon.com ... \n"); |
76
|
|
|
|
77
|
|
|
if ($this->collect) { |
78
|
|
|
$this->sendCollected(); |
79
|
|
|
} else { |
80
|
|
|
$this->sendSingle(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function sendSingle() |
85
|
|
|
{ |
86
|
|
|
$rules = $this->getRuleKeys(); |
87
|
|
|
foreach ($this->results as $result) { |
88
|
|
|
$failedTests = array(); |
89
|
|
|
if ($result->isFailure()) { |
90
|
|
|
foreach ($result->getMessages() as $ruleLKey => $message) { |
91
|
|
|
$identifier = 'smoke_' . $ruleLKey . '_' . $result->getUrl(); |
92
|
|
|
|
93
|
|
View Code Duplication |
if ($this->system === '') { |
|
|
|
|
94
|
|
|
$system = str_replace('http://', '', $result->getUrl()); |
95
|
|
|
} else { |
96
|
|
|
$system = $this->system; |
97
|
|
|
} |
98
|
|
|
$this->send($identifier, $system, 'smoke', $message, self::STATUS_FAILURE, (string) $result->getUrl()); |
99
|
|
|
$failedTests[] = $ruleLKey; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
foreach ($rules as $rule) { |
103
|
|
|
if (!in_array($rule, $failedTests, true)) { |
104
|
|
|
$identifier = 'smoke_' . $rule . '_' . $result->getUrl(); |
105
|
|
|
|
106
|
|
View Code Duplication |
if ($this->system === '') { |
|
|
|
|
107
|
|
|
$system = str_replace('http://', '', $result->getUrl()); |
108
|
|
|
} else { |
109
|
|
|
$system = $this->system; |
110
|
|
|
} |
111
|
|
|
$this->send($identifier, $system, 'smoke_' . $rule, '', self::STATUS_SUCCESS, (string) $result->getUrl()); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function sendCollected() |
118
|
|
|
{ |
119
|
|
|
$failureMessages = array(); |
120
|
|
|
|
121
|
|
|
foreach ($this->getRuleKeys() as $rule) { |
122
|
|
|
$failureMessages[$rule] = ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
foreach ($this->results as $result) { |
126
|
|
|
if ($result->isFailure()) { |
127
|
|
|
foreach ($result->getMessages() as $ruleLKey => $message) { |
128
|
|
|
if ($failureMessages[$ruleLKey] === '') { |
129
|
|
|
$failureMessages[$ruleLKey] = ' The smoke test for ' . $this->system . ' failed (Rule: ' . $ruleLKey . ').<ul>'; |
130
|
|
|
} |
131
|
|
|
$failureMessages[$ruleLKey] .= '<li>' . $message . '(url: ' . $result->getUrl() . ', coming from: ' . $this->retriever->getComingFrom($result->getUrl()) . ')</li>'; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
foreach ($failureMessages as $key => $failureMessage) { |
137
|
|
|
if ($failureMessage !== '') { |
138
|
|
|
$this->send($this->identifier . '_' . $key, $this->system, 'smoke', $failureMessage . '</ul>', self::STATUS_FAILURE, ''); |
139
|
|
|
} else { |
140
|
|
|
$this->send($this->identifier . '_' . $key, $this->system, 'smoke', '', self::STATUS_SUCCESS, ''); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function send($identifier, $system, $tool, $message, $status, $url = '') |
146
|
|
|
{ |
147
|
|
|
$curl = curl_init(); |
148
|
|
|
$responseBody = array( |
149
|
|
|
'system' => $system, |
150
|
|
|
'status' => $status, |
151
|
|
|
'message' => $message, |
152
|
|
|
'identifier' => $identifier, |
153
|
|
|
'type' => $tool, |
154
|
|
|
'url' => $url, |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$koalamonUrl = $this->koaloMon . '?api_key=' . $this->apiKey; |
158
|
|
|
curl_setopt_array($curl, array( |
159
|
|
|
CURLOPT_URL => $koalamonUrl, |
160
|
|
|
CURLOPT_RETURNTRANSFER => true, |
161
|
|
|
CURLOPT_ENCODING => '', |
162
|
|
|
CURLOPT_MAXREDIRS => 10, |
163
|
|
|
CURLOPT_TIMEOUT => 30, |
164
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
165
|
|
|
CURLOPT_CUSTOMREQUEST => 'POST', |
166
|
|
|
CURLOPT_POSTFIELDS => json_encode($responseBody), |
167
|
|
|
)); |
168
|
|
|
|
169
|
|
|
$response = curl_exec($curl); |
170
|
|
|
|
171
|
|
|
$err = curl_error($curl); |
172
|
|
|
curl_close($curl); |
173
|
|
|
|
174
|
|
|
return $err; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.