1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Template; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class TemplateTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** @var Template - System Under Test */ |
12
|
|
|
protected $sut; |
13
|
|
|
|
14
|
|
|
public function setUp(): void |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
|
18
|
|
|
$this->sut = new Template(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function parsingSuccessProvider(): array |
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
|
|
'empty' => [ |
28
|
|
|
'', |
29
|
|
|
[], |
30
|
|
|
], |
31
|
|
|
'fail' => [ |
32
|
|
|
'', |
33
|
|
|
[], |
34
|
|
|
], |
35
|
|
|
'content-only-1' => [ |
36
|
|
|
'{{block/content-one-1}}', |
37
|
|
|
[ |
38
|
|
|
'block' => |
39
|
|
|
[ |
40
|
|
|
'content-one-1' => [ |
41
|
|
|
new ParsedTemplate('block', 'content-one-1', [], ['{{block/content-one-1}}']), // phpcs:ignore |
42
|
|
|
], |
43
|
|
|
] |
44
|
|
|
], |
45
|
|
|
], |
46
|
|
|
'content-only-2' => [ |
47
|
|
|
'{{block/content-one-1}} {{ block/content-2-two }}', |
48
|
|
|
[ |
49
|
|
|
'block' => [ |
50
|
|
|
'content-2-two' => [ |
51
|
|
|
new ParsedTemplate('block', 'content-2-two', [], ['{{ block/content-2-two }}']), // phpcs:ignore |
52
|
|
|
], |
53
|
|
|
'content-one-1' => [ |
54
|
|
|
new ParsedTemplate('block', 'content-one-1', [], ['{{block/content-one-1}}']), |
55
|
|
|
], |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
], |
59
|
|
|
'layout-only-3' => [ |
60
|
|
|
'{{block/layout-one-1}} {{ block/layout-2-two }} {{block/layout-one-1 }}', |
61
|
|
|
[ |
62
|
|
|
'block' => [ |
63
|
|
|
'layout-2-two' => [ |
64
|
|
|
new ParsedTemplate('block', 'layout-2-two', [], ['{{ block/layout-2-two }}']), // phpcs:ignore |
65
|
|
|
], |
66
|
|
|
'layout-one-1' => [ |
67
|
|
|
new ParsedTemplate( |
68
|
|
|
'block', |
69
|
|
|
'layout-one-1', |
70
|
|
|
[], |
71
|
|
|
['{{block/layout-one-1}}', '{{block/layout-one-1 }}'] |
72
|
|
|
), |
73
|
|
|
], |
74
|
|
|
], |
75
|
|
|
], |
76
|
|
|
], |
77
|
|
|
'layout-with-attribute' => [ |
78
|
|
|
'{{block/layout-one-1 a="value is a" a-2="value is a-2"}}', |
79
|
|
|
[ |
80
|
|
|
'block' => [ |
81
|
|
|
'layout-one-1' => [ |
82
|
|
|
new ParsedTemplate( |
83
|
|
|
'block', |
84
|
|
|
'layout-one-1', |
85
|
|
|
['a' => 'value is a', 'a-2' => 'value is a-2'], |
86
|
|
|
['{{block/layout-one-1 a="value is a" a-2="value is a-2"}}'] |
87
|
|
|
), |
88
|
|
|
], |
89
|
|
|
], |
90
|
|
|
], |
91
|
|
|
], |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @dataProvider parsingSuccessProvider |
97
|
|
|
* |
98
|
|
|
* @param string $rawContent |
99
|
|
|
* @param array $expectedTemplates |
100
|
|
|
*/ |
101
|
|
|
public function testParse(string $rawContent, array $expectedTemplates) |
102
|
|
|
{ |
103
|
|
|
$this->sut->setRawContent($rawContent); |
104
|
|
|
|
105
|
|
|
$actualTemplates = $this->sut->parse(); |
106
|
|
|
|
107
|
|
|
$this->assertEquals($expectedTemplates, $actualTemplates); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function renderingSuccessProvider(): array |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
|
|
'content-only-1' => [ |
117
|
|
|
'abc', |
118
|
|
|
[], |
119
|
|
|
[], |
120
|
|
|
'abc', |
121
|
|
|
], |
122
|
|
|
'content-with-vars-only-1' => [ |
123
|
|
|
'0 {{var/variable-one-1}} 1', |
124
|
|
|
['variable-one-1' => 'abc'], |
125
|
|
|
[], |
126
|
|
|
'0 abc 1', |
127
|
|
|
], |
128
|
|
|
'content-with-vars-only-2' => [ |
129
|
|
|
'0 {{var/variable-one-1}} 1{{var/variable-one-2}}2', |
130
|
|
|
['variable-one-1' => 'abc'], |
131
|
|
|
[], |
132
|
|
|
'0 abc 12', |
133
|
|
|
], |
134
|
|
|
'content-with-vars-only-3' => [ |
135
|
|
|
'0 {{var/variable-one-1}} 1{{var/variable-one-2}}2', |
136
|
|
|
['variable-one-1' => 'abc', 'variable-one-2' => 'bcd'], |
137
|
|
|
[], |
138
|
|
|
'0 abc 1bcd2', |
139
|
|
|
], |
140
|
|
|
'content-with-repeated-vars' => [ |
141
|
|
|
'0 {{var/variable-one-1}} {{var/variable-one-1}} 1', |
142
|
|
|
['variable-one-1' => 'abc'], |
143
|
|
|
[], |
144
|
|
|
'0 abc abc 1', |
145
|
|
|
], |
146
|
|
|
'content-with-modified-repeated-vars' => [ |
147
|
|
|
'0 {{var/variable-one-1}} {{ var/variable-one-1 }} 1', |
148
|
|
|
['variable-one-1' => 'abc'], |
149
|
|
|
[], |
150
|
|
|
'0 abc abc 1', |
151
|
|
|
], |
152
|
|
|
'content-with-blocks-only-1' => [ |
153
|
|
|
'0 {{block/one-1}} 1', |
154
|
|
|
[], |
155
|
|
|
['block' => ['one-1' => 'abc']], |
156
|
|
|
'0 abc 1', |
157
|
|
|
], |
158
|
|
|
'content-with-blocks-only-2' => [ |
159
|
|
|
'0 {{block/one-1}} 1{{block/two-2-two}}2', |
160
|
|
|
[], |
161
|
|
|
['block' => ['one-1' => 'abc']], |
162
|
|
|
'0 abc 12', |
163
|
|
|
], |
164
|
|
|
'content-with-blocks-only-3' => [ |
165
|
|
|
'0 {{block/one-1}} 1{{block/two-2-two}}2', |
166
|
|
|
[], |
167
|
|
|
['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd']], |
168
|
|
|
'0 abc 1bcd2', |
169
|
|
|
], |
170
|
|
|
'content-with-repeated-blocks' => [ |
171
|
|
|
'0 {{block/one-1}} {{block/one-1}} 1{{block/two-2-two}}2', |
172
|
|
|
[], |
173
|
|
|
['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd']], |
174
|
|
|
'0 abc abc 1bcd2', |
175
|
|
|
], |
176
|
|
|
'content-with-modified-repeated-blocks' => [ |
177
|
|
|
'0 {{block/one-1}} {{ block/one-1 }} 1{{block/two-2-two}}2', |
178
|
|
|
[], |
179
|
|
|
['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd']], |
180
|
|
|
'0 abc abc 1bcd2', |
181
|
|
|
], |
182
|
|
|
'complex-1' => [ |
183
|
|
|
'0 {{block/one-1}} {{ block/one-1 }} {{var/3-threeThree}} 1{{block/two-2-two}}2{{gallery/event-1}} {{ block/two-2-two }}', // phpcs:ignore |
184
|
|
|
['3-threeThree' => 'cde'], |
185
|
|
|
['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd'], 'gallery' => ['event-1' => 'fgh']], |
186
|
|
|
'0 abc abc cde 1bcd2fgh bcd', |
187
|
|
|
], |
188
|
|
|
'complex-without-subtemplate-value' => [ |
189
|
|
|
'0 {{block/one-1}} {{ block/one-1 }} {{var/3-threeThree}} 1{{block/two-2-two}}2{{gallery/event-1}} {{ block/two-2-two }}', // phpcs:ignore |
190
|
|
|
['3-threeThree' => 'cde'], |
191
|
|
|
['block' => ['one-1' => 'abc'], 'gallery' => ['event-1' => 'fgh']], |
192
|
|
|
'0 abc abc cde 12fgh ', |
193
|
|
|
], |
194
|
|
|
'complex-without-subtemplate-type' => [ |
195
|
|
|
'0 {{block/one-1}} {{ block/one-1 }} {{var/3-threeThree}} 1{{block/two-2-two}}2{{gallery/event-1}} {{ block/two-2-two }}', // phpcs:ignore |
196
|
|
|
['3-threeThree' => 'cde'], |
197
|
|
|
['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd']], |
198
|
|
|
'0 abc abc cde 1bcd2 bcd', |
199
|
|
|
], |
200
|
|
|
'brutal' => [ |
201
|
|
|
"0 {{nope/nay}} {{block/one-1}} {{ block/one-1 }} {{var/3-threeThree}} 1{{block/two-2-two foo=\"This foo!\" bar=\"That bar!\"}}2{{gallery/event-1}}\n{{ block/two-2-two }}", // phpcs:ignore |
202
|
|
|
['3-threeThree' => 'cde'], |
203
|
|
|
['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd']], |
204
|
|
|
"0 {{nope/nay}} abc abc cde 1bcd2\nbcd", |
205
|
|
|
], |
206
|
|
|
]; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @dataProvider renderingSuccessProvider |
211
|
|
|
* |
212
|
|
|
* @param string $rawContent |
213
|
|
|
* @param array $vars |
214
|
|
|
* @param array $templateData |
215
|
|
|
* @param string $expectedResult |
216
|
|
|
*/ |
217
|
|
|
public function testRender(string $rawContent, array $vars, array $templateData, string $expectedResult) |
218
|
|
|
{ |
219
|
|
|
$this->sut->setRawContent($rawContent)->setVars($vars)->setTypes(['block', 'gallery']); |
220
|
|
|
|
221
|
|
|
$this->sut->parse(); |
222
|
|
|
|
223
|
|
|
$actualResult = $this->sut->render($templateData); |
224
|
|
|
|
225
|
|
|
$this->assertSame($expectedResult, $actualResult); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|