Completed
Push — master ( e829a4...a4554a )
by Vojta
09:22
created

PluginTest::testRtlFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 11.

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.

Loading history...
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
11
require_once __DIR__ . '/../vendor/autoload.php';
12
13
class PluginTest extends PluginTestCase
14
{
15
    /**
16
     * Return Twig environment
17
     * 
18
     * @return Twig_Environment
19
     */
20
    private function getTwig()
21
    {
22
        return App::make('twig.environment');
23
    }
24
25 View Code Duplication
    public function testTemplateFromStringFunction()
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...
26
    {
27
        $twig = $this->getTwig();
28
29
        $template = "{% set name = 'John' %}";
30
        $template .= '{{ include(template_from_string("Hello {{ name }}")) }}';
31
32
        $twigTemplate = $twig->createTemplate($template);
33
        $this->assertEquals($twigTemplate->render([]), 'Hello John');
34
    }
35
36 View Code Duplication
    public function testTruncateFilterForFive()
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...
37
    {
38
        $twig = $this->getTwig();
39
40
        $template = "{{ 'Gordon Freeman' | truncate(5) }}";
41
42
        $twigTemplate = $twig->createTemplate($template);
43
        $this->assertEquals($twigTemplate->render([]), 'Gordo...');
44
    }
45
46 View Code Duplication
    public function testTruncateFilterForDefault()
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...
47
    {
48
        $twig = $this->getTwig();
49
50
        $template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | truncate }}";
51
52
        $twigTemplate = $twig->createTemplate($template);
53
        $this->assertEquals($twigTemplate->render([]), 'Lorem ipsum dolor sit amet, co...');
54
    }
55
56 View Code Duplication
    public function testTruncateFilterWithSeparator()
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...
57
    {
58
        $twig = $this->getTwig();
59
60
        $template = "{{ 'Gordon Freeman' | truncate(5, false, '-') }}";
61
62
        $twigTemplate = $twig->createTemplate($template);
63
        $this->assertEquals($twigTemplate->render([]), 'Gordo-');
64
    }
65
66 View Code Duplication
    public function testWordWrapFilter()
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...
67
    {
68
        $twig = $this->getTwig();
69
70
        $template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | wordwrap(10) }}";
71
72
        $twigTemplate = $twig->createTemplate($template);
73
        $this->assertEquals($twigTemplate->render([]), "Lorem ipsu\nm dolor si\nt amet, co\nnsectetur \nadipiscing\n elit");
74
    }
75
76
    public function testShuffleFilter()
77
    {
78
        $twig = $this->getTwig();
79
80
        $template = "{{ [1, 2, 3] | shuffle }}";
81
82
        $twigTemplate = $twig->createTemplate($template);
83
        $this->setExpectedException('Twig_Error_Runtime', 'Array to string conversion');
84
        $twigTemplate->render([]);
85
    }
86
87 View Code Duplication
    public function testShuffleFilterForeach()
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...
88
    {
89
        $twig = $this->getTwig();
90
91
        $template = "{% for i in [1, 2, 3] | shuffle %}{{ i }}{% endfor %}";
92
93
        $twigTemplate = $twig->createTemplate($template);
94
        $this->assertEquals(strlen($twigTemplate->render([])), 3);
95
    }
96
97
    public function testTimeDiffFunction()
98
    {
99
        $twig = $this->getTwig();
100
101
        $now = Carbon::now()->subMinute();
102
        $template = "{{ '" . $now->format('Y-m-d H:i:s') . "' | time_diff }}";
103
104
        $twigTemplate = $twig->createTemplate($template);
105
        $this->assertEquals($twigTemplate->render([]), '1 minute ago');
106
    }
107
108 View Code Duplication
    public function testStrftimeFunction()
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...
109
    {
110
        $twig = $this->getTwig();
111
112
        $template = "{{ '2016-03-24 23:05' | strftime('%d.%m.%Y %H:%M:%S') }}";
113
114
        $twigTemplate = $twig->createTemplate($template);
115
        $this->assertEquals($twigTemplate->render([]), '24.03.2016 23:05:00');
116
    }
