1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
<?php
/*
* This file is part of Mustache.php.
*
* (c) 2010-2016 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @group unit
*/
class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$one = new Mustache_Context();
$this->assertSame('', $one->find('foo'));
$this->assertSame('', $one->find('bar'));
$two = new Mustache_Context(array(
'foo' => 'FOO',
'bar' => '<BAR>',
));
$this->assertEquals('FOO', $two->find('foo'));
$this->assertEquals('<BAR>', $two->find('bar'));
$obj = new StdClass();
$obj->name = 'NAME';
$three = new Mustache_Context($obj);
$this->assertSame($obj, $three->last());
$this->assertEquals('NAME', $three->find('name'));
}
public function testPushPopAndLast()
{
$context = new Mustache_Context();
$this->assertFalse($context->last());
$dummy = new Mustache_Test_TestDummy();
$context->push($dummy);
$this->assertSame($dummy, $context->last());
$this->assertSame($dummy, $context->pop());
$this->assertFalse($context->last());
$obj = new StdClass();
$context->push($dummy);
$this->assertSame($dummy, $context->last());
$context->push($obj);
$this->assertSame($obj, $context->last());
$this->assertSame($obj, $context->pop());
$this->assertSame($dummy, $context->pop());
$this->assertFalse($context->last());
}
public function testFind()
{
$context = new Mustache_Context();
$dummy = new Mustache_Test_TestDummy();
$obj = new StdClass();
$obj->name = 'obj';
$arr = array(
'a' => array('b' => array('c' => 'see')),
'b' => 'bee',
);
$string = 'some arbitrary string';
$context->push($dummy);
$this->assertEquals('dummy', $context->find('name'));
$context->push($obj);
$this->assertEquals('obj', $context->find('name'));
$context->pop();
$this->assertEquals('dummy', $context->find('name'));
$dummy->name = 'dummyer';
$this->assertEquals('dummyer', $context->find('name'));
$context->push($arr);
$this->assertEquals('bee', $context->find('b'));
$this->assertEquals('see', $context->findDot('a.b.c'));
$dummy->name = 'dummy';
$context->push($string);
$this->assertSame($string, $context->last());
$this->assertEquals('dummy', $context->find('name'));
$this->assertEquals('see', $context->findDot('a.b.c'));
$this->assertEquals('<foo>', $context->find('foo'));
$this->assertEquals('<bar>', $context->findDot('bar'));
}
public function testArrayAccessFind()
{
$access = new Mustache_Test_TestArrayAccess(array(
'a' => array('b' => array('c' => 'see')),
'b' => 'bee',
));
$context = new Mustache_Context($access);
$this->assertEquals('bee', $context->find('b'));
$this->assertEquals('see', $context->findDot('a.b.c'));
$this->assertEquals(null, $context->findDot('a.b.c.d'));
}
public function testAccessorPriority()
{
$context = new Mustache_Context(new Mustache_Test_AllTheThings());
$this->assertEquals('win', $context->find('foo'), 'method beats property');
$this->assertEquals('win', $context->find('bar'), 'property beats ArrayAccess');
$this->assertEquals('win', $context->find('baz'), 'ArrayAccess stands alone');
$this->assertEquals('win', $context->find('qux'), 'ArrayAccess beats private property');
}
public function testAnchoredDotNotation()
{
$context = new Mustache_Context();
$a = array(
'name' => 'a',
'number' => 1,
);
$b = array(
'number' => 2,
'child' => array(
'name' => 'baby bee',
),
);
$c = array(
'name' => 'cee',
);
$context->push($a);
$this->assertEquals('a', $context->find('name'));
$this->assertEquals('', $context->findDot('.name'));
$this->assertEquals('a', $context->findAnchoredDot('.name'));
$this->assertEquals(1, $context->find('number'));
$this->assertEquals('', $context->findDot('.number'));
$this->assertEquals(1, $context->findAnchoredDot('.number'));
$context->push($b);
$this->assertEquals('a', $context->find('name'));
$this->assertEquals(2, $context->find('number'));
$this->assertEquals('', $context->findDot('.name'));
$this->assertEquals('', $context->findDot('.number'));
$this->assertEquals('', $context->findAnchoredDot('.name'));
$this->assertEquals(2, $context->findAnchoredDot('.number'));
$this->assertEquals('baby bee', $context->findDot('child.name'));
$this->assertEquals('', $context->findDot('.child.name'));
$this->assertEquals('baby bee', $context->findAnchoredDot('.child.name'));
$context->push($c);
$this->assertEquals('cee', $context->find('name'));
$this->assertEquals('', $context->findDot('.name'));
$this->assertEquals('cee', $context->findAnchoredDot('.name'));
$this->assertEquals(2, $context->find('number'));
$this->assertEquals('', $context->findDot('.number'));
$this->assertEquals('', $context->findAnchoredDot('.number'));
$this->assertEquals('baby bee', $context->findDot('child.name'));
$this->assertEquals('', $context->findDot('.child.name'));
$this->assertEquals('', $context->findAnchoredDot('.child.name'));
}
/**
* @expectedException Mustache_Exception_InvalidArgumentException
*/
public function testAnchoredDotNotationThrowsExceptions()
{
$context = new Mustache_Context();
$context->push(array('a' => 1));
$context->findAnchoredDot('a');
}
}
class Mustache_Test_TestDummy
{
public $name = 'dummy';
public function __invoke()
{
// nothing
}
public static function foo()
{
return '<foo>';
}
public function bar()
{
return '<bar>';
}
}
class Mustache_Test_TestArrayAccess implements ArrayAccess
{
private $container = array();
public function __construct($array)
{
foreach ($array as $key => $value) {
$this->container[$key] = $value;
}
}
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}
public function offsetExists($offset)
{
return isset($this->container[$offset]);
}
public function offsetUnset($offset)
{
unset($this->container[$offset]);
}
public function offsetGet($offset)
{
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}
class Mustache_Test_AllTheThings implements ArrayAccess
{
public $foo = 'fail';
public $bar = 'win';
private $qux = 'fail';
public function foo()
{
return 'win';
}
public function offsetExists($offset)
{
return true;
}
public function offsetGet($offset)
{
switch ($offset) {
case 'foo':
case 'bar':
return 'fail';
case 'baz':
case 'qux':
return 'win';
default:
return 'lolwhut';
}
}
public function offsetSet($offset, $value)
{
// nada
}
public function offsetUnset($offset)
{
// nada
}
}
|