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
06:56 queued 05:04
created

AbstractDivLayoutTest   D

Complexity

Total Complexity 32

Size/Duplication

Total Lines 364
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 25

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 25
dl 0
loc 364
rs 4.8
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 35 3
A setUpVersion2() 0 14 1
A setUpVersion3Plus() 0 18 1
A getExtensions() 0 20 1
B getHelpFormTypeExtension() 0 28 1
A getWidgetFormTypeExtension() 0 6 1
A getLegendFormTypeExtension() 0 11 1
A getLayoutFormTypeExtension() 0 10 1
A getErrorTypeFormTypeExtension() 0 6 1
A getEmbedFormExtension() 0 6 1
A getStaticTextFormTypeExtension() 0 4 1
A getTabbedFormTypeExtension() 0 6 1
A getWidgetCollectionFormTypeExtension() 0 20 1
B assertMatchesXpath() 0 28 5
A removeBreaks() 0 4 1
A renderForm() 0 4 1
A renderRow() 0 4 1
A renderWidget() 0 4 1
A renderLabel() 0 8 2
A getFormType() 0 8 2
A getCollectionTypeKey() 0 8 2
A getCollectionOptionsKey() 0 8 2
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);
0 ignored issues
show
Documentation introduced by
$loader is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Twig_RuntimeLoaderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
    }
96
97
    private function setUpVersion3Plus()
98
    {
99
        $csrfProvider = $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock();
100
101
        // Add runtime loader
102
        $this->environment->addRuntimeLoader(new \Twig_FactoryRuntimeLoader(array(
103
            'Symfony\Bridge\Twig\Form\TwigRenderer' => function() use ($csrfProvider) {
104
                return new TwigRenderer($this->rendererEngine, $csrfProvider);
105
            },
106
            'Symfony\Component\Form\FormRenderer' => function() use ($csrfProvider) {
107
                return new FormRenderer($this->rendererEngine, $csrfProvider);
0 ignored issues
show
Documentation introduced by
$csrfProvider is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Symfony\Comp...fTokenManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
            },
109
        )));
110
111
        $this->renderer = $this->environment->getRuntime('Symfony\Bridge\Twig\Form\TwigRenderer');
112
113
        $this->environment->addExtension(new FormExtension());
114
    }
115
116
    /**
117
     * @return PreloadedExtension[]
118
     */
119
    protected function getExtensions()
120
    {
121
        return array(new PreloadedExtension(array(
122
            'tab' => new TabType(),
123
        ), array(
124
            $this->getFormType('form') => array(
125
                $this->getHelpFormTypeExtension(),
126
                $this->getWidgetFormTypeExtension(),
127
                $this->getLegendFormTypeExtension(),
128
                $this->getLayoutFormTypeExtension(),
129
                $this->getErrorTypeFormTypeExtension(),
130
                $this->getEmbedFormExtension(),
131
                $this->getTabbedFormTypeExtension(),
132
                $this->getWidgetCollectionFormTypeExtension(),
133
            ),
134
            $this->getFormType('text') => array(
135
                $this->getStaticTextFormTypeExtension(),
136
            ),
137
        )));
138
    }
139
140
    /**
141
     * @return HelpFormTypeExtension
142
     */
143
    protected function getHelpFormTypeExtension()
144
    {
145
        $popoverOptions = array(
146
            'title' => null,
147
            'content' => null,
148
            'text' => null,
149
            'trigger' => 'hover',
150
            'toggle' => 'popover',
151
            'icon' => 'info-sign',
152
            'placement' => 'right',
153
            'selector' => null,
154
        );
155
156
        $tooltipOptions = array(
157
            'title' => null,
158
            'text' => null,
159
            'icon' => 'info-sign',
160
            'placement' => 'top',
161
        );
162
163
        return new HelpFormTypeExtension(array(
164
            'help_block_popover' => $popoverOptions,
165
            'help_label_popover' => $popoverOptions,
166
            'help_widget_popover' => $popoverOptions,
167
            'help_block_tooltip' => $tooltipOptions,
168
            'help_label_tooltip' => $tooltipOptions,
169
        ));
170
    }
171
172
    /**
173
     * @return WidgetFormTypeExtension
174
     */
175
    protected function getWidgetFormTypeExtension()
176
    {
177
        return new WidgetFormTypeExtension(array(
178
            'checkbox_label' => 'both',
179
        ));
180
    }
181
182
    /**
183
     * @return LegendFormTypeExtension
184
     */
185
    protected function getLegendFormTypeExtension()
186
    {
187
        return new LegendFormTypeExtension(array(
188
            'render_fieldset' => true,
189
            'show_legend' => true,
190
            'show_child_legend' => false,
191
            'legend_tag' => 'legend',
192
            'render_required_asterisk' => false,
193
            'render_optional_text' => true,
194
        ));
195
    }
196
197
    /**
198
     * @return LayoutFormTypeExtension
199
     */
200
    protected function getLayoutFormTypeExtension()
201
    {
202
        return new LayoutFormTypeExtension(array(
203
            'layout' => 'horizontal',
204
            'horizontal_label_class' => 'col-sm-3',
205
            'horizontal_label_div_class' => null,
206
            'horizontal_label_offset_class' => 'col-sm-offset-3',
207
            'horizontal_input_wrapper_class' => 'col-sm-9',
208
        ));
209
    }