117
118 View Code Duplication
    public function testUppercaseFunction()
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...
119
    {
120
        $twig = $this->getTwig();
121
122
        $template = "{{ 'Jack' | uppercase }}";
123
124
        $twigTemplate = $twig->createTemplate($template);
125
        $this->assertEquals($twigTemplate->render([]), 'JACK');
126
    }
127
128 View Code Duplication
    public function testLowercaseFunction()
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...
129
    {
130
        $twig = $this->getTwig();
131
132
        $template = "{{ 'JACK' | lowercase }}";
133
134
        $twigTemplate = $twig->createTemplate($template);
135
        $this->assertEquals($twigTemplate->render([]), 'jack');
136
    }
137
138 View Code Duplication
    public function testUcfirstFunction()
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...
139
    {
140
        $twig = $this->getTwig();
141
142
        $template = "{{ 'jack' | ucfirst }}";
143
144
        $twigTemplate = $twig->createTemplate($template);
145
        $this->assertEquals($twigTemplate->render([]), 'Jack');
146
    }
147
148 View Code Duplication
    public function testLcfirstFunction()
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...
149
    {
150
        $twig = $this->getTwig();
151
152
        $template = "{{ 'JACK' | lcfirst }}";
153
154
        $twigTemplate = $twig->createTemplate($template);
155
        $this->assertEquals($twigTemplate->render([]), 'jACK');
156
    }
157
158 View Code Duplication
    public function testLtrimFunction()
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...
159
    {
160
        $twig = $this->getTwig();
161
162
        $template = "{{ ' jack' | ltrim }}";
163
164
        $twigTemplate = $twig->createTemplate($template);
165
        $this->assertEquals($twigTemplate->render([]), 'jack');
166
    }
167
168 View Code Duplication
    public function testRtrimFunction()
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...
169
    {
170
        $twig = $this->getTwig();
171
172
        $template = "{{ 'jack ' | rtrim }}";
173
174
        $twigTemplate = $twig->createTemplate($template);
175
        $this->assertEquals($twigTemplate->render([]), 'jack');
176
    }
177
178 View Code Duplication
    public function testStrRepeatFunction()
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...
179
    {
180
        $twig = $this->getTwig();
181
182
        $template = "{{ ' best' | str_repeat(3) }}";
183
184
        $twigTemplate = $twig->createTemplate($template);
185
        $this->assertEquals($twigTemplate->render([]), ' best best best');
186
    }
187
188 View Code Duplication
    public function testPluralFunction()
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...
189
    {
190
        $twig = $this->getTwig();
191
192
        $template = "{{ 'mail' | plural(count) }}";
193
194
        $twigTemplate = $twig->createTemplate($template);
195
        $this->assertEquals($twigTemplate->render([]), 'mails');
196
    }
197
198 View Code Duplication
    public function testStrpadFunction()
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...
199
    {
200
        $twig = $this->getTwig();
201
202
        $template = "{{ 'test' | strpad(10) }}";
203
204
        $twigTemplate = $twig->createTemplate($template);
205
        $this->assertEquals($twigTemplate->render([]), '   test   ');
206
    }
207
208 View Code Duplication
    public function testLeftpadFunction()
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...
209
    {
210
        $twig = $this->getTwig();
211
212
        $template = "{{ 'test' | leftpad(7) }}";
213
214
        $twigTemplate = $twig->createTemplate($template);
215
        $this->assertEquals($twigTemplate->render([]), '   test');
216
    }
217
218 View Code Duplication
    public function testRightpadFunction()
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...
219
    {
220
        $twig = $this->getTwig();
221
222
        $template = "{{ 'test' | rightpad(7, 'o') }}";
223
224
        $twigTemplate = $twig->createTemplate($template);
225
        $this->assertEquals($twigTemplate->render([]), 'testooo');
226
    }
