1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\monitor; |
6
|
|
|
|
7
|
|
|
use function array_merge; |
8
|
|
|
use function is_null; |
9
|
|
|
|
10
|
|
|
final class TestResult |
11
|
|
|
{ |
12
|
|
|
/** @var int The state reflecting the result */ |
13
|
|
|
private int $state = State::NOSTATE; |
14
|
|
|
|
15
|
|
|
/** @var string Test category this test belongs to */ |
16
|
|
|
private string $category; |
17
|
|
|
|
18
|
|
|
/** @var string The subject that was tested */ |
19
|
|
|
private string $subject; |
20
|
|
|
|
21
|
|
|
/** @var string Message describing the result */ |
22
|
|
|
private string $message = ''; |
23
|
|
|
|
24
|
|
|
/** @var array Data to be used by TestSuite or other TestCases */ |
25
|
|
|
private array $output = []; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $category |
30
|
|
|
* @param string $subject |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $category = 'Unknown category', string $subject = 'Unknown subject') |
33
|
|
|
{ |
34
|
|
|
$this->setCategory($category); |
35
|
|
|
$this->setSubject($subject); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param bool $includeOutput |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function arrayizeTestResult(bool $includeOutput = false): array |
45
|
|
|
{ |
46
|
|
|
$output = [ |
47
|
|
|
'state' => $this->getState(), |
48
|
|
|
'category' => $this->getCategory(), |
49
|
|
|
'subject' => $this->getSubject(), |
50
|
|
|
'message' => $this->getMessage() |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
if ($includeOutput === true) { |
54
|
|
|
$output['output'] = $this->getOutput(); |
55
|
|
|
} |
56
|
|
|
return $output; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $subject |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function setSubject(string $subject): void |
66
|
|
|
{ |
67
|
|
|
$this->subject = $subject; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getSubject(): string |
75
|
|
|
{ |
76
|
|
|
return $this->subject; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $category |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function setCategory(string $category): void |
86
|
|
|
{ |
87
|
|
|
$this->category = $category; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getCategory(): string |
95
|
|
|
{ |
96
|
|
|
return $this->category; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $message |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function setMessage(string $message): void |
106
|
|
|
{ |
107
|
|
|
$this->message = $message; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getMessage(): string |
115
|
|
|
{ |
116
|
|
|
return $this->message; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $value |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function setOutput(array $value): void |
126
|
|
|
{ |
127
|
|
|
$this->output = $value; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param mixed $value |
133
|
|
|
* @param string|null $index |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
public function addOutput($value, string $index = null): void |
138
|
|
|
{ |
139
|
|
|
if ($index === null) { |
140
|
|
|
$this->output = array_merge($this->output, $value); |
141
|
|
|
} else { |
142
|
|
|
$this->output[$index] = $value; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string|null $key |
149
|
|
|
* |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
public function getOutput(string $key = null) |
153
|
|
|
{ |
154
|
|
|
return is_null($key) ? $this->output : (isset($this->output[$key]) ? $this->output[$key] : null); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param integer $state |
160
|
|
|
* |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
public function setState(int $state = State::NOSTATE): void |
164
|
|
|
{ |
165
|
|
|
$this->state = $state; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
public function getState(): int |
173
|
|
|
{ |
174
|
|
|
return $this->state; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|