GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#1228)
by Craig
01:55
created

AbstractDivLayoutTest::setUpVersion3Plus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
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
    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));
0 ignored issues
show
Unused Code introduced by
The call to FormExtension::__construct() has too many arguments starting with $this->renderer.

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 @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
87
        $extension->initRuntime($this->environment);
0 ignored issues
show
Bug introduced by
The method initRuntime() does not seem to exist on object<Symfony\Bridge\Tw...xtension\FormExtension>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
89
        // Add runtime loader
90
        $loader = $this->getMockBuilder('Twig_RuntimeLoaderInterface')->getMock();
91
        $loader->expects($this->any())->method('load')->will($this->returnValueMap(array(
92
            array('Symfony\Bridge\Twig\Form\TwigRenderer', $this->renderer),
93
        )));
94
        $this->environment->addRuntimeLoader($loader);
95
    }
96
97
    private function setUpVersion3Plus()
98
    {
99
        $csrfProvider = $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock();
100
        $this->renderer = new FormRenderer($this->rendererEngine, $csrfProvider);
101
        $this->environment->addExtension($extension = new FormExtension());
102
        $extension->initRuntime($this->environment);
0 ignored issues
show
Bug introduced by
The method initRuntime() does not seem to exist on object<Symfony\Bridge\Tw...xtension\FormExtension>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
104
        // Add runtime loader
105
        $this->environment->addRuntimeLoader(new \Twig_FactoryRuntimeLoader(array(
106
            'Symfony\Bridge\Twig\Form\TwigRenderer' => function() use ($csrfProvider) {
107
                return new TwigRenderer($this->rendererEngine, $csrfProvider);
108
            },
109
        )));
110
    }
111
112
    /**
113
     * @return PreloadedExtension[]
114
     */
115
    protected function getExtensions()
116
    {
117
        return array(new PreloadedExtension(array(
118
            'tab' => new TabType(),
119
        ), array(
120
            $this->getFormType('form') => array(
121
                $this->getHelpFormTypeExtension(),
122
                $this->getWidgetFormTypeExtension(),
123
                $this->getLegendFormTypeExtension(),
124
                $this->getLayoutFormTypeExtension(),
125
                $this->getErrorTypeFormTypeExtension(),
126
                $this->getEmbedFormExtension(),
127
                $this->getTabbedFormTypeExtension(),
128
                $this->getWidgetCollectionFormTypeExtension(),
129
            ),
130
            $this->getFormType('text') => array(
131
                $this->getStaticTextFormTypeExtension(),
132
            ),
133
        )));
134
    }
135
136
    /**
137
     * @return HelpFormTypeExtension
138
     */
139
    protected function getHelpFormTypeExtension()
140
    {
141
        $popoverOptions = array(
142
            'title' => null,
143
            'content' => null,
144
            'text' => null,
145
            'trigger' => 'hover',
146
            'toggle' => 'popover',
147
            'icon' => 'info-sign',
148
            'placement' => 'right',
149
            'selector' => null,
150
        );
151
152
        $tooltipOptions = array(
153
            'title' => null,
154
            'text' => null,
155
            'icon' => 'info-sign',
156
            'placement' => 'top',
157
        );
158
159
        return new HelpFormTypeExtension(array(
160
            'help_block_popover' => $popoverOptions,
161
            'help_label_popover' => $popoverOptions,
162
            'help_widget_popover' => $popoverOptions,
163
            'help_block_tooltip' => $tooltipOptions,
164
            'help_label_tooltip' => $tooltipOptions,
165
        ));
166
    }
167
168
    /**
169
     * @return WidgetFormTypeExtension
170
     */
171
    protected function getWidgetFormTypeExtension()
172
    {
173
        return new WidgetFormTypeExtension(array(
174
            'checkbox_label' => 'both',
175
        ));
176
    }
177
178
    /**
179
     * @return LegendFormTypeExtension
180
     */
181
    protected function getLegendFormTypeExtension()
182
    {
183
        return new LegendFormTypeExtension(array(
184
            'render_fieldset' => true,
185
            'show_legend' => true,
186
            'show_child_legend' => false,
187
            'legend_tag' => 'legend',
188
            'render_required_asterisk' => false,
189
            'render_optional_text' => true,
190
        ));
191
    }
192
193
    /**
194
     * @return LayoutFormTypeExtension
195
     */
196
    protected function getLayoutFormTypeExtension()
197
    {
198
        return new LayoutFormTypeExtension(array(
199
            'layout' => 'horizontal',
200
            'horizontal_label_class' => 'col-sm-3',
201
            'horizontal_label_div_class' => null,
202
            'horizontal_label_offset_class' => 'col-sm-offset-3',
203
            'horizontal_input_wrapper_class' => 'col-sm-9',
204
        ));
205
    }
206
207
    /**
208
     * @return ErrorTypeFormTypeExtension
209
     */
210
    protected function getErrorTypeFormTypeExtension()
211
    {
212
        return new ErrorTypeFormTypeExtension(array(
213
            'error_type' => null,
214
        ));
215
    }
216
217
    /**
218
     * @return EmbedFormExtension
219
     */
