|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mopa\Bundle\BootstrapBundle\Tests\Form; |
|
4
|
|
|
|
|
5
|
|
|
use Mopa\Bundle\BootstrapBundle\Form\Extension\EmbedFormExtension; |
|
6
|
|
|
use Mopa\Bundle\BootstrapBundle\Form\Extension\ErrorTypeFormTypeExtension; |
|
7
|
|
|
use Mopa\Bundle\BootstrapBundle\Form\Extension\HelpFormTypeExtension; |
|
8
|
|
|
use Mopa\Bundle\BootstrapBundle\Form\Extension\HorizontalFormTypeExtension; |
|
9
|
|
|
use Mopa\Bundle\BootstrapBundle\Form\Extension\LegendFormTypeExtension; |
|
10
|
|
|
use Mopa\Bundle\BootstrapBundle\Form\Extension\StaticTextExtension; |
|
11
|
|
|
use Mopa\Bundle\BootstrapBundle\Form\Extension\TabbedFormTypeExtension; |
|
12
|
|
|
use Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetCollectionFormTypeExtension; |
|
13
|
|
|
use Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetFormTypeExtension; |
|
14
|
|
|
use Mopa\Bundle\BootstrapBundle\Twig\FormExtension as TwigFormExtension; |
|
15
|
|
|
use Mopa\Bundle\BootstrapBundle\Twig\IconExtension; |
|
16
|
|
|
use Symfony\Bridge\Twig\Extension\FormExtension; |
|
17
|
|
|
use Symfony\Bridge\Twig\Extension\TranslationExtension; |
|
18
|
|
|
use Symfony\Bridge\Twig\Form\TwigRenderer; |
|
19
|
|
|
use Symfony\Bridge\Twig\Form\TwigRendererEngine; |
|
20
|
|
|
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator; |
|
21
|
|
|
use Symfony\Component\Form\Forms; |
|
22
|
|
|
use Symfony\Component\Form\FormView; |
|
23
|
|
|
use Symfony\Component\Form\PreloadedExtension; |
|
24
|
|
|
use Symfony\Component\Form\Test\FormIntegrationTestCase; |
|
25
|
|
|
|
|
26
|
|
|
abstract class AbstractDivLayoutTest extends FormIntegrationTestCase |
|
27
|
|
|
{ |
|
28
|
|
|
protected $extension; |
|
29
|
|
|
protected $tabFactory; |
|
30
|
|
|
protected $formTypeMap = array( |
|
31
|
|
|
'form' => 'Symfony\Component\Form\Extension\Core\Type\FormType', |
|
32
|
|
|
'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType', |
|
33
|
|
|
'email' => 'Symfony\Component\Form\Extension\Core\Type\EmailType', |
|
34
|
|
|
'collection' => 'Symfony\Component\Form\Extension\Core\Type\CollectionType', |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @throws \Twig_Error_Loader |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function setUp() |
|
41
|
|
|
{ |
|
42
|
|
|
// Setup factory for tabs |
|
43
|
|
|
$this->tabFactory = Forms::createFormFactory(); |
|
44
|
|
|
|
|
45
|
|
|
parent::setUp(); |
|
46
|
|
|
|
|
47
|
|
|
$rendererEngine = new TwigRendererEngine(array( |
|
48
|
|
|
'form_div_layout.html.twig', |
|
49
|
|
|
'fields.html.twig', |
|
50
|
|
|
)); |
|
51
|
|
|
|
|
52
|
|
|
if (interface_exists('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')) { |
|
53
|
|
|
$csrfProviderInterface = 'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'; |
|
54
|
|
|
} else { |
|
55
|
|
|
$csrfProviderInterface = 'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfProviderInterface)); |
|
59
|
|
|
|
|
60
|
|
|
$this->extension = new FormExtension($renderer); |
|
61
|
|
|
|
|
62
|
|
|
$reflection = new \ReflectionClass($renderer); |
|
63
|
|
|
$bridgeDirectory = dirname($reflection->getFileName()).'/../Resources/views/Form'; |
|
64
|
|
|
|
|
65
|
|
|
$loader = new \Twig_Loader_Filesystem(array( |
|
66
|
|
|
$bridgeDirectory, |
|
67
|
|
|
__DIR__.'/../../Resources/views/Form', |
|
68
|
|
|
)); |
|
69
|
|
|
|
|
70
|
|
|
$loader->addPath(__DIR__.'/../../Resources/views', 'MopaBootstrap'); |
|
71
|
|
|
|
|
72
|
|
|
$environment = new \Twig_Environment($loader, array('strict_variables' => true)); |
|
73
|
|
|
$environment->addExtension(new TranslationExtension(new StubTranslator())); |
|
74
|
|
|
$environment->addExtension(new IconExtension('fontawesome')); |
|
75
|
|
|
$environment->addExtension(new TwigFormExtension()); |
|
76
|
|
|
$environment->addGlobal('global', ''); |
|
77
|
|
|
$environment->addExtension($this->extension); |
|
78
|
|
|
|
|
79
|
|
|
$this->extension->initRuntime($environment); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return PreloadedExtension[] |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getExtensions() |
|
86
|
|
|
{ |
|
87
|
|
|
return array(new PreloadedExtension(array(), array( |
|
88
|
|
|
$this->getFormType('form') => array( |
|
89
|
|
|
$this->getHelpFormTypeExtension(), |
|
90
|
|
|
$this->getWidgetFormTypeExtension(), |
|
91
|
|
|
$this->getLegendFormTypeExtension(), |
|
92
|
|
|
$this->getHorizontalFormTypeExtension(), |
|
93
|
|
|
$this->getErrorTypeFormTypeExtension(), |
|
94
|
|
|
$this->getEmbedFormExtension(), |
|
95
|
|
|
$this->getTabbedFormTypeExtension(), |
|
96
|
|
|
$this->getWidgetCollectionFormTypeExtension(), |
|
97
|
|
|
), |
|
98
|
|
|
$this->getFormType('text') => array( |
|
99
|
|
|
$this->getStaticTextFormTypeExtension(), |
|
100
|
|
|
), |
|
101
|
|
|
))); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return HelpFormTypeExtension |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function getHelpFormTypeExtension() |
|
108
|
|
|
{ |
|
109
|
|
|
$popoverOptions = array( |
|
110
|
|
|
'title' => null, |
|
111
|
|
|
'content' => null, |
|
112
|
|
|
'text' => null, |
|
113
|
|
|
'trigger' => 'hover', |
|
114
|
|
|
'toggle' => 'popover', |
|
115
|
|
|
'icon' => 'info-sign', |
|
116
|
|
|
'placement' => 'right', |
|
117
|
|
|
'selector' => null, |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
$tooltipOptions = array( |
|
121
|
|
|
'title' => null, |
|
122
|
|
|
'text' => null, |
|
123
|
|
|
'icon' => 'info-sign', |
|
124
|
|
|
'placement' => 'top', |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
return new HelpFormTypeExtension(array( |
|
128
|
|
|
'help_block_popover' => $popoverOptions, |
|
129
|
|
|
'help_label_popover' => $popoverOptions, |
|
130
|
|
|
'help_widget_popover' => $popoverOptions, |
|
131
|
|
|
'help_block_tooltip' => $tooltipOptions, |
|
132
|
|
|
'help_label_tooltip' => $tooltipOptions, |
|
133
|
|
|
)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return WidgetFormTypeExtension |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getWidgetFormTypeExtension() |
|
140
|
|
|
{ |
|
141
|
|
|
return new WidgetFormTypeExtension(array( |
|
142
|
|
|
'checkbox_label' => 'both', |
|
143
|
|
|
)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return LegendFormTypeExtension |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function getLegendFormTypeExtension() |
|
150
|
|
|
{ |
|
151
|
|
|
return new LegendFormTypeExtension(array( |
|
152
|
|
|
'render_fieldset' => true, |
|
153
|
|
|
'show_legend' => true, |
|
154
|
|
|
'show_child_legend' => false, |
|
155
|
|
|
'legend_tag' => 'legend', |
|
156
|
|
|
'render_required_asterisk' => false, |
|
157
|
|
|
'render_optional_text' => true, |
|
158
|
|
|
)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return HorizontalFormTypeExtension |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function getHorizontalFormTypeExtension() |
|
165
|
|
|
{ |
|
166
|
|
|
return new HorizontalFormTypeExtension(array( |
|
167
|
|
|
'horizontal' => true, |
|
168
|
|
|
'horizontal_label_class' => 'col-sm-3', |
|
169
|
|
|
'horizontal_label_div_class' => null, |
|
170
|
|
|
'horizontal_label_offset_class' => 'col-sm-offset-3', |
|
171
|
|
|
'horizontal_input_wrapper_class' => 'col-sm-9', |
|
172
|
|
|
)); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @return ErrorTypeFormTypeExtension |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function getErrorTypeFormTypeExtension() |
|
179
|
|
|
{ |
|
180
|
|
|
return new ErrorTypeFormTypeExtension(array( |
|
181
|
|
|
'error_type' => null, |
|
182
|
|
|
)); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return EmbedFormExtension |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function getEmbedFormExtension() |
|
189
|
|
|
{ |
|
190
|
|
|
return new EmbedFormExtension(array( |
|
|
|
|
|
|
191
|
|
|
'embed_form' => true, |
|
192
|
|
|
)); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return StaticTextExtension |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function getStaticTextFormTypeExtension() |
|
199
|
|
|
{ |
|
200
|
|
|
return new StaticTextExtension(); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return TabbedFormTypeExtension |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function getTabbedFormTypeExtension() |
|
207
|
|
|
{ |
|
208
|
|
|
return new TabbedFormTypeExtension($this->tabFactory, array( |
|
209
|
|
|
'class' => 'tabs nav-tabs', |
|
210
|
|
|
)); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @return WidgetCollectionFormTypeExtension |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function getWidgetCollectionFormTypeExtension() |
|
217
|
|
|
{ |
|
218
|
|
|
return new WidgetCollectionFormTypeExtension(array( |
|
219
|
|
|
'render_collection_item' => true, |
|
220
|
|
|
'widget_add_btn' => array( |
|
221
|
|
|
'attr' => array('class' => 'btn btn-default'), |
|
222
|
|
|
'label' => 'add-item', |
|
223
|
|
|
'icon' => null, |
|
224
|
|
|
'icon_inverted' => false, |
|
225
|
|
|
), |
|
226
|
|
|
'widget_remove_btn' => array( |
|
227
|
|
|
'attr' => array('class' => 'btn btn-default'), |
|
228
|
|
|
'wrapper_div' => array('class' => 'form-group'), |
|
229
|
|
|
'horizontal_wrapper_div' => array('class' => 'col-sm-3 col-sm-offset-3'), |
|
230
|
|
|
'label' => 'remove-item', |
|
231
|
|
|
'icon' => null, |
|
232
|
|
|
'icon_inverted' => false, |
|
233
|
|
|
), |
|
234
|
|
|
)); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param string $html |
|
239
|
|
|
* @param string $expression |
|
240
|
|
|
* @param int $count |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function assertMatchesXpath($html, $expression, $count = 1) |
|
243
|
|
|
{ |
|
244
|
|
|
$dom = new \DomDocument('UTF-8'); |
|
245
|
|
|
try { |
|
246
|
|
|
// Wrap in <root> node so we can load HTML with multiple tags at |
|
247
|
|
|
// the top level |
|
248
|
|
|
$dom->loadXml('<root>'.$html.'</root>'); |
|
249
|
|
|
} catch (\Exception $e) { |
|
250
|
|
|
$this->fail(sprintf( |
|
251
|
|
|
"Failed loading HTML:\n\n%s\n\nError: %s", |
|
252
|
|
|
$html, |
|
253
|
|
|
$e->getMessage() |
|
254
|
|
|
)); |
|
255
|
|
|
} |
|
256
|
|
|
$xpath = new \DOMXPath($dom); |
|
257
|
|
|
$nodeList = $xpath->evaluate('/root'.$expression); |
|
258
|
|
|
if ($nodeList->length != $count) { |
|
259
|
|
|
$dom->formatOutput = true; |
|
260
|
|
|
$this->fail(sprintf( |
|
261
|
|
|
"Failed asserting that \n\n%s\n\nmatches exactly %s. Matches %s in \n\n%s", |
|
262
|
|
|
$expression, |
|
263
|
|
|
$count == 1 ? 'once' : $count.' times', |
|
264
|
|
|
$nodeList->length == 1 ? 'once' : $nodeList->length.' times', |
|
265
|
|
|
// strip away <root> and </root> |
|
266
|
|
|
substr($dom->saveHTML(), 6, -8) |
|
267
|
|
|
)); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @param string $html |
|
273
|
|
|
* |
|
274
|
|
|
* @return string |
|
275
|
|
|
*/ |
|
276
|
|
|
protected function removeBreaks($html) |
|
277
|
|
|
{ |
|
278
|
|
|
return str_replace(' ', '', $html); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @param FormView $view |
|
283
|
|
|
* @param array $vars |
|
284
|
|
|
* |
|
285
|
|
|
* @return string |
|
286
|
|
|
*/ |
|
287
|
|
|
protected function renderForm(FormView $view, array $vars = array()) |
|
288
|
|
|
{ |
|
289
|
|
|
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @param FormView $view |
|
294
|
|
|
* @param array $vars |
|
295
|
|
|
* |
|
296
|
|
|
* @return string |
|
297
|
|
|
*/ |
|
298
|
|
|
protected function renderRow(FormView $view, array $vars = array()) |
|
299
|
|
|
{ |
|
300
|
|
|
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @param FormView $view |
|
305
|
|
|
* @param array $vars |
|
306
|
|
|
* |
|
307
|
|
|
* @return string |
|
308
|
|
|
*/ |
|
309
|
|
|
protected function renderWidget(FormView $view, array $vars = array()) |
|
310
|
|
|
{ |
|
311
|
|
|
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* @param FormView $view |
|
316
|
|
|
* @param string $label |
|
317
|
|
|
* @param array $vars |
|
318
|
|
|
* |
|
319
|
|
|
* @return string |
|
320
|
|
|
*/ |
|
321
|
|
|
protected function renderLabel(FormView $view, $label = null, array $vars = array()) |
|
322
|
|
|
{ |
|
323
|
|
|
if ($label !== null) { |
|
324
|
|
|
$vars += array('label' => $label); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
protected function getFormType($name) |
|
331
|
|
|
{ |
|
332
|
|
|
if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) { |
|
333
|
|
|
return $this->formTypeMap[$name]; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
return $name; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
protected function getCollectionTypeKey() |
|
340
|
|
|
{ |
|
341
|
|
|
if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) { |
|
342
|
|
|
return 'entry_type'; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
return 'type'; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
protected function getCollectionOptionsKey() |
|
349
|
|
|
{ |
|
350
|
|
|
if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) { |
|
351
|
|
|
return 'entry_options'; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
return 'options'; |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.