Completed
Branch feature-unit-tests (9d0273)
by Tim
02:07
created

TestResult::getState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor;
4
5
final class TestResult
6
{
7
    /**
8
     * @var int     The state reflecting the result
9
     */
10
    private $state;
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
        $this->setOutput(array());
41
        $this->setState(State::NOSTATE);
42
    }
43
44
    /**
45
     * param bool $includeOutput
46
     *
47
     * @return array
48
     */
49
    public function arrayizeTestResult($includeOutput = false)
50
    {
51
        $output = [
52
            $this->getState(),
53
            $this->getCategory(),
54
            $this->getSubject(),
55
            $this->getMessage()
56
        ];
57
        if ($includeOutput === true) {
58
           $output[] =  $this->getOutput();
59
        }
60
        return $output;
61
    }
62
63
    /**
64
     * @param string $subject
65
     *
66
     * @return void
67
     */
68
    public function setSubject($subject)
69
    {
70
        assert(is_string($subject));
71
        $this->subject = $subject;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getSubject()
78
    {
79
        assert(is_string($this->subject));
80
        return $this->subject;
81
    }
82
83
    /**
84
     * @param string $category
85
     *
86
     * @return void
87
     */
88
    public function setCategory($category)
89
    {
90
        assert(is_string($category));
91
        $this->category = $category;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getCategory()
98
    {
99
        assert(is_string($this->category));
100
        return $this->category;
101
    }
102
103
    /**
104
     * @param string $message
105
     *
106
     * @return void
107
     */
108
    public function setMessage($message)
109
    {
110
        assert(is_string($message));
111
        $this->message = $message;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getMessage()
118
    {
119
        assert(is_string($this->message));
120
        return $this->message;
121
    }
122
123
    /**
124
     * @param array $value
125
     *
126
     * @return void
127
     */
128
    public function setOutput($value)
129
    {
130
        assert(is_array($value));
131
        $this->output = $value;
132
    }
133
134
    /**
135
     * @param mixed $value
136
     * @param string|null $index
137
     *
138
     * @return void
139
     */
140
    public function addOutput($value, $index = null)
141
    {
142
        if ($index === null) {
143
            $this->output = array_merge($this->output, $value);
144
        } else {
145
            $this->output[$index] = $value;
146
        }
147
    }
148
149
    /**
150
     * @param string|null $key
151
     *
152
     * @return mixed
153
     */
154
    public function getOutput($key = null)
155
    {
156
        assert(is_array($this->output));
157
        return is_null($key) ? $this->output : (isSet($this->output[$key]) ? $this->output[$key] : null);
158
    }
159
    
160
    /**
161
     * @param int $state
162
     *
163
     * @return void
164
     */
165
    public function setState($state = State::NOSTATE)
166
    {
167
        assert(is_int($state));
168
        $this->state = $state;
169
    }
170
171
    /**
172
     * @return int
173
     */
174
    public function getState()
175
    {
176
        assert(is_int($this->state));
177
        return $this->state;
178
    }
179
}
180