220
    protected function getEmbedFormExtension()
221
    {
222
        return new EmbedFormExtension(array(
0 ignored issues
show
Unused Code introduced by
The call to EmbedFormExtension::__construct() has too many arguments starting with array('embed_form' => true).

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 @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
223
            'embed_form' => true,
224
        ));
225
    }
226
227
    /**
228
     * @return StaticTextExtension
229
     */
230
    protected function getStaticTextFormTypeExtension()
231
    {
232
        return new StaticTextExtension();
233
    }
234
235
    /**
236
     * @return TabbedFormTypeExtension
237
     */
238
    protected function getTabbedFormTypeExtension()
239
    {
240
        return new TabbedFormTypeExtension($this->tabFactory, array(
241
            'class' => 'tabs nav-tabs',
242
        ));
243
    }
244
245
    /**
246
     * @return WidgetCollectionFormTypeExtension
247
     */
248
    protected function getWidgetCollectionFormTypeExtension()
249
    {
250
        return new WidgetCollectionFormTypeExtension(array(
251
            'render_collection_item' => true,
252
            'widget_add_btn' => array(
253
                'attr' => array('class' => 'btn btn-default'),
254
                'label' => 'add-item',
255
                'icon' => null,
256
                'icon_inverted' => false,
257
            ),
258
            'widget_remove_btn' => array(
259
                'attr' => array('class' => 'btn btn-default'),
260
                'wrapper_div' => array('class' => 'form-group'),
261
                'horizontal_wrapper_div' => array('class' => 'col-sm-3 col-sm-offset-3'),
262
                'label' => 'remove-item',
263
                'icon' => null,
264
                'icon_inverted' => false,
265
            ),
266
        ));
267
    }
268
269
    /**
270
     * @param string $html
271
     * @param string $expression
272
     * @param int    $count
273
     */
274
    protected function assertMatchesXpath($html, $expression, $count = 1)
275
    {
276
        $dom = new \DomDocument('UTF-8');
277
        try {
278
            // Wrap in <root> node so we can load HTML with multiple tags at
279
            // the top level
280
            $dom->loadXml('<root>'.$html.'</root>');
281
        } catch (\Exception $e) {
282
            $this->fail(sprintf(
283
                "Failed loading HTML:\n\n%s\n\nError: %s",
284
                $html,
285
                $e->getMessage()
286
            ));
287
        }
288
        $xpath = new \DOMXPath($dom);
289
        $nodeList = $xpath->evaluate('/root'.$expression);
290
        if ($nodeList->length != $count) {
291
            $dom->formatOutput = true;
292
            $this->fail(sprintf(
293
                "Failed asserting that \n\n%s\n\nmatches exactly %s. Matches %s in \n\n%s",
294
                $expression,
295
                $count == 1 ? 'once' : $count.' times',
296
                $nodeList->length == 1 ? 'once' : $nodeList->length.' times',
297
                // strip away <root> and </root>
298
                substr($dom->saveHTML(), 6, -8)
299
            ));
300
        }
301
    }
302
303
    /**
304
     * @param string $html
305
     *
306
     * @return string
307
     */
308
    protected function removeBreaks($html)
309
    {
310
        return str_replace('&nbsp;', '', $html);
311
    }
312
313
    /**
314
     * @param FormView $view
315
     * @param array    $vars
316
     *
317
     * @return string
318
     */
319
    protected function renderForm(FormView $view, array $vars = array())
320
    {
321
        return (string) $this->renderer->renderBlock($view, 'form', $vars);
322
    }
323
324
    /**
325
     * @param FormView $view
326
     * @param array    $vars
327
     *
328
     * @return string
329
     */
330
    protected function renderRow(FormView $view, array $vars = array())
331
    {
332
        return (string) $this->renderer->searchAndRenderBlock($view, 'row', $vars);
333
    }
334
335
    /**
336
     * @param FormView $view
337
     * @param array    $vars
338
     *
339
     * @return string
340
     */
341
    protected function renderWidget(FormView $view, array $vars = array())
342
    {
343
        return (string) $this->renderer->searchAndRenderBlock($view, 'widget', $vars);
344
    }
345
346
    /**
347
     * @param FormView $view
348
     * @param string   $label
349
     * @param array    $vars
350
     *
351
     * @return string
352
     */
353
    protected function renderLabel(FormView $view, $label = null, array $vars = array())
354
    {
355
        if ($label !== null) {
356
            $vars += array('label' => $label);
357
        }
358
359
        return (string) $this->renderer->searchAndRenderBlock($view, 'label', $vars);
360
    }
361
362
    protected function getFormType($name)
363
    {
364
         if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
365
             return $this->formTypeMap[$name];
366
         }
367
368
         return $name;
369
    }
370
371
    protected function getCollectionTypeKey()
372
    {
373
         if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
374
             return 'entry_type';
375
         }
376
377
         return 'type';
378
    }
379
380
    protected function getCollectionOptionsKey()
381
    {
382
         if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
383
             return 'entry_options';
384
         }
385
386
         return 'options';
387
    }
388
}
389