Test Failed
Branch main (1f2fe7)
by Stefan
01:54
created

AbstractConfigTest::test_configIsNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Test\Config;
5
6
use PHPUnit\Framework\TestCase;
7
use SKien\Config\AbstractConfig;
8
9
/**
10
 * Abstract class providing comprehensive tests for all classes that implement the
11
 * PNDataProvider Interface.
12
 * Extending classes have to 
13
 * - create database fixture for whole testclass in the static setUpBeforeClass() method
14
 * - create an instance of the class in the setUp() method and assign it to the $dp property
15
 * - implement tests to verify the creation
16
 * - implement tests for extended functions not supported by PNDataProvider 
17
 * - Clean-Up Database after last test in the static tearDownAfterClass() method
18
 * 
19
 * @author Stefanius <[email protected]>
20
 * @copyright MIT License - see the LICENSE file for details
21
 */
22
abstract class AbstractConfigTest extends TestCase
23
{
24
    /**
25
     * @depends test_constructNull
26
     */
27
    public function test_configIsNull(AbstractConfig $cfg) : void
28
    {
29
        // create reflection object to manipulate protected property of AbstractConfig
30
        $reflectionObject = new \ReflectionObject($cfg);
31
        
32
        $reflectionConfig = $reflectionObject->getProperty('aConfig');
33
        $reflectionConfig->setAccessible(true);
34
        $reflectionConfig->setValue($cfg, null);
35
        $this->assertEquals('default', $cfg->getString('BaseString_1', 'default'));
36
    }
37
    
38
    /**
39
     * @depends test_construct
40
     */
41
    public function test_getString(AbstractConfig $cfg) : void
42
    {
43
        $this->assertEquals('Base String Parameter', $cfg->getString('BaseString_1'));
44
    }
45
    
46
    /**
47
     * @depends test_construct
48
     */
49
    public function test_getStringInvalid(AbstractConfig $cfg) : void
50
    {
51
        $this->assertEquals('invalid', $cfg->getString('BaseString_1.Invalid', 'invalid'));
52
    }
53
    
54
    /**
55
     * @depends test_construct
56
     */
57
    public function test_getStringDefault(AbstractConfig $cfg) : void
58
    {
59
        $this->assertEquals('default', $cfg->getString('BaseString_X', 'default'));
60
    }
61
    
62
    /**
63
     * @depends test_construct
64
     */
65
    public function test_getInt(AbstractConfig $cfg) : void
66
    {
67
        $this->assertEquals(10, $cfg->getInt('Module_1.Int_1'));
68
    }
69
    
70
    /**
71
     * @depends test_construct
72
     */
73
    public function test_getIntDefault(AbstractConfig $cfg) : void
74
    {
75
        $this->assertEquals(33, $cfg->getInt('Module_1.Int_X', 33));
76
    }
77
    
78
    /**
79
     * @depends test_construct
80
     */
81
    public function test_getFloat(AbstractConfig $cfg) : void
82
    {
83
        $this->assertEquals(12.34, $cfg->getFloat('Module_1.Float_1'));
84
    }
85
    
86
    /**
87
     * @depends test_construct
88
     */
89
    public function test_getFloatDefault(AbstractConfig $cfg) : void
90
    {
91
        $this->assertEquals(56.78, $cfg->getFloat('Module_1.Float_X', 56.78));
92
    }
93
    
94
    /**
95
     * @depends test_construct
96
     */
97
    public function test_getBoolTrue(AbstractConfig $cfg) : void
98
    {
99
        $this->assertEquals(true, $cfg->getBool('Module_2.True_1'));
100
        $this->assertEquals(true, $cfg->getBool('Module_2.True_2'));
101
        $this->assertEquals(true, $cfg->getBool('Module_2.True_3'));
102
        $this->assertEquals(true, $cfg->getBool('Module_2.True_4'));
103
        $this->assertEquals(true, $cfg->getBool('Module_2.True_5'));
104
        $this->assertEquals(true, $cfg->getBool('Module_2.True_6'));
105
        $this->assertEquals(true, $cfg->getBool('Module_2.True_7'));
106
        $this->assertEquals(true, $cfg->getBool('Module_2.True_8'));
107
    }
108
    
109
    /**
110
     * @depends test_construct
111
     */
112
    public function test_getBoolFalse(AbstractConfig $cfg) : void
113
    {
114
        $this->assertEquals(false, $cfg->getBool('Module_2.False_1'));
115
        $this->assertEquals(false, $cfg->getBool('Module_2.False_2'));
116
        $this->assertEquals(false, $cfg->getBool('Module_2.False_3'));
117
        $this->assertEquals(false, $cfg->getBool('Module_2.False_4'));
118
        $this->assertEquals(false, $cfg->getBool('Module_2.False_5'));
119
        $this->assertEquals(false, $cfg->getBool('Module_2.False_6'));
120
        $this->assertEquals(false, $cfg->getBool('Module_2.False_7'));
121
        $this->assertEquals(false, $cfg->getBool('Module_2.False_8'));
122
    }
123
    
124
    /**
125
     * @depends test_construct
126
     */
127
    public function test_getBoolInvalid(AbstractConfig $cfg) : void
128
    {
129
        $this->assertEquals(false, $cfg->getBool('Module_1.Bool_Invalid', false));
130
        $this->assertEquals(true, $cfg->getBool('Module_1.Bool_Invalid', true));
131
    }
132
    
133
    /**
134
     * @depends test_construct
135
     */
136
    public function test_getBoolDefault(AbstractConfig $cfg) : void
137
    {
138
        $this->assertEquals(false, $cfg->getBool('Module_1.Bool_X', false));
139
        $this->assertEquals(true, $cfg->getBool('Module_1.Bool_X', true));
140
    }
141
    
142
    /**
143
     * @depends test_construct
144
     */
145
    public function test_getDate(AbstractConfig $cfg) : void
146
    {
147
        $this->assertEquals('2021-01-12', date('Y-m-d', $cfg->getDate('Module_1.Date_1')));
148
        $this->assertEquals('2021-02-20', date('Y-m-d', $cfg->getDate('Module_1.Date_2')));
149
        $cfg->setDateFormat('d.m.Y');
150
        $this->assertEquals('2012-03-14', date('Y-m-d', $cfg->getDate('Module_1.Date_3')));
151
    }
152
    
153
    /**
154
     * @depends test_construct
155
     */
156
    public function test_getDateInvalid(AbstractConfig $cfg) : void
157
    {
158
        $this->assertEquals('2021-02-20', date('Y-m-d', $cfg->getDate('Module_1.Date_Invalid', mktime(0, 0, 0, 02, 20, 2021))));
159
    }
160
    
161
    /**
162
     * @depends test_construct
163
     */
164
    public function test_getDateDefault(AbstractConfig $cfg) : void
165
    {
166
        $this->assertEquals('2021-02-20', date('Y-m-d', $cfg->getDate('Module_1.Date_X', mktime(0, 0, 0, 02, 20, 2021))));
167
    }
168
    
169
    /**
170
     * @depends test_construct
171
     */
172
    public function test_getDateTime(AbstractConfig $cfg) : void
173
    {
174
        $this->assertEquals('2021-01-12 13:47', date('Y-m-d H:i', $cfg->getDateTime('Module_1.DateTime_1')));
175
        $this->assertEquals('2021-02-20 05:53', date('Y-m-d H:i', $cfg->getDateTime('Module_1.DateTime_2')));
176
        $cfg->setDateTimeFormat('d.m.Y H:i');
177
        $this->assertEquals('2012-03-14 13:47', date('Y-m-d H:i', $cfg->getDateTime('Module_1.DateTime_3')));
178
    }
179
    
180
    /**
181
     * @depends test_construct
182
     */
183
    public function test_getDateTimeInvalid(AbstractConfig $cfg) : void
184
    {
185
        $this->assertEquals('2021-02-20 16:27', date('Y-m-d H:i', $cfg->getDateTime('Module_1.DateTime_Invalid', mktime(16, 27, 0, 02, 20, 2021))));
186
    }
187
    
188
    /**
189
     * @depends test_construct
190
     */
191
    public function test_getDateTimeDefault(AbstractConfig $cfg) : void
192
    {
193
        $this->assertEquals('2021-02-20 16:27', date('Y-m-d H:i', $cfg->getDateTime('Module_1.DateTime_X', mktime(16, 27, 0, 02, 20, 2021))));
194
    }
195
    
196
    /**
197
     * @depends test_construct
198
     */
199
    public function test_getIndexedArray(AbstractConfig $cfg) : void
200
    {
201
        $aValues = $cfg->getArray('IndexedArray');
202
        $this->assertIsArray($aValues);
203
        $this->assertEquals(3, count($aValues));
204
        $this->assertEquals('First Element', $aValues[0]);
205
        $this->assertEquals('Second Element', $aValues[1]);
206
        $this->assertEquals('Third Element', $aValues[2]);
207
    }
208
    
209
    /**
210
     * @depends test_construct
211
     */
212
    public function test_getAssocArray(AbstractConfig $cfg) : void
213
    {
214
        $aValues = $cfg->getArray('AssocArray');
215
        $this->assertIsArray($aValues);
216
        $this->assertEquals(3, count($aValues));
217
        $this->assertEquals('Element 1', $aValues['First']);
218
        $this->assertEquals('Element 2', $aValues['Second']);
219
        $this->assertEquals('Element 3', $aValues['Third']);
220
    }
221
}
222
223