Completed
Branch feature-unit-tests (9d0273)
by Tim
01:44
created

Module   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 90
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B invokeTest() 0 22 4
A getModule() 0 4 1
A getAvailable() 0 4 1
A initialize() 0 7 1
A getModuleName() 0 11 3
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestData as TestData;
7
use \SimpleSAML\Module\monitor\TestResult as TestResult;
8
9
class Module extends \SimpleSAML\Module\monitor\TestCaseFactory
10
{
11
    /**
12
     * @var array
13
     */
14
    private $parsed;
15
16
    /**
17
     * @var array
18
     */
19
    private $available;
20
21
    /**
22
     * @var string
23
     */
24
    private $module;
25
26
    /**
27
     * @param TestData $testData
28
     *
29
     * @return void
30
     */
31
    protected function initialize($testData)
32
    {
33
        $this->module = $testData->getInputItem('required');
34
        $this->available = $testData->getInputItem('available');
35
        $this->parsed = explode('|', $this->module);
36
37
        parent::initialize($testData);
38
    }
39
40
    /**
41
     * @return void
42
     */
43
    public function invokeTest()
44
    {
45
        $testResult = new TestResult($this->getCategory(), $this->getModuleName());
46
47
        $state = State::ERROR;
48
        $available = $this->getAvailable();
49
50
        foreach ($this->parsed as $module) {
51
            if (in_array($module, $available)) {
52
                $state = State::OK;
53
                break 1;
54
            }
55
        }
56
57
        if ($state == State::OK) {
58
            $testResult->setMessage('Module loaded');
59
        } else {
60
            $testResult->setMessage('Module not loaded');
61
        }
62
63
        $testResult->setState($state);
64
        $this->setTestResult($testResult);
65
    }
66
67
    /**
68
     * @return array|null
69
     */
70
    private function getAvailable()
71
    {
72
        assert(is_array($this->available));
73
        return $this->available;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    private function getModule()
80
    {
81
        assert(is_string($this->module));
82
        return $this->module;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getModuleName()
89
    {
90
        $available = $this->available;
91
92
        foreach ($this->parsed as $module) {
93
            if (in_array($module, $available)) {
94
                return $module;
95
            }
96
        }
97
98
        return $this->getModule();
99
    }
100
}
101