1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace VojtaSvoboda\TwigExtensions\Tests; |
4
|
|
|
|
5
|
|
|
use App; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use PluginTestCase; |
8
|
|
|
use Twig_Environment; |
9
|
|
|
use Config; |
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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
/* |
|
|
|
|
83
|
|
|
$twigTemplate = $twig->createTemplate($template); |
84
|
|
|
$twigTemplate->render([]); |
85
|
|
|
*/ |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testTimeDiffFunction() |
89
|
|
|
{ |
90
|
|
|
$twig = $this->getTwig(); |
91
|
|
|
|
92
|
|
|
$now = Carbon::now()->subMinute(); |
93
|
|
|
$template = "{{ '" . $now->format('Y-m-d H:i:s') . "' | time_diff }}"; |
94
|
|
|
|
95
|
|
|
$twigTemplate = $twig->createTemplate($template); |
96
|
|
|
$this->assertEquals($twigTemplate->render([]), '1 minute ago'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function testStrftimeFunction() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$twig = $this->getTwig(); |
102
|
|
|
|
103
|
|
|
$template = "{{ '2016-03-24 23:05' | strftime('%d.%m.%Y %H:%M:%S') }}"; |
104
|
|
|
|
105
|
|
|
$twigTemplate = $twig->createTemplate($template); |
106
|
|
|
$this->assertEquals($twigTemplate->render([]), '24.03.2016 23:05:00'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
public function testUppercaseFunction() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$twig = $this->getTwig(); |
112
|
|
|
|
113
|
|
|
$template = "{{ 'Jack' | uppercase }}"; |
114
|
|
|
|
115
|
|
|
$twigTemplate = $twig->createTemplate($template); |
116
|
|
|
$this->assertEquals($twigTemplate->render([]), 'JACK'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
public function testLowercaseFunction() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$twig = $this->getTwig(); |
122
|
|
|
|
123
|
|
|
$template = "{{ 'JACK' | lowercase }}"; |
124
|
|
|
|
125
|
|
|
$twigTemplate = $twig->createTemplate($template); |
126
|
|
|
$this->assertEquals($twigTemplate->render([]), 'jack'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
public function testUcfirstFunction() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$twig = $this->getTwig(); |
132
|
|
|
|
133
|
|
|
$template = "{{ 'jack' | ucfirst }}"; |
134
|
|
|
|
135
|
|
|
$twigTemplate = $twig->createTemplate($template); |
136
|
|
|
$this->assertEquals($twigTemplate->render([]), 'Jack'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function testLcfirstFunction() |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$twig = $this->getTwig(); |
142
|
|
|
|
143
|
|
|
$template = "{{ 'JACK' | lcfirst }}"; |
144
|
|
|
|
145
|
|
|
$twigTemplate = $twig->createTemplate($template); |
146
|
|
|
$this->assertEquals($twigTemplate->render([]), 'jACK'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
public function testLtrimFunction() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$twig = $this->getTwig(); |
152
|
|
|
|
153
|
|
|
$template = "{{ ' jack' | ltrim }}"; |
154
|
|
|
|
155
|
|
|
$twigTemplate = $twig->createTemplate($template); |
156
|
|
|
$this->assertEquals($twigTemplate->render([]), 'jack'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
View Code Duplication |
public function testRtrimFunction() |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$twig = $this->getTwig(); |
162
|
|
|
|
163
|
|
|
$template = "{{ 'jack ' | rtrim }}"; |
164
|
|
|
|
165
|
|
|
$twigTemplate = $twig->createTemplate($template); |
166
|
|
|
$this->assertEquals($twigTemplate->render([]), 'jack'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
View Code Duplication |
public function testStrRepeatFunction() |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$twig = $this->getTwig(); |
172
|
|
|
|
173
|
|
|
$template = "{{ ' best' | str_repeat(3) }}"; |
174
|
|
|
|
175
|
|
|
$twigTemplate = $twig->createTemplate($template); |
176
|
|
|
$this->assertEquals($twigTemplate->render([]), ' best best best'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
View Code Duplication |
public function testPluralFunction() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$twig = $this->getTwig(); |
182
|
|
|
|
183
|
|
|
$template = "{{ 'mail' | plural(count) }}"; |
184
|
|
|
|
185
|
|
|
$twigTemplate = $twig->createTemplate($template); |
186
|
|
|
$this->assertEquals($twigTemplate->render([]), 'mails'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
View Code Duplication |
public function testStrpadFunction() |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
$twig = $this->getTwig(); |
192
|
|
|
|
193
|
|
|
$template = "{{ 'test' | strpad(10) }}"; |
194
|
|
|
|
195
|
|
|
$twigTemplate = $twig->createTemplate($template); |
196
|
|
|
$this->assertEquals($twigTemplate->render([]), ' test '); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
View Code Duplication |
public function testLeftpadFunction() |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
$twig = $this->getTwig(); |
202
|
|
|
|
203
|
|
|
$template = "{{ 'test' | leftpad(7) }}"; |
204
|
|
|
|
205
|
|
|
$twigTemplate = $twig->createTemplate($template); |
206
|
|
|
$this->assertEquals($twigTemplate->render([]), ' test'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
View Code Duplication |
public function testRightpadFunction() |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
$twig = $this->getTwig(); |
212
|
|
|
|
213
|
|
|
$template = "{{ 'test' | rightpad(7, 'o') }}"; |
214
|
|
|
|
215
|
|
|
$twigTemplate = $twig->createTemplate($template); |
216
|
|
|
$this->assertEquals($twigTemplate->render([]), 'testooo'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
View Code Duplication |
public function testConfigFunction() |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
$twig = $this->getTwig(); |
222
|
|
|
|
223
|
|
|
Config::set('app.custom.key', 'test value'); |
224
|
|
|
$template = "{{ config('app.custom.key') }}"; |
225
|
|
|
|
226
|
|
|
$twigTemplate = $twig->createTemplate($template); |
227
|
|
|
$this->assertEquals($twigTemplate->render([]), 'test value'); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
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.