Cancelled
Branch master (244451)
by Tim
10:31
created

ModuleSet::setAvailable()   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\TestSuite\Modules;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestCaseFactory as TestCaseFactory;
7
use \SimpleSAML\Module\monitor\TestData as TestData;
8
use \SimpleSAML\Module\monitor\TestResult as TestResult;
9
10
final class ModuleSet extends \SimpleSAML\Module\monitor\TestSuiteFactory
11
{
12
    /**
13
     * @var array
14
     */
15
    private $required;
16
17
    /**
18
     * @var array
19
     */
20
    private $available;
21
 
22
    /**
23
     * @var array
24
     */
25
    private $dependencies;
26
27
    /**
28
     * @var string
29
     */
30
    private $type;
31
32
    /**
33
     * @var TestCaseFactory
34
     */
35
    private $testCase;
36
37
    /**
38
     * @param TestData $testData
39
     *
40
     * @return void
41
     */
42
    protected function initialize($testData)
43
    {
44
        $this->setRequired($testData->getInput('required'));
45
        $this->setAvailable($testData->getInput('available'));
46
        $this->setDependencies($testData->getInput('dependencies'));
47
        $this->setType($testData->getInput('type'));
48
        $this->setTestCase($testData->getInput('testClass'));
49
        $this->setCategory($this->type.' modules');
0 ignored issues
show
Bug introduced by
Are you sure $this->type of type null|mixed|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        $this->setCategory(/** @scrutinizer ignore-type */ $this->type.' modules');
Loading history...
50
    }
51
52
    /**
53
     * @param array
54
     */
55
    private function setRequired($required)
56
    {
57
        assert(is_array($required));
58
        $this->required = $required;
59
    }
60
61
62
    /**
63
     * @param array
64
     */
65
    private function setAvailable($available)
66
    {
67
        assert(is_array($available));
68
        $this->available = $available;
69
    }
70
71
72
    /**
73
     * @param array
74
     */
75
    private function setDependencies($dependencies)
76
    {
77
        assert(is_array($dependencies));
78
        $this->dependencies = $dependencies;
79
    }
80
81
82
    /**
83
     * @param string
84
     */
85
    private function setType($type)
86
    {
87
        assert(is_string($type));
88
        $this->type = $type;
89
    }
90
91
92
    /**
93
     * @param TestCase
94
     */
95
    private function setTestCase($testCase)
96
    {
97
        assert($testCase instanceof TestCaseFactory);
98
        $this->testCase = $testCase;
99
    }
100
101
    
102
    /**
103
     * @return void
104
     */
105
    public function invokeTest()
106
    {
107
        if (empty($this->available)) {
108
            $state = State::SKIPPED;
109
        } else {
110
            foreach ($this->required as $module) {
111
                $testData = new TestData([
112
                    'required' => $module,
113
                    'available' => $this->available
114
                ]);
115
116
                $moduleTest = new $this->testCase($this, $testData);
117
                $moduleTestResult = $moduleTest->getTestResult();
118
                if ($moduleTestResult->getState() !== State::OK) {
119
                    $missing = array();
120
                    $dependencies = $this->dependencies;
121
                    while ($dependency = array_search($this->required, $dependencies)) {
122
                        if (\SimpleSAML\Module::isModuleEnabled($dependency)) {
123
                            $missing[] = $dependency;
124
                        }
125
                        unset($dependencies[$dependency]);
126
                    }
127
                    if (!empty($missing)) {
128
                        $moduleTestResult->setSubject($moduleTest->getModuleName());
129
                        $moduleTestResult->setMessage('Module not loaded; dependency for ' . implode(', ', $missing));
130
                    }
131
                    $this->addTestResult($moduleTestResult);
132
                }
133
            }
134
             $state = $this->calculateState();
135
        }
136
137
        $testResult = new TestResult($this->type, implode(', ', $this->required));       
138
        if ($state === State::OK) {
139
            $testResult->setMessage('All required modules are loaded');
140
        } elseif ($state === State::SKIPPED) {
141
            $testResult->setMessage('Unable to verify installed modules');
142
        } else {
143
            $testResult->setMessage('Not all required modules are loaded');
144
        }
145
        $this->setTestResult($testResult);
146
    }
147
}
148