1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Glenn\Config\Test; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
|
7
|
|
|
use Glenn\Config\Manager; |
8
|
|
|
|
9
|
|
|
class ManagerTest extends PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
protected $sampleConfigData = [ |
12
|
|
|
'name' => 'Glenn', |
13
|
|
|
'age' => 18, |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Test the @has method. |
18
|
|
|
* @author Glenn McEwan <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
public function testHasMethod() |
21
|
|
|
{ |
22
|
|
|
$config = new Manager($this->sampleConfigData); |
23
|
|
|
|
24
|
|
|
$this->assertTrue($config->has('name')); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test the @get method. |
29
|
|
|
* @author Glenn McEwan <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
public function testGetMethod() |
32
|
|
|
{ |
33
|
|
|
$config = new Manager($this->sampleConfigData); |
34
|
|
|
|
35
|
|
|
$this->assertEquals($config->get('name'), 'Glenn'); |
36
|
|
|
$this->assertSame($config->get('age'), 18); |
37
|
|
|
$this->assertNotSame($config->get('age'), '18'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Test the @set method. |
42
|
|
|
* @author Glenn McEwan <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
public function testSetMethod() |
45
|
|
|
{ |
46
|
|
|
$config = new Manager($this->sampleConfigData); |
47
|
|
|
|
48
|
|
|
$config->set('gender', 'male'); |
49
|
|
|
|
50
|
|
|
$this->assertTrue($config->has('gender')); |
51
|
|
|
$this->assertSame($config->get('gender'), 'male'); |
52
|
|
|
|
53
|
|
|
$config->set('gender', 'female'); |
54
|
|
|
|
55
|
|
|
$this->assertSame($config->get('gender'), 'female'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Test the @get method works properly when passing a default. |
60
|
|
|
* @author Glenn McEwan <[email protected]> |
61
|
|
|
*/ |
62
|
|
|
public function testGetDefaultMethod() |
63
|
|
|
{ |
64
|
|
|
$config = new Manager($this->sampleConfigData); |
65
|
|
|
|
66
|
|
|
$this->assertEquals($config->get('gender', 'male'), 'male'); |
67
|
|
|
$this->assertEquals($config->get('gender', 'female'), 'female'); |
68
|
|
|
$this->assertNull($config->get('gender')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Test the @all method. |
73
|
|
|
* @author Glenn McEwan <[email protected]> |
74
|
|
|
*/ |
75
|
|
|
public function testAllMethod() |
76
|
|
|
{ |
77
|
|
|
$sampleData = $this->sampleConfigData; |
78
|
|
|
|
79
|
|
|
$config = new Manager($sampleData); |
80
|
|
|
|
81
|
|
|
$this->assertSame($config->all(), $sampleData); |
82
|
|
|
|
83
|
|
|
$sampleData['gender'] = 'male'; |
84
|
|
|
|
85
|
|
|
$config->set('gender', 'male'); |
86
|
|
|
|
87
|
|
|
$this->assertSame($config->all(), $sampleData); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Test the @set method sets a config entry, |
92
|
|
|
* and is retrievable with @get. |
93
|
|
|
* @get with a default parameter should still return null if the |
94
|
|
|
* original @set call had a null value. |
95
|
|
|
* @author Glenn McEwan <[email protected]> |
96
|
|
|
*/ |
97
|
|
|
public function testSetNullMethod() |
98
|
|
|
{ |
99
|
|
|
$config = new Manager($this->sampleConfigData); |
100
|
|
|
|
101
|
|
|
$config->set('gender'); |
102
|
|
|
|
103
|
|
|
$this->assertTrue($config->has('gender')); |
104
|
|
|
$this->assertNull($config->get('gender')); |
105
|
|
|
$this->assertNull($config->get('gender', 'male')); |
106
|
|
|
|
107
|
|
|
$config->set('gender', ''); |
108
|
|
|
|
109
|
|
|
$this->assertSame($config->get('gender'), ''); |
110
|
|
|
|
111
|
|
|
$config->set('gender', 'female'); |
112
|
|
|
|
113
|
|
|
$this->assertSame($config->get('gender'), 'female'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|