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