1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of library-template |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Nicolò Martini <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace StringTemplate\Test; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use StringTemplate\NestedKeyArray; |
15
|
|
|
|
16
|
|
|
class NestedKeyArrayTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $ary; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var NestedKeyArray |
25
|
|
|
*/ |
26
|
|
|
protected $nestedKeyAry; |
27
|
|
|
|
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
$this->ary = array( |
31
|
|
|
'a' => 'b', |
32
|
|
|
'c' => array( |
33
|
|
|
'd' => 'e', |
34
|
|
|
'f' => array( |
35
|
|
|
'g' => 'h' |
36
|
|
|
) |
37
|
|
|
), |
38
|
|
|
2 => 'i' |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$this->nestedKeyAry = new NestedKeyArray($this->ary); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testToArray() |
45
|
|
|
{ |
46
|
|
|
$this->assertEquals($this->ary, $this->nestedKeyAry->toArray()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testOffsetGet() |
50
|
|
|
{ |
51
|
|
|
$this->assertEquals('b', $this->nestedKeyAry['a']); |
52
|
|
|
$this->assertEquals( |
53
|
|
|
array( |
54
|
|
|
'd' => 'e', |
55
|
|
|
'f' => array( |
56
|
|
|
'g' => 'h' |
57
|
|
|
) |
58
|
|
|
), |
59
|
|
|
$this->nestedKeyAry['c']); |
60
|
|
|
$this->assertEquals('e', $this->nestedKeyAry['c.d']); |
61
|
|
|
$this->assertEquals('h', $this->nestedKeyAry['c.f.g']); |
62
|
|
|
$this->assertEquals('i', $this->nestedKeyAry['2']); |
63
|
|
|
$this->assertEquals('i', $this->nestedKeyAry[2]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testOffsetSet() |
67
|
|
|
{ |
68
|
|
|
$this->nestedKeyAry['a'] = 'bb'; |
69
|
|
|
$this->nestedKeyAry['c.f.g'] = 'hh'; |
70
|
|
|
$ary = $this->nestedKeyAry->toArray(); |
71
|
|
|
|
72
|
|
|
$this->assertEquals('bb', $ary['a']); |
73
|
|
|
$this->assertEquals('hh', $ary['c']['f']['g']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testOffsetUnset() |
77
|
|
|
{ |
78
|
|
|
unset($this->nestedKeyAry['a']); |
79
|
|
|
unset($this->nestedKeyAry['c.f.g']); |
80
|
|
|
$ary = $this->nestedKeyAry->toArray(); |
81
|
|
|
|
82
|
|
|
$this->assertFalse(isset($ary['a'])); |
83
|
|
|
$this->assertFalse(isset($ary['c']['f']['g'])); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testOffsetExists() |
87
|
|
|
{ |
88
|
|
|
$this->assertTrue(isset($this->nestedKeyAry['a'])); |
89
|
|
|
$this->assertTrue(isset($this->nestedKeyAry['c.f.g'])); |
90
|
|
|
$this->assertTrue(isset($this->nestedKeyAry['c.f'])); |
91
|
|
|
|
92
|
|
|
$this->assertFalse(isset($this->nestedKeyAry['aa'])); |
93
|
|
|
$this->assertFalse(isset($this->nestedKeyAry['c.ff'])); |
94
|
|
|
$this->assertFalse(isset($this->nestedKeyAry['c.f.gg'])); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testIteration() |
98
|
|
|
{ |
99
|
|
|
$flattenAry = array(); |
100
|
|
|
foreach ($this->nestedKeyAry as $key => $value) |
101
|
|
|
{ |
102
|
|
|
$flattenAry[$key] = $value; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->assertEquals( |
106
|
|
|
array( |
107
|
|
|
'a' => 'b', |
108
|
|
|
'c.d' => 'e', |
109
|
|
|
'c.f.g' => 'h', |
110
|
|
|
'2' => 'i' |
111
|
|
|
), |
112
|
|
|
$flattenAry |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|