Completed
Pull Request — master (#28)
by Vojta
10:05
created

PluginTest::testSortByFieldFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
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 14 and the first side effect is on line 12.

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