210
211
    /**
212
     * @return ErrorTypeFormTypeExtension
213
     */
214
    protected function getErrorTypeFormTypeExtension()
215
    {
216
        return new ErrorTypeFormTypeExtension(array(
217
            'error_type' => null,
218
        ));
219
    }
220
221
    /**
222
     * @return EmbedFormExtension
223
     */
224
    protected function getEmbedFormExtension()
225
    {
226
        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...
227
            'embed_form' => true,
228
        ));
229
    }
230
231
    /**
232
     * @return StaticTextExtension
233
     */
234
    protected function getStaticTextFormTypeExtension()
235
    {
236
        return new StaticTextExtension();
237
    }
238
239
    /**
240
     * @return TabbedFormTypeExtension
241
     */
242
    protected function getTabbedFormTypeExtension()
243
    {
244
        return new TabbedFormTypeExtension($this->tabFactory, array(
245
            'class' => 'tabs nav-tabs',
246
        ));
247
    }
248
249
    /**
250
     * @return WidgetCollectionFormTypeExtension
251
     */
252
    protected function getWidgetCollectionFormTypeExtension()
253
    {
254
        return new WidgetCollectionFormTypeExtension(array(
255
            'render_collection_item' => true,
256
            'widget_add_btn' => array(
257
                'attr' => array('class' => 'btn btn-default'),
258
                'label' => 'add-item',
259
                'icon' => null,
260
                'icon_inverted' => false,
261
            ),
262
            'widget_remove_btn' => array(
263
                'attr' => array('class' => 'btn btn-default'),
264
                'wrapper_div' => array('class' => 'form-group'),
265
                'horizontal_wrapper_div' => array('class' => 'col-sm-3 col-sm-offset-3'),
266
                'label' => 'remove-item',
267
                'icon' => null,
268
                'icon_inverted' => false,
269
            ),
270
        ));
271
    }
272
273
    /**
274
     * @param string $html
275
     * @param string $expression
276
     * @param int    $count
277
     */
278
    protected function assertMatchesXpath($html, $expression, $count = 1)
279
    {
280
        $dom = new \DomDocument('UTF-8');
281
        try {
282
            // Wrap in <root> node so we can load HTML with multiple tags at
283
            // the top level
284
            $dom->loadXml('<root>'.$html.'</root>');
285
        } catch (\Exception $e) {
286
            $this->fail(sprintf(
287
                "Failed loading HTML:\n\n%s\n\nError: %s",
288
                $html,
289
                $e->getMessage()
290
            ));
291
        }
292
        $xpath = new \DOMXPath($dom);
293
        $nodeList = $xpath->evaluate('/root'.$expression);
294
        if ($nodeList->length != $count) {
295
            $dom->formatOutput = true;
296
            $this->fail(sprintf(
297
                "Failed asserting that \n\n%s\n\nmatches exactly %s. Matches %s in \n\n%s",
298
                $expression,
299
                $count == 1 ? 'once' : $count.' times',
300
                $nodeList->length == 1 ? 'once' : $nodeList->length.' times',
301
                // strip away <root> and </root>
302
                substr($dom->saveHTML(), 6, -8)
303
            ));
304
        }
305
    }
306
307
    /**
308
     * @param string $html
309
     *
310
     * @return string
311
     */
312
    protected function removeBreaks($html)
313
    {
314
        return str_replace('&nbsp;', '', $html);
315
    }
316
317
    /**
318
     * @param FormView $view
319
     * @param array    $vars
320
     *
321
     * @return string
322
     */
323
    protected function renderForm(FormView $view, array $vars = array())
324
    {
325
        return (string) $this->renderer->renderBlock($view, 'form', $vars);
326
    }
327
328
    /**
329
     * @param FormView $view
330
     * @param array    $vars
331
     *
332
     * @return string
333
     */
334
    protected function renderRow(FormView $view, array $vars = array())
335
    {
336
        return (string) $this->renderer->searchAndRenderBlock($view, 'row', $vars);
337
    }
338
339
    /**
340
     * @param FormView $view
341
     * @param array    $vars
342
     *
343
     * @return string
344
     */
345
    protected function renderWidget(FormView $view, array $vars = array())
346
    {
347
        return (string) $this->renderer->searchAndRenderBlock($view, 'widget', $vars);
348
    }
349
350
    /**
351
     * @param FormView $view
352
     * @param string   $label
353
     * @param array    $vars
354
     *
355
     * @return string
356
     */
357
    protected function renderLabel(FormView $view, $label = null, array $vars = array())
358
    {
359
        if ($label !== null) {
360
            $vars += array('label' => $label);
361
        }
362
363
        return (string) $this->renderer->searchAndRenderBlock($view, 'label', $vars);
364
    }
365
366
    protected function getFormType($name)
367
    {
368
         if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
369
             return $this->formTypeMap[$name];
370
         }
371
372
         return $name;
373
    }
374
375
    protected function getCollectionTypeKey()
376
    {
377
         if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
378
             return 'entry_type';
379
         }
380
381
         return 'type';
382
    }
383
384
    protected function getCollectionOptionsKey()
385
    {
386
         if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
387
             return 'entry_options';
388
         }
389
390
         return 'options';
391
    }
392
}
393