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
Push — master ( df0e42...d30a6d )
by Craig
01:54
created

AbstractDivLayoutTest::assertMatchesXpath()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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