Test Failed
Pull Request — master (#52)
by
unknown
07:46
created

PluginTest::testEnvFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php namespace VojtaSvoboda\TwigExtensions\Tests;
2
3
use App;
4
use Carbon\Carbon;
5
use Config;
6
use PluginTestCase;
7
use Twig_Environment;
8
use VojtaSvoboda\TwigExtensions\Classes\TimeDiffTranslator;
9
10
require_once __DIR__ . '/../vendor/autoload.php';
11
12
class PluginTest extends PluginTestCase
13
{
14 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
    {
16
        parent::setUp();
17
18
        $this->app->setLocale('en');
19
20
        $this->app->singleton('time_diff_translator', function ($app) {
21
            $loader = $app->make('translation.loader');
22
            $locale = $app->config->get('app.locale');
23
            $translator = $app->make(TimeDiffTranslator::class, [$loader, $locale]);
24
            $translator->setFallback($app->config->get('app.fallback_locale'));
25
26
            return $translator;
27
        });
28
    }
29
30
    /**
31
     * Return Twig environment
32
     *
33
     * @return Twig_Environment
34
     */
35
    private function getTwig()
36
    {
37
        return App::make('twig.environment');
38
    }
39
40
    public function testTemplateFromStringFunction()
41
    {
42
        $twig = $this->getTwig();
43
44
        $template = "{% set name = 'John' %}";
45
        $template .= '{{ include(template_from_string("Hello {{ name }}")) }}';
46
47
        $twigTemplate = $twig->createTemplate($template);
48
        $this->assertEquals($twigTemplate->render([]), 'Hello John');
49
    }
50
51
    public function testTruncateFilterForFive()
52
    {
53
        $twig = $this->getTwig();
54
55
        $template = "{{ 'Gordon Freeman' | truncate(5) }}";
56
57
        $twigTemplate = $twig->createTemplate($template);
58
        $this->assertEquals($twigTemplate->render([]), 'Gordo...');
59
    }
60
61
    public function testTruncateFilterForDefault()
62
    {
63
        $twig = $this->getTwig();
64
65
        $template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | truncate }}";
66
67
        $twigTemplate = $twig->createTemplate($template);
68
        $this->assertEquals($twigTemplate->render([]), 'Lorem ipsum dolor sit amet, co...');
69
    }
70
71
    public function testTruncateFilterWithSeparator()
72
    {
73
        $twig = $this->getTwig();
74
75
        $template = "{{ 'Gordon Freeman' | truncate(5, false, '-') }}";
76
77
        $twigTemplate = $twig->createTemplate($template);
78
        $this->assertEquals($twigTemplate->render([]), 'Gordo-');
79
    }
80
81
    public function testWordWrapFilter()
82
    {
83
        $twig = $this->getTwig();
84
85
        $template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | wordwrap(10) }}";
86
87
        $twigTemplate = $twig->createTemplate($template);
88
        $this->assertEquals($twigTemplate->render([]), "Lorem ipsu\nm dolor si\nt amet, co\nnsectetur \nadipiscing\n elit");
89
    }
90
91
    public function testShuffleFilter()
92
    {
93
        $twig = $this->getTwig();
94
95
        $template = "{{ [1, 2, 3] | shuffle }}";
96
97
        $twigTemplate = $twig->createTemplate($template);
98
        $this->setExpectedException('Twig_Error_Runtime', 'Array to string conversion');
99
        $twigTemplate->render([]);
100
    }
101
102
    public function testShuffleFilterForeach()
103
    {
104
        $twig = $this->getTwig();
105
106
        $template = "{% for i in [1, 2, 3] | shuffle %}{{ i }}{% endfor %}";
107
108
        $twigTemplate = $twig->createTemplate($template);
109
        $this->assertEquals(strlen($twigTemplate->render([])), 3);
110
    }
111
112
    public function testTimeDiffFunction()
113
    {
114
        $twig = $this->getTwig();
115
116
        $now = Carbon::now()->subMinute();
117
        $template = "{{ '" . $now->format('Y-m-d H:i:s') . "' | time_diff }}";
118
119
        // this test fails at TravisCI and I don't know why
120
        $twigTemplate = $twig->createTemplate($template);
0 ignored issues
show
Unused Code introduced by
$twigTemplate is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
121
        // $this->assertEquals($twigTemplate->render([]), '1 minute ago');
122
    }
123
124
    public function testStrftimeFunction()
125
    {
126
        $twig = $this->getTwig();
127
128
        $template = "{{ '2016-03-24 23:05' | strftime('%d.%m.%Y %H:%M:%S') }}";
129
130
        $twigTemplate = $twig->createTemplate($template);
131
        $this->assertEquals($twigTemplate->render([]), '24.03.2016 23:05:00');
132
    }
133
134
    public function testUppercaseFunction()
135
    {
136
        $twig = $this->getTwig();
137
138
        $template = "{{ 'Jack' | uppercase }}";
139
140
        $twigTemplate = $twig->createTemplate($template);
141
        $this->assertEquals($twigTemplate->render([]), 'JACK');
142
    }
