Passed
Push — master ( ec621c...281469 )
by Tim
01:58
created

TestSuiteFactory::addTestResults()   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 1
dl 0
loc 4
rs 10
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
     * @param array $testResults
49
     *
50
     * @return void
51
     */
52
    protected function addTestResults($testResults)
53
    {
54
        assert(is_array($testResults));
55
        $this->testResults = array_merge($this->testResults, $testResults);
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getTestResults()
62
    {
63
        assert(is_array($this->testResults));
64
        return $this->testResults;
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getArrayizeTestResults()
71
    {
72
        assert(is_array($this->testResults));
73
        $result = [];
74
        foreach ($this->testResults as $testResult) {
75
            $result[] = $testResult->arrayizeTestResult();
76
        }
77
        return $result;
78
    }
79
80
    /**
81
     * @return int
82
     */
83
    public function calculateState()
84
    {
85
        $testResults = $this->getTestResults();
86
87
        if (!empty($testResults)) {
88
            $state = State::OK;
89
            foreach ($testResults as $testResult) {
90
                $testState = $testResult->getState();
91
                if ($testState < $state) {
92
                    $state = $testState;
93
                }
94
            }
95
        } else {
96
            $state = State::NOSTATE;
97
        }
98
        return $state;
99
    }
100
101
    /**
102
     * @return void
103
     */
104
    public function invokeTestSuite()
105
    {
106
        $this->invokeTest();
107
    }
108
}
109