Passed
Push — master ( c37f5d...ac0c17 )
by Alexander
07:19
created

RequirementsCheckerTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 94
c 1
b 0
f 0
dl 0
loc 188
rs 10
wmc 10
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
5
require_once __DIR__ . '/../src/RequirementsChecker.php';
6
7
class RequirementsCheckerTest extends TestCase
8
{
9
    public function testCheck()
10
    {
11
        $requirementsChecker = new RequirementsChecker();
12
13
        $requirements = [
14
            'requirementPass' => [
15
                'name' => 'Requirement 1',
16
                'mandatory' => true,
17
                'condition' => true,
18
                'by' => 'Requirement 1',
19
                'memo' => 'Requirement 1',
20
            ],
21
            'requirementError' => [
22
                'name' => 'Requirement 2',
23
                'mandatory' => true,
24
                'condition' => false,
25
                'by' => 'Requirement 2',
26
                'memo' => 'Requirement 2',
27
            ],
28
            'requirementWarning' => [
29
                'name' => 'Requirement 3',
30
                'mandatory' => false,
31
                'condition' => false,
32
                'by' => 'Requirement 3',
33
                'memo' => 'Requirement 3',
34
            ],
35
        ];
36
37
        $checkResult = $requirementsChecker->check($requirements)->getResult();
38
        $summary = $checkResult['summary'];
39
40
        $this->assertCount($summary['total'], $requirements, 'Wrong summary total!');
41
        $this->assertEquals(1, $summary['errors'], 'Wrong summary errors!');
42
        $this->assertEquals(1, $summary['warnings'], 'Wrong summary warnings!');
43
44
        $checkedRequirements = $checkResult['requirements'];
45
        $requirementsKeys = array_flip(array_keys($requirements));
46
47
        $this->assertFalse($checkedRequirements[$requirementsKeys['requirementPass']]['error'], 'Passed requirement has an error!');
48
        $this->assertFalse($checkedRequirements[$requirementsKeys['requirementPass']]['warning'], 'Passed requirement has a warning!');
49
50
        $this->assertTrue($checkedRequirements[$requirementsKeys['requirementError']]['error'], 'Error requirement has no error!');
51
52
        $this->assertFalse($checkedRequirements[$requirementsKeys['requirementWarning']]['error'], 'Error requirement has an error!');
53
        $this->assertTrue($checkedRequirements[$requirementsKeys['requirementWarning']]['warning'], 'Error requirement has no warning!');
54
    }
55
56
    /**
57
     * @depends testCheck
58
     */
59
    public function testCheckEval()
60
    {
61
        $requirementsChecker = new RequirementsChecker();
62
63
        $requirements = [
64
            'requirementPass' => [
65
                'name' => 'Requirement 1',
66
                'mandatory' => true,
67
                'condition' => 'eval:2>1',
68
                'by' => 'Requirement 1',
69
                'memo' => 'Requirement 1',
70
            ],
71
            'requirementError' => [
72
                'name' => 'Requirement 2',
73
                'mandatory' => true,
74
                'condition' => 'eval:2<1',
75
                'by' => 'Requirement 2',
76
                'memo' => 'Requirement 2',
77
            ],
78
        ];
79
80
        $checkResult = $requirementsChecker->check($requirements)->getResult();
81
        $checkedRequirements = $checkResult['requirements'];
82
        $requirementsKeys = array_flip(array_keys($requirements));
83
84
        $this->assertFalse($checkedRequirements[$requirementsKeys['requirementPass']]['error'], 'Passed requirement has an error!');
85
        $this->assertFalse($checkedRequirements[$requirementsKeys['requirementPass']]['warning'], 'Passed requirement has a warning!');
86
87
        $this->assertTrue($checkedRequirements[$requirementsKeys['requirementError']]['error'], 'Error requirement has no error!');
88
    }
89
90
    /**
91
     * @depends testCheck
92
     */
93
    public function testCheckChained()
94
    {
95
        $requirementsChecker = new RequirementsChecker();
96
97
        $requirements1 = [
98
            [
99
                'name' => 'Requirement 1',
100
                'mandatory' => true,
101
                'condition' => true,
102
                'by' => 'Requirement 1',
103
                'memo' => 'Requirement 1',
104
            ],
105
        ];
106
        $requirements2 = [
107
            [
108
                'name' => 'Requirement 2',
109
                'mandatory' => true,
110
                'condition' => true,
111
                'by' => 'Requirement 2',
112
                'memo' => 'Requirement 2',
113
            ],
114
        ];
115
        $checkResult = $requirementsChecker->check($requirements1)->check($requirements2)->getResult();
116
117
        $mergedRequirements = array_merge($requirements1, $requirements2);
118
119
        $this->assertCount($checkResult['summary']['total'], $mergedRequirements, 'Wrong total checks count!');
120
        foreach ($mergedRequirements as $key => $mergedRequirement) {
121
            $this->assertEquals($mergedRequirement['name'], $checkResult['requirements'][$key]['name'], 'Wrong requirements list!');
122
        }
123
    }
124
125
    public function testCheckPhpExtensionVersion()
126
    {
127
        if (defined('HHVM_VERSION')) {
128
            $this->markTestSkipped('Can not test this on HHVM.');
129
        }
130
131
        $requirementsChecker = new RequirementsChecker();
132
133
        $this->assertFalse($requirementsChecker->checkPhpExtensionVersion('some_unexisting_php_extension', '0.1'), 'No fail while checking unexisting extension!');
134
        $this->assertTrue($requirementsChecker->checkPhpExtensionVersion('pdo', '1.0'), 'Unable to check PDO version!');
135
    }
136
137
    /**
138
     * Data provider for [[testGetByteSize()]].
139
     * @return array
140
     */
141
    public function dataProviderGetByteSize()
142
    {
143
        return [
144
            ['456', 456],
145
            ['5K', 5 * 1024],
146
            ['16KB', 16 * 1024],
147
            ['4M', 4 * 1024 * 1024],
148
            ['14MB', 14 * 1024 * 1024],
149
            ['7G', 7 * 1024 * 1024 * 1024],
150
            ['12GB', 12 * 1024 * 1024 * 1024],
151
        ];
152
    }
153
154
    /**
155
     * @dataProvider dataProviderGetByteSize
156
     *
157
     * @param string  $verboseValue     verbose value.
158
     * @param int $expectedByteSize expected byte size.
159
     */
160
    public function testGetByteSize($verboseValue, $expectedByteSize)
161
    {
162
        $requirementsChecker = new RequirementsChecker();
163
164
        $this->assertEquals($expectedByteSize, $requirementsChecker->getByteSize($verboseValue), "Wrong byte size for '{$verboseValue}'!");
165
    }
166
167
    /**
168
     * Data provider for [[testCompareByteSize()]]
169
     * @return array
170
     */
171
    public function dataProviderCompareByteSize()
172
    {
173
        return [
174
            ['2M', '2K', '>', true],
175
            ['2M', '2K', '>=', true],
176
            ['1K', '1024', '==', true],
177
            ['10M', '11M', '<', true],
178
            ['10M', '11M', '<=', true],
179
        ];
180
    }
181
182
    /**
183
     * @depends testGetByteSize
184
     * @dataProvider dataProviderCompareByteSize
185
     *
186
     * @param string  $a                        first value.
187
     * @param string  $b                        second value.
188
     * @param string  $compare                  comparison.
189
     * @param bool $expectedComparisonResult expected comparison result.
190
     */
191
    public function testCompareByteSize($a, $b, $compare, $expectedComparisonResult)
192
    {
193
        $requirementsChecker = new RequirementsChecker();
194
        $this->assertEquals($expectedComparisonResult, $requirementsChecker->compareByteSize($a, $b, $compare), "Wrong compare '{$a}{$compare}{$b}'");
195
    }
196
}
197