Completed
Pull Request — master (#40)
by Marko
445:50 queued 380:43
created

AbstractFOSCKEditorExtensionTest   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 373
Duplicated Lines 13.14 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 7
dl 49
loc 373
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 32 1
loadConfiguration() 0 1 ?
A testFormType() 0 23 1
A testFormTag() 0 12 2
A testDisable() 0 7 1
A testAsync() 0 7 1
A testAutoload() 0 7 1
A testAutoInline() 0 7 1
A testInline() 0 7 1
A testInputSync() 0 7 1
A testRequireJs() 0 7 1
A testJquery() 0 7 1
A testJqueryPath() 7 7 1
A testCustomPaths() 0 10 1
A testFilebrowsers() 10 10 1
A testSingleConfiguration() 17 17 1
A testMultipleConfiguration() 0 21 1
A testDefaultConfiguration() 15 15 1
A testPlugins() 0 14 1
B testStylesSets() 0 33 1
A testTemplates() 0 23 1
B testToolbars() 0 33 1
A testInvalidDefaultConfig() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Ivory CKEditor package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\CKEditorBundle\Tests\DependencyInjection;
13
14
use FOS\CKEditorBundle\DependencyInjection\FOSCKEditorExtension;
15
use FOS\CKEditorBundle\Form\Type\CKEditorType;
16
use FOS\CKEditorBundle\FOSCKEditorBundle;
17
use FOS\CKEditorBundle\Tests\AbstractTestCase;
18
use FOS\CKEditorBundle\Tests\DependencyInjection\Compiler\TestContainerPass;
19
use Symfony\Component\Asset\Packages;
20
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\Form\AbstractType;
23
use Symfony\Component\Form\FormRendererInterface;
24
use Symfony\Component\HttpFoundation\RequestStack;
25
use Symfony\Component\Routing\RouterInterface;
26
use Symfony\Component\Templating\EngineInterface;
27
use Symfony\Component\Templating\Helper\CoreAssetsHelper;
28
29
/**
30
 * @author GeLo <[email protected]>
31
 * @author Adam Misiorny <[email protected]>
32
 */
33
abstract class AbstractFOSCKEditorExtensionTest extends AbstractTestCase
34
{
35
    /**
36
     * @var ContainerBuilder
37
     */
38
    private $container;
39
    /**
40
     * @var Packages|CoreAssetsHelper|\PHPUnit_Framework_MockObject_MockObject
41
     */
42
    private $packages;
43
    /**
44
     * @var RouterInterface|\PHPUnit_Framework_MockObject_MockObject
45
     */
46
    private $router;
47
    /**
48
     * @var FormRendererInterface|\PHPUnit_Framework_MockObject_MockObject
49
     */
50
    private $formRenderer;
51
    /**
52
     * @var RequestStack|\PHPUnit_Framework_MockObject_MockObject
53
     */
54
    private $requestStack;
55
    /**
56
     * @var EngineInterface
57
     */
58
    private $templating;
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function setUp()
64
    {
65
        $this->router = $this->createMock(RouterInterface::class);
66
        $this->formRenderer = $this->createMock(FormRendererInterface::class);
67
        $this->packages = $this->getMockBuilder(Packages::class)
68
            ->disableOriginalConstructor()
69
            ->getMock();
70
        $this->requestStack = $this->createMock(RequestStack::class);
71
        $this->container = new ContainerBuilder();
72
        $this->templating = $this->createMock(EngineInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...EngineInterface::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...lating\EngineInterface> of property $templating.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
74
        $this->container->set('assets.packages', $this->packages);
75
        $this->container->set('router', $this->router);
76
        $this->container->set('templating.form.renderer', $this->formRenderer);
77
        $this->container->set('twig.form.renderer', $this->formRenderer);
78
        $this->container->set('request_stack', $this->requestStack);
79
        $this->container->set('templating', $this->templating);
80
81
        $this->container->registerExtension($extension = new FOSCKEditorExtension());
82
        $this->container->loadFromExtension($extension->getAlias());
83
84
        $toBePublic = [
85
            'fosck_editor.template_manager',
86
            'fosck_editor.form.type',
87
            'fosck_editor.config_manager',
88
            'fosck_editor.plugin_manager',
89
            'fosck_editor.styles_set_manager',
90
            'fosck_editor.toolbar_manager',
91
        ];
92
        $this->container->addCompilerPass(new TestContainerPass($toBePublic), PassConfig::TYPE_OPTIMIZE);
93
        (new FOSCKEditorBundle())->build($this->container);
94
    }
95
96
    /**
97
     * @param ContainerBuilder $container
98
     * @param string           $configuration
99
     */
100
    abstract protected function loadConfiguration(ContainerBuilder $container, $configuration);
101
102
    public function testFormType()
103
    {
104
        $this->container->compile();
105
106
        $type = $this->container->get('fosck_editor.form.type');
107
108
        $this->assertInstanceOf(CKEditorType::class, $type);
109
        $this->assertTrue($type->isEnable());
110
        $this->assertTrue($type->isAutoload());
111
        $this->assertTrue($type->isAutoInline());
112
        $this->assertFalse($type->isInline());
113
        $this->assertFalse($type->useJquery());
114
        $this->assertFalse($type->isInputSync());
115
        $this->assertFalse($type->useRequireJs());
116
        $this->assertFalse($type->hasFilebrowsers());
117
        $this->assertSame('bundles/fosckeditor/', $type->getBasePath());
118
        $this->assertSame('bundles/fosckeditor/ckeditor.js', $type->getJsPath());
119
        $this->assertSame('bundles/fosckeditor/adapters/jquery.js', $type->getJqueryPath());
120
        $this->assertSame($this->container->get('fosck_editor.config_manager'), $type->getConfigManager());
121
        $this->assertSame($this->container->get('fosck_editor.plugin_manager'), $type->getPluginManager());
122
        $this->assertSame($this->container->get('fosck_editor.styles_set_manager'), $type->getStylesSetManager());
123
        $this->assertSame($this->container->get('fosck_editor.template_manager'), $type->getTemplateManager());
124
    }
125
126
    public function testFormTag()
127
    {
128
        $this->container->compile();
129
130
        $tag = $this->container->getDefinition('fosck_editor.form.type')->getTag('form.type');
131
132
        if (!method_exists(AbstractType::class, 'getBlockPrefix')) {
133
            $this->assertSame([['alias' => 'ckeditor']], $tag);
134
        } else {
135
            $this->assertSame([[]], $tag);
136
        }
137
    }
138
139
    public function testDisable()
140
    {
141
        $this->loadConfiguration($this->container, 'disable');
142
        $this->container->compile();
143
144
        $this->assertFalse($this->container->get('fosck_editor.form.type')->isEnable());
145
    }
146
147
    public function testAsync()
148
    {
149
        $this->loadConfiguration($this->container, 'async');
150
        $this->container->compile();
151
152
        $this->assertTrue($this->container->get('fosck_editor.form.type')->isAsync());
153
    }
154
155
    public function testAutoload()
156
    {
157
        $this->loadConfiguration($this->container, 'autoload');
158
        $this->container->compile();
159
160
        $this->assertFalse($this->container->get('fosck_editor.form.type')->isAutoload());
161
    }
162
163
    public function testAutoInline()
164
    {
165
        $this->loadConfiguration($this->container, 'auto_inline');
166
        $this->container->compile();
167
168
        $this->assertFalse($this->container->get('fosck_editor.form.type')->isAutoInline());
169
    }
170
171
    public function testInline()
172
    {
173
        $this->loadConfiguration($this->container, 'inline');
174
        $this->container->compile();
175
176
        $this->assertTrue($this->container->get('fosck_editor.form.type')->isInline());
177
    }
178
179
    public function testInputSync()
180
    {
181
        $this->loadConfiguration($this->container, 'input_sync');
182
        $this->container->compile();
183
184
        $this->assertTrue($this->container->get('fosck_editor.form.type')->isInputSync());
185
    }
186
187
    public function testRequireJs()
188
    {
189
        $this->loadConfiguration($this->container, 'require_js');
190
        $this->container->compile();
191
192
        $this->assertTrue($this->container->get('fosck_editor.form.type')->useRequireJs());
193
    }
194
195
    public function testJquery()
196
    {
197
        $this->loadConfiguration($this->container, 'jquery');
198
        $this->container->compile();
199
200
        $this->assertTrue($this->container->get('fosck_editor.form.type')->useJquery());
201
    }
202
203 View Code Duplication
    public function testJqueryPath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
204
    {
205
        $this->loadConfiguration($this->container, 'jquery_path');
206
        $this->container->compile();
207
208
        $this->assertSame('foo/jquery.js', $this->container->get('fosck_editor.form.type')->getJqueryPath());
209
    }
210
211
    public function testCustomPaths()
212
    {
213
        $this->loadConfiguration($this->container, 'custom_paths');
214
        $this->container->compile();
215
216
        $ckEditorType = $this->container->get('fosck_editor.form.type');
217
218
        $this->assertSame('foo/', $ckEditorType->getBasePath());
219
        $this->assertSame('foo/ckeditor.js', $ckEditorType->getJsPath());
220
    }
221
222 View Code Duplication
    public function testFilebrowsers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
223
    {
224
        $this->loadConfiguration($this->container, 'filebrowsers');
225
        $this->container->compile();
226
227
        $this->assertSame(
228
            ['VideoBrowse', 'VideoUpload'],
229
            $this->container->get('fosck_editor.form.type')->getFilebrowsers()
230
        );
231
    }
232
233 View Code Duplication
    public function testSingleConfiguration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
234
    {
235
        $this->loadConfiguration($this->container, 'single_configuration');
236
        $this->container->compile();
237
238
        $configManager = $this->container->get('fosck_editor.config_manager');
239
240
        $expected = [
241
            'default' => [
242
                'toolbar' => 'default',
243
                'uiColor' => '#000000',
244
            ],
245
        ];
246
247
        $this->assertSame('default', $configManager->getDefaultConfig());
248
        $this->assertSame($expected, $configManager->getConfigs());
249
    }
250
251
    public function testMultipleConfiguration()
252
    {
253
        $this->loadConfiguration($this->container, 'multiple_configuration');
254
        $this->container->compile();
255
256
        $configManager = $this->container->get('fosck_editor.config_manager');
257
258
        $expected = [
259
            'default' => [
260
                'toolbar' => 'default',
261
                'uiColor' => '#000000',
262
            ],
263
            'custom' => [
264
                'toolbar' => 'custom',
265
                'uiColor' => '#ffffff',
266
            ],
267
        ];
268
269
        $this->assertSame('default', $configManager->getDefaultConfig());
270
        $this->assertSame($expected, $configManager->getConfigs());
271
    }
272
273 View Code Duplication
    public function testDefaultConfiguration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
274
    {
275
        $this->loadConfiguration($this->container, 'default_configuration');
276
        $this->container->compile();
277
278
        $configManager = $this->container->get('fosck_editor.config_manager');
279
280
        $expected = [
281
            'default' => ['uiColor' => '#000000'],
282
            'custom' => ['uiColor' => '#ffffff'],
283
        ];
284
285
        $this->assertSame('default', $configManager->getDefaultConfig());
286
        $this->assertSame($expected, $configManager->getConfigs());
287
    }
288
289
    public function testPlugins()
290
    {
291
        $this->loadConfiguration($this->container, 'plugins');
292
        $this->container->compile();
293
294
        $expected = [
295
            'plugin-name' => [
296
                'path' => '/my/path',
297
                'filename' => 'plugin.js',
298
            ],
299
        ];
300
301
        $this->assertSame($expected, $this->container->get('fosck_editor.plugin_manager')->getPlugins());
302
    }
303
304
    public function testStylesSets()
305
    {
306
        $this->loadConfiguration($this->container, 'styles_sets');
307
        $this->container->compile();
308
309
        $expected = [
310
            'styles-set-name' => [
311
                [
312
                    'name' => 'Blue Title',
313
                    'element' => 'h2',
314
                    'styles' => ['text-decoration' => 'underline'],
315
                ],
316
                [
317
                    'name' => 'CSS Style',
318
                    'element' => 'span',
319
                    'attributes' => ['data-class' => 'my-style'],
320
                ],
321
                [
322
                    'name' => 'Widget Style',
323
                    'type' => 'widget',
324
                    'widget' => 'my-widget',
325
                    'attributes' => ['data-class' => 'my-style'],
326
                ],
327
                [
328
                    'name' => 'Multiple Elements Style',
329
                    'element' => ['span', 'p', 'h3'],
330
                    'attributes' => ['data-class' => 'my-style'],
331
                ],
332
            ],
333
        ];
334
335
        $this->assertSame($expected, $this->container->get('fosck_editor.styles_set_manager')->getStylesSets());
336
    }
337
338
    public function testTemplates()
339
    {
340
        $this->loadConfiguration($this->container, 'templates');
341
        $this->container->compile();
342
343
        $expected = [
344
            'template-name' => [
345
                'imagesPath' => '/my/path',
346
                'templates' => [
347
                    [
348
                        'title' => 'My Template',
349
                        'image' => 'image.jpg',
350
                        'description' => 'My awesome description',
351
                        'html' => '<h1>Template</h1><p>Type your text here.</p>',
352
                        'template' => 'AppBundle:CKEditor:template.html.twig',
353
                        'template_parameters' => ['foo' => 'bar'],
354
                    ],
355
                ],
356
            ],
357
        ];
358
359
        $this->assertSame($expected, $this->container->get('fosck_editor.template_manager')->getTemplates());
360
    }
361
362
    public function testToolbars()
363
    {
364
        $this->loadConfiguration($this->container, 'toolbars');
365
        $this->container->compile();
366
367
        $toolbarManager = $this->container->get('fosck_editor.toolbar_manager');
368
369
        $this->assertSame(
370
            [
371
                'document' => ['Source', '-', 'Save'],
372
                'tools' => ['Maximize'],
373
            ],
374
            array_intersect_key($toolbarManager->getItems(), ['document' => true, 'tools' => true])
375
        );
376
377
        $this->assertSame(
378
            [
379
                'default' => [
380
                    '@document',
381
                    '/',
382
                    ['Anchor'],
383
                    '/',
384
                    '@tools',
385
                ],
386
                'custom' => [
387
                    '@document',
388
                    '/',
389
                    ['Anchor'],
390
                ],
391
            ],
392
            array_intersect_key($toolbarManager->getToolbars(), ['default' => true, 'custom' => true])
393
        );
394
    }
395
396
    /**
397
     * @expectedException \FOS\CKEditorBundle\Exception\DependencyInjectionException
398
     * @expectedExceptionMessage The default config "bar" does not exist.
399
     */
400
    public function testInvalidDefaultConfig()
401
    {
402
        $this->loadConfiguration($this->container, 'invalid_default_config');
403
        $this->container->compile();
404
    }
405
}
406