|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Reporter; |
|
13
|
|
|
|
|
14
|
|
|
use ZendDiagnostics\Result\FailureInterface; |
|
15
|
|
|
use ZendDiagnostics\Result\ResultInterface; |
|
16
|
|
|
use ZendDiagnostics\Result\SkipInterface; |
|
17
|
|
|
use ZendDiagnostics\Result\SuccessInterface; |
|
18
|
|
|
use ZendDiagnostics\Result\WarningInterface; |
|
19
|
|
|
use ZendDiagnostics\Runner\Reporter\ReporterInterface; |
|
20
|
|
|
use ZendDiagnostics\Check\CheckInterface; |
|
21
|
|
|
use ZendDiagnostics\Result\Collection as ResultsCollection; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
|
|
|
|
|
24
|
|
|
* @author Vladimir Turnaev <[email protected]> |
|
25
|
|
|
*/ |
|
|
|
|
|
|
26
|
|
|
abstract class ReporterAbstract implements ReporterInterface |
|
27
|
|
|
{ |
|
28
|
|
|
public const STATUS_CODE_SUCCESS = 0; |
|
29
|
|
|
public const STATUS_CODE_WARNING = 100; |
|
30
|
|
|
public const STATUS_CODE_SKIP = 200; |
|
31
|
|
|
public const STATUS_CODE_UNKNOWN = 300; |
|
32
|
|
|
public const STATUS_CODE_FAILURE = 1000; |
|
33
|
|
|
|
|
34
|
|
|
public const STATUS_NAME_SUCCESS = 'SUCCESS'; |
|
35
|
|
|
public const STATUS_NAME_WARNING = 'WARNING'; |
|
36
|
|
|
public const STATUS_NAME_SKIP = 'SKIP'; |
|
37
|
|
|
public const STATUS_NAME_UNKNOWN = 'UNKNOWN'; |
|
38
|
|
|
public const STATUS_NAME_FAILURE = 'FAILURE'; |
|
39
|
|
|
|
|
40
|
|
|
public static $STATUS_MAP = [ |
|
41
|
|
|
self::STATUS_CODE_SUCCESS => self::STATUS_NAME_SUCCESS, |
|
42
|
|
|
self::STATUS_CODE_WARNING => self::STATUS_NAME_WARNING, |
|
43
|
|
|
self::STATUS_CODE_SKIP => self::STATUS_NAME_SKIP, |
|
44
|
|
|
self::STATUS_CODE_UNKNOWN => self::STATUS_NAME_UNKNOWN, |
|
45
|
|
|
self::STATUS_CODE_FAILURE => self::STATUS_NAME_FAILURE, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Total number of Checks. |
|
50
|
|
|
* |
|
51
|
|
|
* @var int |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $totalCount = 0; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Has the Runner operation been aborted (stopped) ? |
|
57
|
|
|
* |
|
58
|
|
|
* @var bool |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $stopped = false; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
|
|
|
|
|
63
|
|
|
* @var ResultsCollection |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $results; |
|
66
|
|
|
|
|
67
|
1 |
|
public static function getStatusNameByCode(int $statusCode): string |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
1 |
|
return isset(self::$STATUS_MAP[$statusCode]) ? self::$STATUS_MAP[$statusCode] : self::STATUS_NAME_UNKNOWN; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* This method is called right after Reporter starts running, via Runner::run(). |
|
74
|
|
|
* |
|
75
|
|
|
* @param ArrayObject $checks A collection of Checks that will be performed |
|
|
|
|
|
|
76
|
|
|
* @param array $runnerConfig Complete Runner configuration, obtained via Runner::getConfig() |
|
77
|
|
|
*/ |
|
|
|
|
|
|
78
|
1 |
|
public function onStart(\ArrayObject $checks, $runnerConfig) |
|
79
|
|
|
{ |
|
80
|
1 |
|
$this->stopped = false; |
|
81
|
|
|
|
|
82
|
1 |
|
$this->totalCount = \count($checks); |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* This method is called before each individual Check is performed. If this |
|
87
|
|
|
* method returns false, the Check will not be performed (will be skipped). |
|
88
|
|
|
* |
|
89
|
|
|
* @param CheckInterface $check check instance that is about to be performed |
|
90
|
|
|
* @param string|null $checkAlias The alias for the check that is about to be performed |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool|void Return false to prevent check from happening |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function onBeforeRun(CheckInterface $check, $checkAlias = null) |
|
95
|
|
|
{ |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* This method is called every time a Check has been performed. If this method |
|
100
|
|
|
* returns false, the Runner will not perform any additional checks and stop |
|
101
|
|
|
* its run. |
|
102
|
|
|
* |
|
103
|
|
|
* @param CheckInterface $check A Check instance that has just finished running |
|
104
|
|
|
* @param ResultInterface $result Result for that particular check instance |
|
105
|
|
|
* @param string|null $checkAlias The alias for the check that has just finished |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool|void Return false to prevent from running additional Checks |
|
108
|
|
|
*/ |
|
109
|
|
|
public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null) |
|
110
|
|
|
{ |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* This method is called when Runner has been aborted and could not finish the |
|
115
|
|
|
* whole run(). |
|
116
|
|
|
* |
|
117
|
|
|
* @param ResultsCollection $results collection of Results for performed Checks |
|
118
|
|
|
*/ |
|
|
|
|
|
|
119
|
|
|
public function onStop(ResultsCollection $results) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->stopped = true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* This method is called when Runner has finished its run. |
|
126
|
|
|
* |
|
127
|
|
|
* @param ResultsCollection $results collection of Results for performed Checks |
|
128
|
|
|
*/ |
|
|
|
|
|
|
129
|
8 |
|
public function onFinish(ResultsCollection $results) |
|
130
|
|
|
{ |
|
131
|
8 |
|
$this->results = $results; |
|
132
|
|
|
|
|
133
|
|
|
// Display information that the test has been aborted. |
|
134
|
8 |
|
if ($this->stopped) { |
|
135
|
|
|
$this->onStopped(); |
|
136
|
|
|
} |
|
137
|
8 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
|
|
|
|
|
140
|
|
|
* @return int |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function getSuccessCount(): ?int |
|
143
|
|
|
{ |
|
144
|
1 |
|
return $this->results ? $this->results->getSuccessCount() : null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
|
|
|
|
|
148
|
|
|
* @return int |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function getWarningCount(): ?int |
|
151
|
|
|
{ |
|
152
|
1 |
|
return $this->results ? $this->results->getWarningCount() : null; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
|
|
|
|
|
156
|
|
|
* @return int |
|
157
|
|
|
*/ |
|
158
|
1 |
|
public function getFailureCount(): ?int |
|
159
|
|
|
{ |
|
160
|
1 |
|
return $this->results ? $this->results->getFailureCount() : null; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
|
|
|
|
|
164
|
|
|
* @return int |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getSkipCount(): ?int |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->results ? $this->results->getSkipCount() : null; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
|
|
|
|
|
172
|
|
|
* @return int |
|
173
|
|
|
*/ |
|
174
|
1 |
|
public function getUnknownCount(): ?int |
|
175
|
|
|
{ |
|
176
|
1 |
|
return $this->results ? $this->results->getUnknownCount() : null; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
public function getTotalCount(): int |
|
|
|
|
|
|
180
|
|
|
{ |
|
181
|
1 |
|
return $this->totalCount; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function isStopped(): bool |
|
|
|
|
|
|
185
|
|
|
{ |
|
186
|
|
|
return $this->stopped; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function getResults(): ResultsCollection |
|
|
|
|
|
|
190
|
|
|
{ |
|
191
|
|
|
return $this->results; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
protected function onStopped() |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
|
|
|
|
|
199
|
|
|
* @return array [name, code] |
|
200
|
|
|
*/ |
|
201
|
1 |
|
protected function getStatusByResul(ResultInterface $result) |
|
202
|
|
|
{ |
|
203
|
|
|
switch (true) { |
|
204
|
1 |
|
case $result instanceof SuccessInterface: |
|
|
|
|
|
|
205
|
1 |
|
$name = self::STATUS_NAME_SUCCESS; |
|
206
|
1 |
|
$code = self::STATUS_CODE_SUCCESS; |
|
207
|
1 |
|
break; |
|
208
|
|
|
|
|
209
|
|
|
case $result instanceof WarningInterface: |
|
|
|
|
|
|
210
|
|
|
$name = self::STATUS_NAME_WARNING; |
|
211
|
|
|
$code = self::STATUS_CODE_WARNING; |
|
212
|
|
|
break; |
|
213
|
|
|
|
|
214
|
|
|
case $result instanceof SkipInterface: |
|
|
|
|
|
|
215
|
|
|
$name = self::STATUS_NAME_SKIP; |
|
216
|
|
|
$code = self::STATUS_CODE_SKIP; |
|
217
|
|
|
break; |
|
218
|
|
|
|
|
219
|
|
|
case $result instanceof FailureInterface: |
|
|
|
|
|
|
220
|
|
|
$name = self::STATUS_NAME_FAILURE; |
|
221
|
|
|
$code = self::STATUS_CODE_FAILURE; |
|
222
|
|
|
break; |
|
223
|
|
|
|
|
224
|
|
|
default: |
|
|
|
|
|
|
225
|
|
|
$name = self::STATUS_NAME_UNKNOWN; |
|
226
|
|
|
$code = self::STATUS_CODE_UNKNOWN; |
|
227
|
|
|
break; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
1 |
|
return [$name, $code]; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|