Passed
Branch monitor-2.5.x (8b654c)
by Tim
01:31
created

TestSuiteFactory::calculateState()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 2
nop 0
dl 0
loc 18
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Modules\Monitor;
4
5
abstract class TestSuiteFactory extends TestCaseFactory
6
{
7
    /**
8
     * @var TestConfiguration
9
     */
10
    private $configuration;
11
12
    /**
13
     * @var array   An associative array of name => TestResult pairs
14
     */
15
    private $testResults = [];
16
17
    /**
18
     * @param TestConfiguration|null $configuration
19
     * @param TestData|null $testData
20
     */
21
    public function __construct($configuration = null, $testData = null)
22
    {
23
        assert($configuration instanceof TestConfiguration || is_null($configuration));
24
        assert($testData instanceof TestData || is_null($testData));
25
26
        $this->setConfiguration($configuration);
27
        $this->initialize($testData);
28
        $this->invokeTestSuite();
29
    }
30
31
    /**
32
     * @param TestData $testData
33
     *
34
     * @return void
35
     */
36
    protected function initialize($testData)
37
    {
38
        $this->setTestData($testData);
39
    }
40
41
    /**
42
     * @param TestConfiguration|null $configuration
43
     *
44
     * @return void
45
     */
46
    protected function setConfiguration($configuration = null)
47
    {
48
        assert($configuration instanceof TestConfiguration);
49
        if (!is_null($configuration)) {
50
            $this->configuration = $configuration;
51
        }
52
    }
53
54
    /**
55
     * @return TestConfiguration
56
     */
57
    public function getConfiguration()
58
    {
59
        assert($this->configuration instanceof TestConfiguration);
60
        return $this->configuration;
61
    }
62
63
    /**
64
     * @param TestResult $testResult
65
     *
66
     * @return void
67
     */
68
    protected function addTestResult($testResult)
69
    {
70
        assert($testResult instanceof TestResult);
71
        $this->testResults[] = $testResult;
72
    }
73
74
    /**
75
     * @param array $testResults
76
     *
77
     * @return void
78
     */
79
    protected function addTestResults($testResults)
80
    {
81
        assert(is_array($testResults));
82
        $this->testResults = array_merge($this->testResults, $testResults);
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function getTestResults()
89
    {
90
        assert(is_array($this->testResults));
91
        return $this->testResults;
92
    }
93
94
    /**
95
     * param bool $includeOutput
96
     *
97
     * @return array
98
     */
99
    public function getArrayizeTestResults($includeOutput = false)
100
    {
101
        assert(is_array($this->testResults));
102
        $result = [];
103
        foreach ($this->testResults as $testResult) {
104
            $result[] = $testResult->arrayizeTestResult($includeOutput);
105
        }
106
        return $result;
107
    }
108
109
    /**
110
     * @return int
111
     */
112
    public function calculateState()
113
    {
114
        $testResults = $this->getTestResults();
115
116
        if (!empty($testResults)) {
117
            $state = State::OK;
118
            foreach ($testResults as $testResult) {
119
                $testState = $testResult->getState();
120
                if ($testState !== State::NOSTATE && $testState !== State::SKIPPED) {
121
                    if ($testState < $state) {
122
                        $state = $testState;
123
                    }
124
                }
125
            }
126
        } else {
127
            $state = State::NOSTATE;
128
        }
129
        return $state;
130
    }
131
132
    /**
133
     * @return void
134
     */
135
    public function invokeTestSuite()
136
    {
137
        $this->invokeTest();
138
    }
139
}
140