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:52
created

AbstractDivLayoutTest::setUpVersion2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

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