|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Admingenerator\GeneratorBundle\Tests\Twig\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Admingenerator\GeneratorBundle\Twig\Extension\EchoExtension; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This class test the Admingenerator\GeneratorBundle\Twig\Extension\EchoExtension |
|
9
|
|
|
* |
|
10
|
|
|
* @author Cedric LOMBARDOT |
|
11
|
|
|
* @author Stéphane Escandell |
|
12
|
|
|
*/ |
|
13
|
|
|
class EchoExtensionTest extends BaseExtensionTest |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var bool |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $useJms = false; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return \Twig_Extension |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function getTestedExtension() |
|
24
|
|
|
{ |
|
25
|
|
|
return new EchoExtension($this->useJms); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function getTwigVariables() |
|
32
|
|
|
{ |
|
33
|
|
|
$object = new TestObject(); |
|
34
|
|
|
|
|
35
|
|
|
return array( |
|
36
|
|
|
'obj' => $object, |
|
37
|
|
|
'name' => 'cedric', |
|
38
|
|
|
'arr' => array('obj' => 'val'), |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
View Code Duplication |
public function testGetEchoTrans() |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$tpls = array( |
|
45
|
|
|
'string' => '{{ echo_trans( "foo" ) }}', |
|
46
|
|
|
'variable_key' => '{{ echo_trans( name ) }}', |
|
47
|
|
|
'quote_included' => '{{ echo_trans( "My awesome \"title\"") }}' |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$returns = array( |
|
51
|
|
|
'string' => array( |
|
52
|
|
|
'{{ "foo"|trans({}, "Admingenerator")|raw }}', |
|
53
|
|
|
'trans return a good trans tag with string elements' |
|
54
|
|
|
), |
|
55
|
|
|
'variable_key' => array( |
|
56
|
|
|
'{{ "cedric"|trans({}, "Admingenerator")|raw }}', |
|
57
|
|
|
'trans return a good trans tag with variable as key' |
|
58
|
|
|
), |
|
59
|
|
|
'quote_included' => array( |
|
60
|
|
|
'{{ "My awesome \"title\""|trans({}, "Admingenerator")|raw }}', |
|
61
|
|
|
'trans return a good trans tag with variable as key' |
|
62
|
|
|
), |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$this->runTwigTests($tpls, $returns); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testGetEchoTransWithEscape() |
|
69
|
|
|
{ |
|
70
|
|
|
$tpls = array( |
|
71
|
|
|
'string' => '{{ echo_trans( "foo", {}, "Admingenerator", "html_attr" ) }}', |
|
72
|
|
|
'variable_key' => '{{ echo_trans( name, {}, "Admingenerator", "html_attr" ) }}', |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$returns = array( |
|
76
|
|
|
'string' => array( |
|
77
|
|
|
'{{ "foo"|trans({}, "Admingenerator")|escape("html_attr") }}', |
|
78
|
|
|
'trans return a good trans tag with string elements' |
|
79
|
|
|
), |
|
80
|
|
|
'variable_key' => array( |
|
81
|
|
|
'{{ "cedric"|trans({}, "Admingenerator")|escape("html_attr") }}', |
|
82
|
|
|
'trans return a good trans tag with variable as key' |
|
83
|
|
|
), |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
$this->runTwigTests($tpls, $returns); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
public function testGetEchoTransWithParameters() |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$tpls = array( |
|
92
|
|
|
'string' => "{{ echo_trans('Display all <b>%foo% %bar%</b> results', { 'foo': 'foo', 'bar': 'bar' }) }}", |
|
93
|
|
|
'variable_key' => '{{ echo_trans(name, { \'foo\': \'foo\', \'bar\': \'bar\' }) }}', |
|
94
|
|
|
'quote_in_param_value' => '{{ echo_trans(name, { \'foo\': \'foo\\\'s\', \'bar\': \'bar\' }) }}', |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$returns = array( |
|
98
|
|
|
'string' => array( |
|
99
|
|
|
'{{ "Display all <b>%foo% %bar%</b> results"|trans({\'%foo%\': \'foo\',\'%bar%\': \'bar\',}, "Admingenerator")|raw }}', |
|
100
|
|
|
'trans return a good trans tag with string elements' |
|
101
|
|
|
), |
|
102
|
|
|
'variable_key' => array( |
|
103
|
|
|
'{{ "cedric"|trans({\'%foo%\': \'foo\',\'%bar%\': \'bar\',}, "Admingenerator")|raw }}', |
|
104
|
|
|
'trans return a good trans tag with variable as key' |
|
105
|
|
|
), |
|
106
|
|
|
'quote_in_param_value' => array( |
|
107
|
|
|
'{{ "cedric"|trans({\'%foo%\': \'foo\\\'s\',\'%bar%\': \'bar\',}, "Admingenerator")|raw }}', |
|
108
|
|
|
'trans return a good trans tag with variable as key' |
|
109
|
|
|
), |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
$this->runTwigTests($tpls, $returns); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testGetEchoTransWithParameterBag() |
|
116
|
|
|
{ |
|
117
|
|
|
$tpls = array( |
|
118
|
|
|
'string_bc' => "{{ echo_trans('You\'re editing {{ Book.title }} written by {{ Book.author.name }}!') }}", |
|
119
|
|
|
'string_with_full_param_bag' => "{{ echo_trans('You\'re editing %book% written by %author%!|{ %book%: Book.title, %author%: Book.author.name }|') }}", |
|
120
|
|
|
'string_with_abbrev_param_bag' => "{{ echo_trans('You\'re editing %Book.title% written by %Book.author.name%!|{ Book.title, Book.author.name }|') }}", |
|
121
|
|
|
'string_with_full_param_bag_and_params' => "{{ echo_trans('You\'re editing %book% written by %foo%!|{ %book%: Book.title }|',{ 'foo': 'foo' }) }}", |
|
122
|
|
|
'string_with_abbrev_param_bag_and_params' => "{{ echo_trans('You\'re editing %Book.title% written by %foo%!|{ Book.title }|',{ 'foo': 'foo' }) }}", |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
$returns = array( |
|
126
|
|
|
'string_bc' => array( |
|
127
|
|
|
'{{ "You\'re editing %Book.title% written by %Book.author.name%!"|trans({\'%Book.title%\': Book.title,\'%Book.author.name%\': Book.author.name,}, "Admingenerator")|raw }}', |
|
128
|
|
|
'trans return a good trans tag with string elements' |
|
129
|
|
|
), |
|
130
|
|
|
'string_with_full_param_bag' => array( |
|
131
|
|
|
'{{ "You\'re editing %book% written by %author%!"|trans({\'%book%\': Book.title,\'%author%\': Book.author.name,}, "Admingenerator")|raw }}', |
|
132
|
|
|
'trans return a good trans tag with string elements' |
|
133
|
|
|
), |
|
134
|
|
|
'string_with_abbrev_param_bag' => array( |
|
135
|
|
|
'{{ "You\'re editing %Book.title% written by %Book.author.name%!"|trans({\'%Book.title%\': Book.title,\'%Book.author.name%\': Book.author.name,}, "Admingenerator")|raw }}', |
|
136
|
|
|
'trans return a good trans tag with string elements' |
|
137
|
|
|
), |
|
138
|
|
|
'string_with_full_param_bag_and_params' => array( |
|
139
|
|
|
'{{ "You\'re editing %book% written by %foo%!"|trans({\'%foo%\': \'foo\',\'%book%\': Book.title,}, "Admingenerator")|raw }}', |
|
140
|
|
|
'trans return a good trans tag with string elements' |
|
141
|
|
|
), |
|
142
|
|
|
'string_with_abbrev_param_bag_and_params' => array( |
|
143
|
|
|
'{{ "You\'re editing %Book.title% written by %foo%!"|trans({\'%foo%\': \'foo\',\'%Book.title%\': Book.title,}, "Admingenerator")|raw }}', |
|
144
|
|
|
'trans return a good trans tag with string elements' |
|
145
|
|
|
), |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
$this->runTwigTests($tpls, $returns); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testGetEchoPath() |
|
152
|
|
|
{ |
|
153
|
|
|
$tpls = array( |
|
154
|
|
|
'string' => '{{ echo_path( "foo" ) }}', |
|
155
|
|
|
'variable' => '{{ echo_path( name ) }}', |
|
156
|
|
|
'array' => '{{ echo_path( arr.obj ) }}', |
|
157
|
|
|
'string_filtered' => '{{ echo_path( "foo", null, ["foo", "bar"] ) }}', |
|
158
|
|
|
'variable_filtered' => '{{ echo_path( name, null, ["foo", "bar"] ) }}', |
|
159
|
|
|
'array_filtered' => '{{ echo_path( arr.obj, null, ["foo", "bar"] ) }}', |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
$returns = array( |
|
163
|
|
|
'string' => array( |
|
164
|
|
|
'{{ path("foo") }}', |
|
165
|
|
|
'Path return a good Path tag with string elements' |
|
166
|
|
|
), |
|
167
|
|
|
'variable' => array( |
|
168
|
|
|
'{{ path("cedric") }}', |
|
169
|
|
|
'Path return a good Path tag with variable' |
|
170
|
|
|
), |
|
171
|
|
|
'array' => array( |
|
172
|
|
|
'{{ path("val") }}', |
|
173
|
|
|
'Path return a good Path tag with array element' |
|
174
|
|
|
), |
|
175
|
|
|
'string_filtered' => array( |
|
176
|
|
|
'{{ path("foo")|foo|bar }}', |
|
177
|
|
|
'Path return a good Path tag with string elements and filters' |
|
178
|
|
|
), |
|
179
|
|
|
'variable_filtered' => array( |
|
180
|
|
|
'{{ path("cedric")|foo|bar }}', |
|
181
|
|
|
'Path return a good Path tag with variable and filters' |
|
182
|
|
|
), |
|
183
|
|
|
'array_filtered' => array( |
|
184
|
|
|
'{{ path("val")|foo|bar }}', |
|
185
|
|
|
'Path return a good Path tag with array element and filters' |
|
186
|
|
|
), |
|
187
|
|
|
); |
|
188
|
|
|
|
|
189
|
|
|
$this->runTwigTests($tpls, $returns); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function testGetEchoIfGrantedWithoutJms() |
|
193
|
|
|
{ |
|
194
|
|
|
$this->useJms = false; |
|
195
|
|
|
$tpls = array( |
|
196
|
|
|
'simple' => '{{ echo_if_granted ( "ROLE_A" ) }}', |
|
197
|
|
|
'with_object' => '{{ echo_if_granted ( "ROLE_A", \'modelName\' ) }}', |
|
198
|
|
|
); |
|
199
|
|
|
|
|
200
|
|
|
$returns = array( |
|
201
|
|
|
'simple' => array( |
|
202
|
|
|
'{% if is_granted(\'ROLE_A\') %}', |
|
203
|
|
|
'If granted work with a simple role'), |
|
204
|
|
|
'with_object' => array( |
|
205
|
|
|
'{% if is_granted(\'ROLE_A\', modelName is defined ? modelName : null) %}', |
|
206
|
|
|
'If granted work with an object' |
|
207
|
|
|
), |
|
208
|
|
|
); |
|
209
|
|
|
|
|
210
|
|
|
$this->runTwigTests($tpls, $returns); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function testGetEchoIfGrantedWithJms() |
|
214
|
|
|
{ |
|
215
|
|
|
$this->useJms = true; |
|
216
|
|
|
$tpls = array( |
|
217
|
|
|
'simple' => '{{ echo_if_granted ( "hasRole(\'ROLE_A\')" ) }}', |
|
218
|
|
|
'complex' => '{{ echo_if_granted ( "hasRole(\'ROLE_A\')\') or (hasRole(\'ROLE_B\') and hasRole(\'ROLE_C\')" ) }}', |
|
219
|
|
|
'with_object' => '{{ echo_if_granted ( "hasRole(\'ROLE_A\')", \'modelName\' ) }}', |
|
220
|
|
|
); |
|
221
|
|
|
|
|
222
|
|
|
$returns = array( |
|
223
|
|
|
'simple' => array( |
|
224
|
|
|
'{% if is_expr_granted(\'hasRole(\'ROLE_A\')\') %}', |
|
225
|
|
|
'If granted work with a simple role'), |
|
226
|
|
|
'complex' => array( |
|
227
|
|
|
'{% if is_expr_granted(\'hasRole(\'ROLE_A\')\') or (hasRole(\'ROLE_B\') and hasRole(\'ROLE_C\')\') %}', |
|
228
|
|
|
'If granted work with a complex role expression' |
|
229
|
|
|
), |
|
230
|
|
|
'with_object' => array( |
|
231
|
|
|
'{% if is_expr_granted(\'hasRole(\'ROLE_A\')\', modelName is defined ? modelName : null) %}', |
|
232
|
|
|
'If granted work with an object' |
|
233
|
|
|
), |
|
234
|
|
|
); |
|
235
|
|
|
|
|
236
|
|
|
$this->runTwigTests($tpls, $returns); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function testGetEchoRender() |
|
240
|
|
|
{ |
|
241
|
|
|
$tpls = array( |
|
242
|
|
|
'controller' => '{{ echo_render( "MyController" ) }}', |
|
243
|
|
|
'with_params' => '{{ echo_render( "MyController", {"hello": name } ) }}', |
|
244
|
|
|
); |
|
245
|
|
|
|
|
246
|
|
|
$returns = array( |
|
247
|
|
|
'controller' => array( |
|
248
|
|
|
'{{ render(controller("MyController", { })) }}', |
|
249
|
|
|
'controller return a good controller tag' |
|
250
|
|
|
), |
|
251
|
|
|
'with_params' => array( |
|
252
|
|
|
'{{ render(controller("MyController", { hello: \'cedric\' })) }}', |
|
253
|
|
|
'controller return a good controller tag' |
|
254
|
|
|
), |
|
255
|
|
|
); |
|
256
|
|
|
|
|
257
|
|
|
$this->runTwigTests($tpls, $returns); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
public function testConvertAsForm() |
|
261
|
|
|
{ |
|
262
|
|
|
$tpls = array( |
|
263
|
|
|
'no_modifications' => "{{ 'my string'|convert_as_form('unused') }}", |
|
264
|
|
|
'no_modifications_on_specific_characters' => "{{ '\"\'&<>;{}()[]\\\/'|convert_as_form('unused') }}", |
|
265
|
|
|
'query_builder' => "{{ \"'query_builder' => 'function(\$er) { return \$er->createQueryBuilder(); }',\"|convert_as_form('MyEntityType') }}", |
|
266
|
|
|
'query' => "{{ \"'query' => 'function() { return MyModel::GetPeerTable(); }',\"|convert_as_form('ModelType') }}", |
|
267
|
|
|
'php_call' => "{{ \"'__php(strtolower(\'TeSt\'))'\"|convert_as_form('unused') }}", |
|
268
|
|
|
'php_call_and_string' => "{{ \"'__php(strtolower(\'TeSt\'))','my other string'\"|convert_as_form('unused') }}", |
|
269
|
|
|
); |
|
270
|
|
|
|
|
271
|
|
|
$returns = array( |
|
272
|
|
|
'no_modifications' => array('my string', "convert_as_form doesn't modify string"), |
|
273
|
|
|
'no_modifications_on_specific_characters' => array("\"'&<>;{}()[]\\/", "convert_as_form doesn't modify specific characters"), |
|
274
|
|
|
'query_builder' => array("'query_builder' => function(\$er) { return \$er->createQueryBuilder(); },", 'convert_as_form properly transforms query_builder'), |
|
275
|
|
|
'query' => array("'query' => function() { return MyModel::GetPeerTable(); },", 'convert_as_form properly transforms query'), |
|
276
|
|
|
'php_call' => array("strtolower('TeSt')", 'convert_as_form properly transforms __php'), |
|
277
|
|
|
'php_call_and_string' => array("strtolower('TeSt'),'my other string'", 'convert_as_form properly transforms __php'), |
|
278
|
|
|
); |
|
279
|
|
|
|
|
280
|
|
|
$this->runTwigTests($tpls, $returns); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
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.