143
144
    public function testLowercaseFunction()
145
    {
146
        $twig = $this->getTwig();
147
148
        $template = "{{ 'JACK' | lowercase }}";
149
150
        $twigTemplate = $twig->createTemplate($template);
151
        $this->assertEquals($twigTemplate->render([]), 'jack');
152
    }
153
154
    public function testUcfirstFunction()
155
    {
156
        $twig = $this->getTwig();
157
158
        $template = "{{ 'jack' | ucfirst }}";
159
160
        $twigTemplate = $twig->createTemplate($template);
161
        $this->assertEquals($twigTemplate->render([]), 'Jack');
162
    }
163
164
    public function testLcfirstFunction()
165
    {
166
        $twig = $this->getTwig();
167
168
        $template = "{{ 'JACK' | lcfirst }}";
169
170
        $twigTemplate = $twig->createTemplate($template);
171
        $this->assertEquals($twigTemplate->render([]), 'jACK');
172
    }
173
174
    public function testLtrimFunction()
175
    {
176
        $twig = $this->getTwig();
177
178
        $template = "{{ ' jack' | ltrim }}";
179
180
        $twigTemplate = $twig->createTemplate($template);
181
        $this->assertEquals($twigTemplate->render([]), 'jack');
182
    }
183
184
    public function testRtrimFunction()
185
    {
186
        $twig = $this->getTwig();
187
188
        $template = "{{ 'jack ' | rtrim }}";
189
190
        $twigTemplate = $twig->createTemplate($template);
191
        $this->assertEquals($twigTemplate->render([]), 'jack');
192
    }
193
194
    public function testStrRepeatFunction()
195
    {
196
        $twig = $this->getTwig();
197
198
        $template = "{{ ' best' | str_repeat(3) }}";
199
200
        $twigTemplate = $twig->createTemplate($template);
201
        $this->assertEquals($twigTemplate->render([]), ' best best best');
202
    }
203
204
    public function testPluralFunction()
205
    {
206
        $twig = $this->getTwig();
207
208
        $template = "{{ 'mail' | plural(count) }}";
209
210
        $twigTemplate = $twig->createTemplate($template);
211
        $this->assertEquals($twigTemplate->render([]), 'mails');
212
    }
213
214
    public function testStrpadFunction()
215
    {
216
        $twig = $this->getTwig();
217
218
        $template = "{{ 'test' | strpad(10) }}";
219
220
        $twigTemplate = $twig->createTemplate($template);
221
        $this->assertEquals($twigTemplate->render([]), '   test   ');
222
    }
223
224
    public function testStrReplaceFunction()
225
    {
226
        $twig = $this->getTwig();
227
228
        $template = "{{ 'test' | str_replace('test', 'tset') }}";
229
230
        $twigTemplate = $twig->createTemplate($template);
231
        $this->assertEquals($twigTemplate->render([]), 'tset');
232
    }
233
234
    public function testStripTagsFunction()
235
    {
236
        $twig = $this->getTwig();
237
238
        $template = "{{ '<p><b>text</b></p>' | strip_tags('<p>') }}";
239
240
        $twigTemplate = $twig->createTemplate($template);
241
        $this->assertEquals($twigTemplate->render([]), '<p>text</p>');
242
    }
243
244
    public function testLeftpadFunction()
245
    {
246
        $twig = $this->getTwig();
247
248
        $template = "{{ 'test' | leftpad(7) }}";
249
250
        $twigTemplate = $twig->createTemplate($template);
251
        $this->assertEquals($twigTemplate->render([]), '   test');
252
    }
253
254
    public function testRightpadFunction()
255
    {
256
        $twig = $this->getTwig();
257
258
        $template = "{{ 'test' | rightpad(7, 'o') }}";
259
260
        $twigTemplate = $twig->createTemplate($template);
261
        $this->assertEquals($twigTemplate->render([]), 'testooo');
262
    }
263
264
    public function testRtlFunction()
265
    {
266
        $twig = $this->getTwig();
267
268
        $template = "{{ 'Hello world!' | rtl }}";
269
270
        $twigTemplate = $twig->createTemplate($template);
271
        $this->assertEquals($twigTemplate->render([]), '!dlrow olleH');
272
    }
273
274
    public function testSortByFieldFunction()
275
    {
276
        $twig = $this->getTwig();
277
278
        // sort by name
279
        $template = "{% set data = [{'name': 'David', 'age': 31}, {'name': 'John', 'age': 28}] %}";
280
        $template .= "{% for item in data | sortbyfield('name') %}{{ item.name }}{% endfor %}";
281
        $twigTemplate = $twig->createTemplate($template);
282
        $this->assertEquals($twigTemplate->render([]), 'DavidJohn');
283
284
        // sort by age
285
        $template = "{% set data = [{'name': 'David', 'age': 31}, {'name': 'John', 'age': 28}] %}";
286
        $template .= "{% for item in data | sortbyfield('age') %}{{ item.name }}{% endfor %}";
287
        $twigTemplate = $twig->createTemplate($template);
288
        $this->assertEquals($twigTemplate->render([]), 'JohnDavid');
289
    }