227
228 View Code Duplication
    public function testRtlFunction()
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...
229
    {
230
        $twig = $this->getTwig();
231
232
        $template = "{{ 'Hello world!' | rtl }}";
233
234
        $twigTemplate = $twig->createTemplate($template);
235
        $this->assertEquals($twigTemplate->render([]), '!dlrow olleH');
236
    }
237
238
    public function testMailtoFilter()
239
    {
240
        $twig = $this->getTwig();
241
242
        // same as mailto(true, true)
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
243
        $template = "{{ '[email protected]' | mailto }}";
244
        $twigTemplate = $twig->createTemplate($template);
245
        $this->assertNotContains('[email protected]', $twigTemplate->render([]));
246
        $this->assertContains('mailto:', $twigTemplate->render([]));
247
248
        // mailto(false, false) eg. without link and unprotected
249
        $template = "{{ '[email protected]' | mailto(false, false) }}";
250
        $twigTemplate = $twig->createTemplate($template);
251
        $this->assertContains('[email protected]', $twigTemplate->render([]));
252
        $this->assertNotContains('mailto:', $twigTemplate->render([]));
253
254
        // mailto(true, false) eg. with link but unprotected
255
        $template = "{{ '[email protected]' | mailto(true, false) }}";
256
        $twigTemplate = $twig->createTemplate($template);
257
        $this->assertContains('[email protected]', $twigTemplate->render([]));
258
        $this->assertContains('mailto', $twigTemplate->render([]));
259
260
        // mailto(false, true) eg. without link and protected
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
261
        $template = "{{ '[email protected]' | mailto(false, true) }}";
262
        $twigTemplate = $twig->createTemplate($template);
263
        $this->assertNotContains('[email protected]', $twigTemplate->render([]));
264
        $this->assertNotContains('mailto', $twigTemplate->render([]));
265
266
        // mailto(true, true, 'Let me know') eg. with link, protected and with non-crypted text
267
        $template = "{{ '[email protected]' | mailto(false, true, 'Let me know') }}";
268
        $twigTemplate = $twig->createTemplate($template);
269
        $this->assertNotContains('[email protected]', $twigTemplate->render([]));
270
        $this->assertNotContains('mailto', $twigTemplate->render([]));
271
        $this->assertContains('Let me know', $twigTemplate->render([]));
272
    }
273
274
    public function testVardumpFunction()
275
    {
276
        $twig = $this->getTwig();
277
278
        $template = "{{ var_dump('test') }}";
279
280
        $twigTemplate = $twig->createTemplate($template);
281
        $this->assertContains('string(4) "test"', $twigTemplate->render([]));
282
    }
283
284 View Code Duplication
    public function testVardumpFilter()
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...
285
    {
286
        $twig = $this->getTwig();
287
288
        $template = "{{ 'test' | var_dump }}";
289
290
        $twigTemplate = $twig->createTemplate($template);
291
        $this->assertContains('string(4) "test"', $twigTemplate->render([]));
292
    }
293
294
    public function testConfigFunction()
295
    {
296
        $twig = $this->getTwig();
297
298
        $key = 'app.custom.key';
299
        $value = 'test value';
300
        Config::set($key, $value);
301
        $template = "{{ config('" . $key . "') }}";
302
303
        $twigTemplate = $twig->createTemplate($template);
304
        $this->assertEquals($twigTemplate->render([]), $value);
305
    }
306
307 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...
308
    {
309
        $twig = $this->getTwig();
310
311
        session(['my.session.key' => 'test value']);
312
313
        $template = "{{ session('my.session.key') }}";
314
315
        $twigTemplate = $twig->createTemplate($template);
316
        $this->assertEquals($twigTemplate->render([]), 'test value');
317
    }
318
319 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...
320
    {
321
        $twig = $this->getTwig();
322
        Config::set('app.locale', 'en');
323
        
324
        $template = "{{ trans('validation.accepted') }}";
325
326
        $twigTemplate = $twig->createTemplate($template);
327
        $this->assertEquals($twigTemplate->render([]), 'The :attribute must be accepted.');
328
    }
329
}
330