forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserTest.php
More file actions
200 lines (153 loc) · 5.39 KB
/
Copy pathParserTest.php
File metadata and controls
200 lines (153 loc) · 5.39 KB
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
<?php
use CodeIgniter\View\Parser;
class ParserTest extends \CIUnitTestCase
{
public function setUp()
{
$this->loader = new \CodeIgniter\Autoloader\FileLocator(new \Config\Autoload());
$this->viewsDir = __DIR__.'/Views';
$this->config = new Config\View();
}
// --------------------------------------------------------------------
public function testSetDelimiters()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
// Make sure default delimiters are there
$this->assertEquals('{', $parser->leftDelimiter);
$this->assertEquals('}', $parser->rightDelimiter);
// Change them to square brackets
$parser->setDelimiters('[', ']');
// Make sure they changed
$this->assertEquals('[', $parser->leftDelimiter);
$this->assertEquals(']', $parser->rightDelimiter);
// Reset them
$parser->setDelimiters();
// Make sure default delimiters are there
$this->assertEquals('{', $parser->leftDelimiter);
$this->assertEquals('}', $parser->rightDelimiter);
}
// --------------------------------------------------------------------
public function testParseSimple()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$parser->setVar('teststring', 'Hello World');
$expected = '<h1>Hello World</h1>';
$this->assertEquals($expected, $parser->render('template1'));
}
// --------------------------------------------------------------------
public function testParseString()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$data = array (
'title' => 'Page Title',
'body' => 'Lorem ipsum dolor sit amet.'
);
$template = "{title}\n{body}";
$result = implode("\n", $data);
$parser->setData($data);
$this->assertEquals($result, $parser->renderString($template));
}
// --------------------------------------------------------------------
public function testParseStringMissingData()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$data = array (
'title' => 'Page Title',
'body' => 'Lorem ipsum dolor sit amet.'
);
$template = "{title}\n{body}\n{name}";
$result = implode("\n", $data)."\n{name}";
$parser->setData($data);
$this->assertEquals($result, $parser->renderString($template));
}
// --------------------------------------------------------------------
public function testParseStringUnusedData()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$data = array (
'title' => 'Page Title',
'body' => 'Lorem ipsum dolor sit amet.',
'name' => 'Someone'
);
$template = "{title}\n{body}";
$result = "Page Title\nLorem ipsum dolor sit amet.";
$parser->setData($data);
$this->assertEquals($result, $parser->renderString($template));
}
// --------------------------------------------------------------------
public function testParseNoTemplate()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$this->assertEquals('', $parser->renderString(''));
}
// --------------------------------------------------------------------
public function testParseNested()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$data = array (
'title' => 'Super Heroes',
'powers' => array (
array ('invisibility' => 'yes', 'flying' => 'no')
)
);
$template = "{title}\n{powers}{invisibility}\n{flying}{/powers}\nsecond:{powers} {invisibility} {flying}{/powers}";
$parser->setData($data);
$this->assertEquals("Super Heroes\nyes\nno\nsecond: yes no", $parser->renderString($template));
}
// --------------------------------------------------------------------
public function testParseLoop()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$data = array (
'title' => 'Super Heroes',
'powers' => array (
array ('name' => 'Tom'),
array ('name' => 'Dick'),
array ('name' => 'Henry')
)
);
$template = "{title}\n{powers}{name} {/powers}";
$parser->setData($data);
$this->assertEquals("Super Heroes\nTom Dick Henry ", $parser->renderString($template));
}
// --------------------------------------------------------------------
public function testMismatchedVarPair()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$data = array (
'title' => 'Super Heroes',
'powers' => array (
array ('invisibility' => 'yes', 'flying' => 'no')
)
);
$template = "{title}\n{powers}{invisibility}\n{flying}";
$result = "Super Heroes\n{powers}{invisibility}\n{flying}";
$parser->setData($data);
$this->assertEquals($result, $parser->renderString($template));
}
// Test anchor
public function escValueTypes()
{
return [
'scalar' => [42],
'string' => ['George'],
'scalarlist' => [ [1, 2, 17, -4]],
'stringlist' => [ ['George', 'Paul', 'John', 'Ringo']],
'associative' => [ ['name' => 'George', 'role' => 'guitar']],
'compound' => [ ['name' => 'George', 'address' => ['line1' => '123 Some St', 'planet' => 'Naboo']]],
'pseudo' => [ ['name' => 'George', 'emails' => [
['email' => 'me@here.com', 'type' => 'home'],
['email' => 'me@there.com', 'type' => 'work']
]]],
];
}
/**
* @dataProvider escValueTypes
*/
public function testEscHandling($value, $expected = null)
{
if ($expected == null) $expected = $value;
$this->assertEquals($expected, \esc($value, 'html'));
}
// ------------------------------------------------------------------------
}