290
291
    public function testMailtoFilter()
292
    {
293
        $twig = $this->getTwig();
294
295
        // same as mailto(true, true)
296
        $template = "{{ '[email protected]' | mailto }}";
297
        $twigTemplate = $twig->createTemplate($template);
298
        $this->assertNotContains('[email protected]', $twigTemplate->render([]));
299
        $this->assertContains('mailto:', $twigTemplate->render([]));
300
301
        // mailto(false, false) eg. without link and unprotected
302
        $template = "{{ '[email protected]' | mailto(false, false) }}";
303
        $twigTemplate = $twig->createTemplate($template);
304
        $this->assertContains('[email protected]', $twigTemplate->render([]));
305
        $this->assertNotContains('mailto:', $twigTemplate->render([]));
306
307
        // mailto(true, false) eg. with link but unprotected
308
        $template = "{{ '[email protected]' | mailto(true, false) }}";
309
        $twigTemplate = $twig->createTemplate($template);
310
        $this->assertContains('[email protected]', $twigTemplate->render([]));
311
        $this->assertContains('mailto', $twigTemplate->render([]));
312
313
        // mailto(false, true) eg. without link and protected
314
        $template = "{{ '[email protected]' | mailto(false, true) }}";
315
        $twigTemplate = $twig->createTemplate($template);
316
        $this->assertNotContains('[email protected]', $twigTemplate->render([]));
317
        $this->assertNotContains('mailto', $twigTemplate->render([]));
318
319
        // mailto(true, true, 'Let me know') eg. with link, protected and with non-crypted text
320
        $template = "{{ '[email protected]' | mailto(false, true, 'Let me know') }}";
321
        $twigTemplate = $twig->createTemplate($template);
322
        $this->assertNotContains('[email protected]', $twigTemplate->render([]));
323
        $this->assertNotContains('mailto', $twigTemplate->render([]));
324
        $this->assertContains('Let me know', $twigTemplate->render([]));
325
    }
326
327
    public function testVardumpFunction()
328
    {
329
        $twig = $this->getTwig();
330
331
        $template = "{{ var_dump('test') }}";
332
333
        $twigTemplate = $twig->createTemplate($template);
334
        $this->assertContains('string(4) "test"', $twigTemplate->render([]));
335
    }
336
337
    public function testVardumpFilter()
338
    {
339
        $twig = $this->getTwig();
340
341
        $template = "{{ 'test' | var_dump }}";
342
343
        $twigTemplate = $twig->createTemplate($template);
344
        $this->assertContains('string(4) "test"', $twigTemplate->render([]));
345
    }
346
347 View Code Duplication
    public function testConfigFunction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
348
    {
349
        $twig = $this->getTwig();
350
351
        $key = 'app.custom.key';
352
        $value = 'test value';
353
        Config::set($key, $value);
354
        $template = "{{ config('" . $key . "') }}";
355
356
        $twigTemplate = $twig->createTemplate($template);
357
        $this->assertEquals($twigTemplate->render([]), $value);
358
    }
359
360 View Code Duplication
    public function testEnvFunction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
361
    {
362
        $twig = $this->getTwig();
363
364
        $key = 'env.custom.key';
365
        $value = 'test value';
366
        putenv($key.'='.$value);
367
        $template = "{{ env('" . $key . "') }}";
368
369
        $twigTemplate = $twig->createTemplate($template);
370
        $this->assertEquals($twigTemplate->render([]), $value);
371
    }
372
373 View Code Duplication
    public function testSessionFunction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
374
    {
375
        $twig = $this->getTwig();
376
377
        session(['my.session.key' => 'test value']);
378
379
        $template = "{{ session('my.session.key') }}";
380
381
        $twigTemplate = $twig->createTemplate($template);
382
        $this->assertEquals($twigTemplate->render([]), 'test value');
383
    }
384
385 View Code Duplication
    public function testTransFunction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
386
    {
387
        $twig = $this->getTwig();
388
        Config::set('app.locale', 'en');
389
390
        $template = "{{ trans('validation.accepted') }}";
391
392
        $twigTemplate = $twig->createTemplate($template);
393
        $this->assertEquals($twigTemplate->render([]), 'The :attribute must be accepted.');
394
    }
395
396 View Code Duplication
    public function testTransFunctionWithParam()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
397
    {
398
        $twig = $this->getTwig();
399
        Config::set('app.locale', 'en');
400
401
        $template = "{{ trans('backend::lang.access_log.hint', {'days': 60}) }}";
402
403
        $twigTemplate = $twig->createTemplate($template);
404
        $this->assertContains('60 days', $twigTemplate->render([]));
405
    }
406
}
407