Passed
Push — master ( 70a077...53c2bb )
by Tim
02:59
created

TestSuiteFactory::addTestResult()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace SimpleSAML\Module\monitor;
4
5
abstract class TestSuiteFactory extends TestCaseFactory
6
{
7
    /**
8
     * @var array
9
     */
10
    private $testResults = array();
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|null $testData
28
     *
29
     * @return void
30
     */
31
    protected function initialize($testData = null)
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 State
58
     */
59
    public function calculateState()
60
    {
61
        $testResults = $this->getTestResults();
62
        $state = State::NOSTATE;
63
64
        if (!empty($testResults)) {
65
            $overall = array();
66
            foreach ($testResults as $testResult) {
67
                $overall[] = $testResult->getState();
68
            }
69
            $state = min($overall);
70
        }
71
        return $state;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $state also could return the type integer which is incompatible with the documented return type SimpleSAML\Module\monitor\State.
Loading history...
72
    }
73
74
    /**
75
     * @return TestResult
76
     */
77
    public function getTestResult()
78
    {
79
        $result = parent::getTestResult();
80
        if ($result === null) {
81
            $result = new TestResult($this->getCategory(), $this->getSubject());
82
            $result->setState($this->calculateState());
83
            $this->setTestResult($result);
84
        }
85
        return $result;
86
    }
87
88
    /**
89
     * @return void
90
     */
91
    public function invokeTestSuite()
92
    {
93
        $this->invokeTest();
94
    }
95
96
    abstract public function invokeTest();
97
}
98