Completed
Pull Request — master (#41)
by Vojta
07:17
created

PluginTest   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 372
Duplicated Lines 12.37 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 32
lcom 1
cbo 2
dl 46
loc 372
rs 9.6
c 1
b 0
f 0

32 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 15 15 1
A getTwig() 0 4 1
A testTemplateFromStringFunction() 0 10 1
A testTruncateFilterForFive() 0 9 1
A testTruncateFilterForDefault() 0 9 1
A testTruncateFilterWithSeparator() 0 9 1
A testWordWrapFilter() 0 9 1
A testShuffleFilter() 0 10 1
A testShuffleFilterForeach() 0 9 1
A testTimeDiffFunction() 0 11 1
A testStrftimeFunction() 0 9 1
A testUppercaseFunction() 0 9 1
A testLowercaseFunction() 0 9 1
A testUcfirstFunction() 0 9 1
A testLcfirstFunction() 0 9 1
A testLtrimFunction() 0 9 1
A testRtrimFunction() 0 9 1
A testStrRepeatFunction() 0 9 1
A testPluralFunction() 0 9 1
A testStrpadFunction() 0 9 1
A testStripTagsFunction() 0 9 1
A testLeftpadFunction() 0 9 1
A testRightpadFunction() 0 9 1
A testRtlFunction() 0 9 1
A testSortByFieldFunction() 0 16 1
B testMailtoFilter() 0 35 1
A testVardumpFunction() 0 9 1
A testVardumpFilter() 0 9 1
A testConfigFunction() 0 12 1
A testSessionFunction() 11 11 1
A testTransFunction() 10 10 1
A testTransFunctionWithParam() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace VojtaSvoboda\TwigExtensions\Tests;
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 12 and the first side effect is on line 10.

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
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');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
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 testStripTagsFunction()
225
    {
226
        $twig = $this->getTwig();
227
228
        $template = "{{ '<p><b>text</b></p>' | strip_tags('<p>') }}";
229
230
        $twigTemplate = $twig->createTemplate($template);
231
        $this->assertEquals($twigTemplate->render([]), '<p>text</p>');
232
    }
233
234
    public function testLeftpadFunction()
235
    {
236
        $twig = $this->getTwig();
237
238
        $template = "{{ 'test' | leftpad(7) }}";
239
240
        $twigTemplate = $twig->createTemplate($template);
241
        $this->assertEquals($twigTemplate->render([]), '   test');
242
    }
243
244
    public function testRightpadFunction()
245
    {
246
        $twig = $this->getTwig();
247
248
        $template = "{{ 'test' | rightpad(7, 'o') }}";
249
250
        $twigTemplate = $twig->createTemplate($template);
251
        $this->assertEquals($twigTemplate->render([]), 'testooo');
252
    }
253
254
    public function testRtlFunction()
255
    {
256
        $twig = $this->getTwig();
257
258
        $template = "{{ 'Hello world!' | rtl }}";
259
260
        $twigTemplate = $twig->createTemplate($template);
261
        $this->assertEquals($twigTemplate->render([]), '!dlrow olleH');
262
    }
263
264
    public function testSortByFieldFunction()
265
    {
266
        $twig = $this->getTwig();
267
268
        // sort by name
269
        $template = "{% set data = [{'name': 'David', 'age': 31}, {'name': 'John', 'age': 28}] %}";
270
        $template .= "{% for item in data | sortbyfield('name') %}{{ item.name }}{% endfor %}";
271
        $twigTemplate = $twig->createTemplate($template);
272
        $this->assertEquals($twigTemplate->render([]), 'DavidJohn');
273
274
        // sort by age
275
        $template = "{% set data = [{'name': 'David', 'age': 31}, {'name': 'John', 'age': 28}] %}";
276
        $template .= "{% for item in data | sortbyfield('age') %}{{ item.name }}{% endfor %}";
277
        $twigTemplate = $twig->createTemplate($template);
278
        $this->assertEquals($twigTemplate->render([]), 'JohnDavid');
279
    }
280
281
    public function testMailtoFilter()
282
    {
283
        $twig = $this->getTwig();
284
285
        // 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...
286
        $template = "{{ '[email protected]' | mailto }}";
287
        $twigTemplate = $twig->createTemplate($template);
288
        $this->assertNotContains('[email protected]', $twigTemplate->render([]));
289
        $this->assertContains('mailto:', $twigTemplate->render([]));
290
291
        // mailto(false, false) eg. without link and unprotected
292
        $template = "{{ '[email protected]' | mailto(false, false) }}";
293
        $twigTemplate = $twig->createTemplate($template);
294
        $this->assertContains('[email protected]', $twigTemplate->render([]));
295
        $this->assertNotContains('mailto:', $twigTemplate->render([]));
296
297
        // mailto(true, false) eg. with link but unprotected
298
        $template = "{{ '[email protected]' | mailto(true, false) }}";
299
        $twigTemplate = $twig->createTemplate($template);
300
        $this->assertContains('[email protected]', $twigTemplate->render([]));
301
        $this->assertContains('mailto', $twigTemplate->render([]));
302
303
        // 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...
304
        $template = "{{ '[email protected]' | mailto(false, true) }}";
305
        $twigTemplate = $twig->createTemplate($template);
306
        $this->assertNotContains('[email protected]', $twigTemplate->render([]));
307
        $this->assertNotContains('mailto', $twigTemplate->render([]));
308
309
        // mailto(true, true, 'Let me know') eg. with link, protected and with non-crypted text
310
        $template = "{{ '[email protected]' | mailto(false, true, 'Let me know') }}";
311
        $twigTemplate = $twig->createTemplate($template);
312
        $this->assertNotContains('[email protected]', $twigTemplate->render([]));
313
        $this->assertNotContains('mailto', $twigTemplate->render([]));
314
        $this->assertContains('Let me know', $twigTemplate->render([]));
315
    }
316
317
    public function testVardumpFunction()
318
    {
319
        $twig = $this->getTwig();
320
321
        $template = "{{ var_dump('test') }}";
322
323
        $twigTemplate = $twig->createTemplate($template);
324
        $this->assertContains('string(4) "test"', $twigTemplate->render([]));
325
    }
326
327
    public function testVardumpFilter()
328
    {
329
        $twig = $this->getTwig();
330
331
        $template = "{{ 'test' | var_dump }}";
332
333
        $twigTemplate = $twig->createTemplate($template);
334
        $this->assertContains('string(4) "test"', $twigTemplate->render([]));
335
    }
336
337
    public function testConfigFunction()
338
    {
339
        $twig = $this->getTwig();
340
341
        $key = 'app.custom.key';
342
        $value = 'test value';
343
        Config::set($key, $value);
344
        $template = "{{ config('" . $key . "') }}";
345
346
        $twigTemplate = $twig->createTemplate($template);
347
        $this->assertEquals($twigTemplate->render([]), $value);
348
    }
349
350 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...
351
    {
352
        $twig = $this->getTwig();
353
354
        session(['my.session.key' => 'test value']);
355
356
        $template = "{{ session('my.session.key') }}";
357
358
        $twigTemplate = $twig->createTemplate($template);
359
        $this->assertEquals($twigTemplate->render([]), 'test value');
360
    }
361
362 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...
363
    {
364
        $twig = $this->getTwig();
365
        Config::set('app.locale', 'en');
366
367
        $template = "{{ trans('validation.accepted') }}";
368
369
        $twigTemplate = $twig->createTemplate($template);
370
        $this->assertEquals($twigTemplate->render([]), 'The :attribute must be accepted.');
371
    }
372
373 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...
374
    {
375
        $twig = $this->getTwig();
376
        Config::set('app.locale', 'en');
377
378
        $template = "{{ trans('backend::lang.access_log.hint', {'days': 60}) }}";
379
380
        $twigTemplate = $twig->createTemplate($template);
381
        $this->assertContains('60 days', $twigTemplate->render([]));
382
    }
383
}
384