Passed
Push — master ( 2ee37f...93876b )
by Tim
02:23
created

TestSuiteFactory::getArrayizeTestResults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor;
4
5
abstract class TestSuiteFactory extends TestCaseFactory
6
{
7
    /**
8
     * @var array   An associative array of name => TestResult pairs
9
     */
10
    private $testResults = [];
11
12
    /**
13
     * @param TestConfiguration|null $configuration
14
     * @param TestData|null $testData
15
     */
16
    public function __construct($configuration = null, $testData = null)
17
    {
18
        assert($configuration instanceof TestConfiguration || is_null($configuration));
19
        assert($testData instanceof TestData || is_null($testData));
20
21
        $this->setConfiguration($configuration);
22
        $this->initialize($testData);
23
        $this->invokeTestSuite();
24
    }
25
26
    /**
27
     * @param TestData $testData
28
     *
29
     * @return void
30
     */
31
    protected function initialize($testData)
32
    {
33
        $this->setTestData($testData);
34
    }
35
36
    /**
37
     * @param TestResult $testResult
38
     *
39
     * @return void
40
     */
41
    protected function addTestResult($testResult)
42
    {
43
        assert($testResult instanceof TestResult);
44
        $this->testResults[] = $testResult;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getTestResults()
51
    {
52
        assert(is_array($this->testResults));
53
        return $this->testResults;
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function getArrayizeTestResults()
60
    {
61
        assert(is_array($this->testResults));
62
        $result = array();
63
        foreach ($this->testResults as $testResult) {
64
            $result[] = $testResult->arrayizeTestResult();
65
        }
66
        return $result;
67
    }
68
69
    /**
70
     * @return int
71
     */
72
    public function calculateState()
73
    {
74
        $testResults = $this->getTestResults();
75
76
        if (!empty($testResults)) {
77
            $state = State::OK;
78
            foreach ($testResults as $testResult) {
79
                $testState = $testResult->getState();
80
                if ($testState < $state) {
81
                    $state = $testState;
82
                }
83
            }
84
        } else {
85
            $state = State::NOSTATE;
86
        }
87
        return $state;
88
    }
89
90
    /**
91
     * @return void
92
     */
93
    public function invokeTestSuite()
94
    {
95
        $this->invokeTest();
96
    }
97
}
98