1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace VojtaSvoboda\TwigExtensions\Tests; |
4
|
|
|
|
5
|
|
|
use App; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Config; |
8
|
|
|
use PluginTestCase; |
9
|
|
|
use Twig_Environment; |
10
|
|
|
use VojtaSvoboda\TwigExtensions\Classes\TimeDiffTranslator; |
11
|
|
|
|
12
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
13
|
|
|
|
14
|
|
|
class PluginTest extends PluginTestCase |
15
|
|
|
{ |
16
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
$this->app->setLocale('en'); |
21
|
|
|
|
22
|
|
|
$this->app->singleton('time_diff_translator', function($app) { |
23
|
|
|
$loader = $app->make('translation.loader'); |
24
|
|
|
$locale = $app->config->get('app.locale'); |
25
|
|
|
$translator = $app->make(TimeDiffTranslator::class, [$loader, $locale]); |
26
|
|
|
$translator->setFallback($app->config->get('app.fallback_locale')); |
27
|
|
|
|
28
|
|
|
return $translator; |
29
|
|
|
}); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return Twig environment |
34
|
|
|
* |
35
|
|
|
* @return Twig_Environment |
36
|
|
|
*/ |
37
|
|
|
private function getTwig() |
38
|
|
|
{ |
39
|
|
|
return App::make('twig.environment'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function testTemplateFromStringFunction() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$twig = $this->getTwig(); |
45
|
|
|
|
46
|
|
|
$template = "{% set name = 'John' %}"; |
47
|
|
|
$template .= '{{ include(template_from_string("Hello {{ name }}")) }}'; |
48
|
|
|
|
49
|
|
|
$twigTemplate = $twig->createTemplate($template); |
50
|
|
|
$this->assertEquals($twigTemplate->render([]), 'Hello John'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function testTruncateFilterForFive() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$twig = $this->getTwig(); |
56
|
|
|
|
57
|
|
|
$template = "{{ 'Gordon Freeman' | truncate(5) }}"; |
58
|
|
|
|
59
|
|
|
$twigTemplate = $twig->createTemplate($template); |
60
|
|
|
$this->assertEquals($twigTemplate->render([]), 'Gordo...'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testTruncateFilterForDefault() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$twig = $this->getTwig(); |
66
|
|
|
|
67
|
|
|
$template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | truncate }}"; |
68
|
|
|
|
69
|
|
|
$twigTemplate = $twig->createTemplate($template); |
70
|
|
|
$this->assertEquals($twigTemplate->render([]), 'Lorem ipsum dolor sit amet, co...'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function testTruncateFilterWithSeparator() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$twig = $this->getTwig(); |
76
|
|
|
|
77
|
|
|
$template = "{{ 'Gordon Freeman' | truncate(5, false, '-') }}"; |
78
|
|
|
|
79
|
|
|
$twigTemplate = $twig->createTemplate($template); |
80
|
|
|
$this->assertEquals($twigTemplate->render([]), 'Gordo-'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function testWordWrapFilter() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$twig = $this->getTwig(); |
86
|
|
|
|
87
|
|
|
$template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | wordwrap(10) }}"; |
88
|
|
|
|
89
|
|
|
$twigTemplate = $twig->createTemplate($template); |
90
|
|
|
$this->assertEquals($twigTemplate->render([]), "Lorem ipsu\nm dolor si\nt amet, co\nnsectetur \nadipiscing\n elit"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testShuffleFilter() |
94
|
|
|
{ |
95
|
|
|
$twig = $this->getTwig(); |
96
|
|
|
|
97
|
|
|
$template = "{{ [1, 2, 3] | shuffle }}"; |
98
|
|
|
|
99
|
|
|
$twigTemplate = $twig->createTemplate($template); |
100
|
|
|
$this->setExpectedException('Twig_Error_Runtime', 'Array to string conversion'); |
101
|
|
|
$twigTemplate->render([]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function testShuffleFilterForeach() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$twig = $this->getTwig(); |
107
|
|
|
|
108
|
|
|
$template = "{% for i in [1, 2, 3] | shuffle %}{{ i }}{% endfor %}"; |
109
|
|
|
|
110
|
|
|
$twigTemplate = $twig->createTemplate($template); |
111
|
|
|
$this->assertEquals(strlen($twigTemplate->render([])), 3); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testTimeDiffFunction() |
115
|
|
|
{ |
116
|
|
|
$twig = $this->getTwig(); |
117
|
|
|
|
118
|
|
|
$now = Carbon::now()->subMinute(); |
119
|
|
|
$template = "{{ '" . $now->format('Y-m-d H:i:s') . "' | time_diff }}"; |
120
|
|
|
|
121
|
|
|
// this test fails at TravisCI and I don't know why |
122
|
|
|
$twigTemplate = $twig->createTemplate($template); |
|
|
|
|
123
|
|
|
// $this->assertEquals($twigTemplate->render([]), '1 minute ago'); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function testStrftimeFunction() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$twig = $this->getTwig(); |
129
|
|
|
|
130
|
|
|
$template = "{{ '2016-03-24 23:05' | strftime('%d.%m.%Y %H:%M:%S') }}"; |
131
|
|
|
|
132
|
|
|
$twigTemplate = $twig->createTemplate($template); |
133
|
|
|
$this->assertEquals($twigTemplate->render([]), '24.03.2016 23:05:00'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
public function testUppercaseFunction() |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$twig = $this->getTwig(); |
139
|
|
|
|
140
|
|
|
$template = "{{ 'Jack' | uppercase }}"; |
141
|
|
|
|
142
|
|
|
$twigTemplate = $twig->createTemplate($template); |
143
|
|
|
$this->assertEquals($twigTemplate->render([]), 'JACK'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
View Code Duplication |
public function testLowercaseFunction() |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$twig = $this->getTwig(); |
149
|
|
|
|
150
|
|
|
$template = "{{ 'JACK' | lowercase }}"; |
151
|
|
|
|
152
|
|
|
$twigTemplate = $twig->createTemplate($template); |
153
|
|
|
$this->assertEquals($twigTemplate->render([]), 'jack'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
View Code Duplication |
public function testUcfirstFunction() |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$twig = $this->getTwig(); |
159
|
|
|
|
160
|
|
|
$template = "{{ 'jack' | ucfirst }}"; |
161
|
|
|
|
162
|
|
|
$twigTemplate = $twig->createTemplate($template); |
163
|
|
|
$this->assertEquals($twigTemplate->render([]), 'Jack'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
public function testLcfirstFunction() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$twig = $this->getTwig(); |
169
|
|
|
|
170
|
|
|
$template = "{{ 'JACK' | lcfirst }}"; |
171
|
|
|
|
172
|
|
|
$twigTemplate = $twig->createTemplate($template); |
173
|
|
|
$this->assertEquals($twigTemplate->render([]), 'jACK'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
public function testLtrimFunction() |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
$twig = $this->getTwig(); |
179
|
|
|
|
180
|
|
|
$template = "{{ ' jack' | ltrim }}"; |
181
|
|
|
|
182
|
|
|
$twigTemplate = $twig->createTemplate($template); |
183
|
|
|
$this->assertEquals($twigTemplate->render([]), 'jack'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
View Code Duplication |
public function testRtrimFunction() |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$twig = $this->getTwig(); |
189
|
|
|
|
190
|
|
|
$template = "{{ 'jack ' | rtrim }}"; |
191
|
|
|
|
192
|
|
|
$twigTemplate = $twig->createTemplate($template); |
193
|
|
|
$this->assertEquals($twigTemplate->render([]), 'jack'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
View Code Duplication |
public function testStrRepeatFunction() |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
$twig = $this->getTwig(); |
199
|
|
|
|
200
|
|
|
$template = "{{ ' best' | str_repeat(3) }}"; |
201
|
|
|
|
202
|
|
|
$twigTemplate = $twig->createTemplate($template); |
203
|
|
|
$this->assertEquals($twigTemplate->render([]), ' best best best'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
View Code Duplication |
public function testPluralFunction() |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
$twig = $this->getTwig(); |
209
|
|
|
|
210
|
|
|
$template = "{{ 'mail' | plural(count) }}"; |
211
|
|
|
|
212
|
|
|
$twigTemplate = $twig->createTemplate($template); |
213
|
|
|
$this->assertEquals($twigTemplate->render([]), 'mails'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
View Code Duplication |
public function testStrpadFunction() |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
$twig = $this->getTwig(); |
219
|
|
|
|
220
|
|
|
$template = "{{ 'test' | strpad(10) }}"; |
221
|
|
|
|
222
|
|
|
$twigTemplate = $twig->createTemplate($template); |
223
|
|
|
$this->assertEquals($twigTemplate->render([]), ' test '); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
View Code Duplication |
public function testStripTagsFunction() |
|
|
|
|
227
|
|
|
{ |
228
|
|
|
$twig = $this->getTwig(); |
229
|
|
|
|
230
|
|
|
$template = "{{ '<p><b>text</b></p>' | strip_tags('<p>') }}"; |
231
|
|
|
|
232
|
|
|
$twigTemplate = $twig->createTemplate($template); |
233
|
|
|
$this->assertEquals($twigTemplate->render([]), '<p>text</p>'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
View Code Duplication |
public function testLeftpadFunction() |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
$twig = $this->getTwig(); |
239
|
|
|
|
240
|
|
|
$template = "{{ 'test' | leftpad(7) }}"; |
241
|
|
|
|
242
|
|
|
$twigTemplate = $twig->createTemplate($template); |
243
|
|
|
$this->assertEquals($twigTemplate->render([]), ' test'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
View Code Duplication |
public function testRightpadFunction() |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
$twig = $this->getTwig(); |
249
|
|
|
|
250
|
|
|
$template = "{{ 'test' | rightpad(7, 'o') }}"; |
251
|
|
|
|
252
|
|
|
$twigTemplate = $twig->createTemplate($template); |
253
|
|
|
$this->assertEquals($twigTemplate->render([]), 'testooo'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
View Code Duplication |
public function testRtlFunction() |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
$twig = $this->getTwig(); |
259
|
|
|
|
260
|
|
|
$template = "{{ 'Hello world!' | rtl }}"; |
261
|
|
|
|
262
|
|
|
$twigTemplate = $twig->createTemplate($template); |
263
|
|
|
$this->assertEquals($twigTemplate->render([]), '!dlrow olleH'); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function testSortByFieldFunction() |
267
|
|
|
{ |
268
|
|
|
$twig = $this->getTwig(); |
269
|
|
|
|
270
|
|
|
// sort by name |
271
|
|
|
$template = "{% set data = [{'name': 'David', 'age': 31}, {'name': 'John', 'age': 28}] %}"; |
272
|
|
|
$template .= "{% for item in data | sortbyfield('name') %}{{ item.name }}{% endfor %}"; |
273
|
|
|
$twigTemplate = $twig->createTemplate($template); |
274
|
|
|
$this->assertEquals($twigTemplate->render([]), 'DavidJohn'); |
275
|
|
|
|
276
|
|
|
// sort by age |
277
|
|
|
$template = "{% set data = [{'name': 'David', 'age': 31}, {'name': 'John', 'age': 28}] %}"; |
278
|
|
|
$template .= "{% for item in data | sortbyfield('age') %}{{ item.name }}{% endfor %}"; |
279
|
|
|
$twigTemplate = $twig->createTemplate($template); |
280
|
|
|
$this->assertEquals($twigTemplate->render([]), 'JohnDavid'); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function testMailtoFilter() |
284
|
|
|
{ |
285
|
|
|
$twig = $this->getTwig(); |
286
|
|
|
|
287
|
|
|
// same as mailto(true, true) |
|
|
|
|
288
|
|
|
$template = "{{ '[email protected]' | mailto }}"; |
289
|
|
|
$twigTemplate = $twig->createTemplate($template); |
290
|
|
|
$this->assertNotContains('[email protected]', $twigTemplate->render([])); |
291
|
|
|
$this->assertContains('mailto:', $twigTemplate->render([])); |
292
|
|
|
|
293
|
|
|
// mailto(false, false) eg. without link and unprotected |
294
|
|
|
$template = "{{ '[email protected]' | mailto(false, false) }}"; |
295
|
|
|
$twigTemplate = $twig->createTemplate($template); |
296
|
|
|
$this->assertContains('[email protected]', $twigTemplate->render([])); |
297
|
|
|
$this->assertNotContains('mailto:', $twigTemplate->render([])); |
298
|
|
|
|
299
|
|
|
// mailto(true, false) eg. with link but unprotected |
300
|
|
|
$template = "{{ '[email protected]' | mailto(true, false) }}"; |
301
|
|
|
$twigTemplate = $twig->createTemplate($template); |
302
|
|
|
$this->assertContains('[email protected]', $twigTemplate->render([])); |
303
|
|
|
$this->assertContains('mailto', $twigTemplate->render([])); |
304
|
|
|
|
305
|
|
|
// mailto(false, true) eg. without link and protected |
|
|
|
|
306
|
|
|
$template = "{{ '[email protected]' | mailto(false, true) }}"; |
307
|
|
|
$twigTemplate = $twig->createTemplate($template); |
308
|
|
|
$this->assertNotContains('[email protected]', $twigTemplate->render([])); |
309
|
|
|
$this->assertNotContains('mailto', $twigTemplate->render([])); |
310
|
|
|
|
311
|
|
|
// mailto(true, true, 'Let me know') eg. with link, protected and with non-crypted text |
312
|
|
|
$template = "{{ '[email protected]' | mailto(false, true, 'Let me know') }}"; |
313
|
|
|
$twigTemplate = $twig->createTemplate($template); |
314
|
|
|
$this->assertNotContains('[email protected]', $twigTemplate->render([])); |
315
|
|
|
$this->assertNotContains('mailto', $twigTemplate->render([])); |
316
|
|
|
$this->assertContains('Let me know', $twigTemplate->render([])); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
public function testVardumpFunction() |
320
|
|
|
{ |
321
|
|
|
$twig = $this->getTwig(); |
322
|
|
|
|
323
|
|
|
$template = "{{ var_dump('test') }}"; |
324
|
|
|
|
325
|
|
|
$twigTemplate = $twig->createTemplate($template); |
326
|
|
|
$this->assertContains('string(4) "test"', $twigTemplate->render([])); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
View Code Duplication |
public function testVardumpFilter() |
|
|
|
|
330
|
|
|
{ |
331
|
|
|
$twig = $this->getTwig(); |
332
|
|
|
|
333
|
|
|
$template = "{{ 'test' | var_dump }}"; |
334
|
|
|
|
335
|
|
|
$twigTemplate = $twig->createTemplate($template); |
336
|
|
|
$this->assertContains('string(4) "test"', $twigTemplate->render([])); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
public function testConfigFunction() |
340
|
|
|
{ |
341
|
|
|
$twig = $this->getTwig(); |
342
|
|
|
|
343
|
|
|
$key = 'app.custom.key'; |
344
|
|
|
$value = 'test value'; |
345
|
|
|
Config::set($key, $value); |
346
|
|
|
$template = "{{ config('" . $key . "') }}"; |
347
|
|
|
|
348
|
|
|
$twigTemplate = $twig->createTemplate($template); |
349
|
|
|
$this->assertEquals($twigTemplate->render([]), $value); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
View Code Duplication |
public function testSessionFunction() |
|
|
|
|
353
|
|
|
{ |
354
|
|
|
$twig = $this->getTwig(); |
355
|
|
|
|
356
|
|
|
session(['my.session.key' => 'test value']); |
357
|
|
|
|
358
|
|
|
$template = "{{ session('my.session.key') }}"; |
359
|
|
|
|
360
|
|
|
$twigTemplate = $twig->createTemplate($template); |
361
|
|
|
$this->assertEquals($twigTemplate->render([]), 'test value'); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
View Code Duplication |
public function testTransFunction() |
|
|
|
|
365
|
|
|
{ |
366
|
|
|
$twig = $this->getTwig(); |
367
|
|
|
Config::set('app.locale', 'en'); |
368
|
|
|
|
369
|
|
|
$template = "{{ trans('validation.accepted') }}"; |
370
|
|
|
|
371
|
|
|
$twigTemplate = $twig->createTemplate($template); |
372
|
|
|
$this->assertEquals($twigTemplate->render([]), 'The :attribute must be accepted.'); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
View Code Duplication |
public function testTransFunctionWithParam() |
|
|
|
|
376
|
|
|
{ |
377
|
|
|
$twig = $this->getTwig(); |
378
|
|
|
Config::set('app.locale', 'en'); |
379
|
|
|
|
380
|
|
|
$template = "{{ trans('backend::lang.access_log.hint', {'days': 60}) }}"; |
381
|
|
|
|
382
|
|
|
$twigTemplate = $twig->createTemplate($template); |
383
|
|
|
$this->assertContains('60 days', $twigTemplate->render([])); |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.