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\NestedKeyIterator; |
15
|
|
|
use StringTemplate\RecursiveArrayOnlyIterator; |
16
|
|
|
|
17
|
|
|
class NestedKeyIteratorTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
public function testIteration() |
20
|
|
|
{ |
21
|
|
|
$ary = array( |
22
|
|
|
1 => 'a', |
23
|
|
|
'2' => 'b', |
24
|
|
|
'third' => array( |
25
|
|
|
'a', |
26
|
|
|
'b', |
27
|
|
|
'c' => array( |
28
|
|
|
1, |
29
|
|
|
'miao' => 'bau' |
30
|
|
|
) |
31
|
|
|
) |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
$iterator = new NestedKeyIterator(new RecursiveArrayOnlyIterator($ary)); |
35
|
|
|
|
36
|
|
|
$iterator->next(); |
37
|
|
|
$this->assertSame('1', $iterator->key()); |
38
|
|
|
$this->assertSame('a', $iterator->current()); |
39
|
|
|
|
40
|
|
|
$iterator->next(); |
41
|
|
|
$this->assertSame('2', $iterator->key()); |
42
|
|
|
$this->assertSame('b', $iterator->current()); |
43
|
|
|
|
44
|
|
|
$iterator->next(); |
45
|
|
|
$this->assertSame('third.0', $iterator->key()); |
46
|
|
|
$this->assertSame('a', $iterator->current()); |
47
|
|
|
|
48
|
|
|
$iterator->next(); |
49
|
|
|
$this->assertSame('third.1', $iterator->key()); |
50
|
|
|
$this->assertSame('b', $iterator->current()); |
51
|
|
|
|
52
|
|
|
$iterator->next(); |
53
|
|
|
$this->assertSame('third.c.0', $iterator->key()); |
54
|
|
|
$this->assertSame(1, $iterator->current()); |
55
|
|
|
|
56
|
|
|
$iterator->next(); |
57
|
|
|
$this->assertSame('third.c.miao', $iterator->key()); |
58
|
|
|
$this->assertSame('bau', $iterator->current()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testIterationWhenValueAreNotScalars() |
62
|
|
|
{ |
63
|
|
|
$ary = array( |
64
|
|
|
'a' => array( |
65
|
|
|
'b' => 'a', |
66
|
|
|
'c' => $c = new \stdClass(), |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$iterator = new NestedKeyIterator(new RecursiveArrayOnlyIterator($ary)); |
71
|
|
|
|
72
|
|
|
$iterator->next(); |
73
|
|
|
$this->assertSame('a.b', $iterator->key()); |
74
|
|
|
$this->assertSame('a', $iterator->current()); |
75
|
|
|
|
76
|
|
|
$iterator->next(); |
77
|
|
|
$this->assertSame('a.c', $iterator->key()); |
78
|
|
|
$this->assertSame($c, $iterator->current()); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|