/
StringTemplateTest.php
197 lines (177 loc) · 4.74 KB
/
StringTemplateTest.php
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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\View;
use Cake\Core\Plugin;
use Cake\TestSuite\TestCase;
use Cake\View\StringTemplate;
class StringTemplateTest extends TestCase {
/**
* setUp
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->template = new StringTemplate();
}
/**
* Test adding templates through the constructor.
*
* @return void
*/
public function testConstructorAdd() {
$templates = [
'link' => '<a href="{{url}}">{{text}}</a>'
];
$template = new StringTemplate($templates);
$this->assertEquals($templates['link'], $template->get('link'));
}
/**
* test adding templates.
*
* @return void
*/
public function testAdd() {
$templates = [
'link' => '<a href="{{url}}">{{text}}</a>'
];
$result = $this->template->add($templates);
$this->assertSame(
$this->template,
$result,
'The same instance should be returned'
);
$this->assertEquals($templates['link'], $this->template->get('link'));
}
/**
* Test remove.
*
* @return void
*/
public function testRemove() {
$templates = [
'link' => '<a href="{{url}}">{{text}}</a>'
];
$this->template->add($templates);
$this->assertNull($this->template->remove('link'), 'No return');
$this->assertNull($this->template->get('link'), 'Template should be gone.');
}
/**
* Test formatting strings.
*
* @return void
*/
public function testFormat() {
$templates = [
'link' => '<a href="{{url}}">{{text}}</a>'
];
$this->template->add($templates);
$result = $this->template->format('not there', []);
$this->assertSame('', $result);
$result = $this->template->format('link', [
'url' => '/',
'text' => 'example'
]);
$this->assertEquals('<a href="/">example</a>', $result);
}
/**
* Test loading templates files in the app.
*
* @return void
*/
public function testLoad() {
$this->template->remove('attribute');
$this->template->remove('compactAttribute');
$this->assertEquals([], $this->template->get());
$this->assertNull($this->template->load('test_templates'));
$this->assertEquals('<a href="{{url}}">{{text}}</a>', $this->template->get('link'));
}
/**
* Test loading templates files from a plugin
*
* @return void
*/
public function testLoadPlugin() {
Plugin::load('TestPlugin');
$this->assertNull($this->template->load('TestPlugin.test_templates'));
$this->assertEquals('<em>{{text}}</em>', $this->template->get('italic'));
}
/**
* Test that loading non-existing templates causes errors.
*
* @expectedException \Cake\Error\Exception
* @expectedExceptionMessage Could not load configuration file
*/
public function testLoadErrorNoFile() {
$this->template->load('no_such_file');
}
/**
* Test formatting compact attributes.
*
* @return void
*/
public function testFormatAttributesCompact() {
$attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
$result = $this->template->formatAttributes($attrs);
$this->assertEquals(
' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
$result
);
$attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
$result = $this->template->formatAttributes($attrs);
$this->assertEquals(
'',
$result
);
}
/**
* Test formatting normal attributes.
*
* @return void
*/
public function testFormatAttributes() {
$attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
$result = $this->template->formatAttributes($attrs);
$this->assertEquals(
' name="bruce" data-hero="<batman>"',
$result
);
$attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
$result = $this->template->formatAttributes($attrs);
$this->assertEquals(
' name="bruce" data-hero="<batman>"',
$result
);
$attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
$result = $this->template->formatAttributes($attrs, ['name']);
$this->assertEquals(
' data-hero="<batman>"',
$result
);
}
/**
* Test formatting array attributes.
*
* @return void
*/
public function testFormatAttributesArray() {
$attrs = ['name' => ['bruce', 'wayne']];
$result = $this->template->formatAttributes($attrs);
$this->assertEquals(
' name="bruce wayne"',
$result
);
}
}