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