Completed
Push — master ( 9e0190...307824 )
by Vojta
07:24
created

PluginTest::testTemplateFromStringFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 10
loc 10
rs 9.4285
cc 1
eloc 6
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 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
namespace VojtaSvoboda\TwigExtensions\Tests;
4
5
use App;
6
use Carbon\Carbon;
7
use PluginTestCase;
8
use Twig_Environment;
9
10
require_once __DIR__ . '/../vendor/autoload.php';
11
12
class PluginTest extends PluginTestCase
13
{
14
    /**
15
     * Return Twig environment
16
     * 
17
     * @return Twig_Environment
18
     */
19
    private function getTwig()
20
    {
21
        return App::make('twig.environment');
22
    }
23
24 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...
25
    {
26
        $twig = $this->getTwig();
27
28
        $template = "{% set name = 'John' %}";
29
        $template .= '{{ include(template_from_string("Hello {{ name }}")) }}';
30
31
        $twigTemplate = $twig->createTemplate($template);
32
        $this->assertEquals($twigTemplate->render([]), 'Hello John');
33
    }
34
35 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...
36
    {
37
        $twig = $this->getTwig();
38
39
        $template = "{{ 'Gordon Freeman' | truncate(5) }}";
40
41
        $twigTemplate = $twig->createTemplate($template);
42
        $this->assertEquals($twigTemplate->render([]), 'Gordo...');
43
    }
44
45 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...
46
    {
47
        $twig = $this->getTwig();
48
49
        $template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | truncate }}";
50
51
        $twigTemplate = $twig->createTemplate($template);
52
        $this->assertEquals($twigTemplate->render([]), 'Lorem ipsum dolor sit amet, co...');
53
    }
54
55 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...
56
    {
57
        $twig = $this->getTwig();
58
59
        $template = "{{ 'Gordon Freeman' | truncate(5, false, '-') }}";
60
61
        $twigTemplate = $twig->createTemplate($template);
62
        $this->assertEquals($twigTemplate->render([]), 'Gordo-');
63
    }
64
65 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...
66
    {
67
        $twig = $this->getTwig();
68
69
        $template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | wordwrap(10) }}";
70
71
        $twigTemplate = $twig->createTemplate($template);
72
        $this->assertEquals($twigTemplate->render([]), "Lorem ipsu\nm dolor si\nt amet, co\nnsectetur \nadipiscing\n elit");
73
    }
74
75 View Code Duplication
    public function testShuffleFilter()
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...
76
    {
77
        $twig = $this->getTwig();
78
79
        $template = "{{ [1, 2, 3] | shuffle }}";
80
81
        // throws shuffle() exception, so filter is loaded
82
        $this->setExpectedException('\Twig_Error_Runtime', 'shuffle() expects parameter 1 to be array, object given');
83
        $twigTemplate = $twig->createTemplate($template);
84
        $twigTemplate->render([]);
85
    }
86
87
    public function testTimeDiffFunction()
88
    {
89
        $twig = $this->getTwig();
90
91
        $now = Carbon::now()->subMinute();
92
        $template = "{{ '" . $now->format('Y-m-d H:i:s') . "' | time_diff }}";
93
94
        $twigTemplate = $twig->createTemplate($template);
95
        $this->assertEquals($twigTemplate->render([]), '1 minute ago');
96
    }
97
98 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...
99
    {
100
        $twig = $this->getTwig();
101
102
        $template = "{{ '2016-03-24 23:05' | strftime('%d.%m.%Y %H:%M:%S') }}";
103
104
        $twigTemplate = $twig->createTemplate($template);
105
        $this->assertEquals($twigTemplate->render([]), '24.03.2016 23:05:00');
106
    }
107
108 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...
109
    {
110
        $twig = $this->getTwig();
111
112
        $template = "{{ 'Jack' | uppercase }}";
113
114
        $twigTemplate = $twig->createTemplate($template);
115
        $this->assertEquals($twigTemplate->render([]), 'JACK');
116
    }
117
118 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...
119
    {
120
        $twig = $this->getTwig();
121
122
        $template = "{{ 'JACK' | lowercase }}";
123
124
        $twigTemplate = $twig->createTemplate($template);
125
        $this->assertEquals($twigTemplate->render([]), 'jack');
126
    }
127
128 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...
129
    {
130
        $twig = $this->getTwig();
131
132
        $template = "{{ 'jack' | ucfirst }}";
133
134
        $twigTemplate = $twig->createTemplate($template);
135
        $this->assertEquals($twigTemplate->render([]), 'Jack');
136
    }
137
138 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...
139
    {
140
        $twig = $this->getTwig();
141
142
        $template = "{{ 'JACK' | lcfirst }}";
143
144
        $twigTemplate = $twig->createTemplate($template);
145
        $this->assertEquals($twigTemplate->render([]), 'jACK');
146
    }
147
148 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...
149
    {
150
        $twig = $this->getTwig();
151
152
        $template = "{{ ' jack' | ltrim }}";
153
154
        $twigTemplate = $twig->createTemplate($template);
155
        $this->assertEquals($twigTemplate->render([]), 'jack');
156
    }
157
158 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...
159
    {
160
        $twig = $this->getTwig();
161
162
        $template = "{{ 'jack ' | rtrim }}";
163
164
        $twigTemplate = $twig->createTemplate($template);
165
        $this->assertEquals($twigTemplate->render([]), 'jack');
166
    }
167
168 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...
169
    {
170
        $twig = $this->getTwig();
171
172
        $template = "{{ ' best' | str_repeat(3) }}";
173
174
        $twigTemplate = $twig->createTemplate($template);
175
        $this->assertEquals($twigTemplate->render([]), ' best best best');
176
    }
177
178 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...
179
    {
180
        $twig = $this->getTwig();
181
182
        $template = "{{ 'mail' | plural(count) }}";
183
184
        $twigTemplate = $twig->createTemplate($template);
185
        $this->assertEquals($twigTemplate->render([]), 'mails');
186
    }
187
188 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...
189
    {
190
        $twig = $this->getTwig();
191
192
        $template = "{{ 'test' | strpad(10) }}";
193
194
        $twigTemplate = $twig->createTemplate($template);
195
        $this->assertEquals($twigTemplate->render([]), '   test   ');
196
    }
197
198 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...
199
    {
200
        $twig = $this->getTwig();
201
202
        $template = "{{ 'test' | leftpad(7) }}";
203
204
        $twigTemplate = $twig->createTemplate($template);
205
        $this->assertEquals($twigTemplate->render([]), '   test');
206
    }
207
208 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...
209
    {
210
        $twig = $this->getTwig();
211
212
        $template = "{{ 'test' | rightpad(7, 'o') }}";
213
214
        $twigTemplate = $twig->createTemplate($template);
215
        $this->assertEquals($twigTemplate->render([]), 'testooo');
216
    }
217
}
218