TwigExtensionPassTest::getContainer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 12
loc 12
rs 9.4285
c 2
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/*
4
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\SerializerBundle\Tests\DependencyInjection;
20
21
use JMS\SerializerBundle\DependencyInjection\Compiler\TwigExtensionPass;
22
use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
23
use PHPUnit\Framework\TestCase;
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
26
class TwigExtensionPassTest extends TestCase
27
{
28
    /**
29
     * @return ContainerBuilder
30
     */
31 View Code Duplication
    private function getContainer()
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...
32
    {
33
        $loader = new JMSSerializerExtension();
34
        $container = new ContainerBuilder();
35
36
        $container->setParameter('kernel.debug', true);
37
        $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer');
38
        $container->setParameter('kernel.bundles', array());
39
40
        $loader->load([[]], $container);
41
        return $container;
42
    }
43
44
    public function testStandardExtension()
45
    {
46
        $container = $this->getContainer();
47
48
        $pass = new TwigExtensionPass();
49
        $pass->process($container);
50
51
        $extension = $container->getDefinition('jms_serializer.twig_extension.serializer');
52
        $this->assertEquals('%jms_serializer.twig_extension.class%', (string)$extension->getClass());
53
        $this->assertCount(1, $extension->getArguments());
54
55
        $this->assertFalse($container->hasDefinition('jms_serializer.twig_extension.serializer_runtime_helper'));
56
    }
57
58
    public function testLazyExtension()
59
    {
60
        if (
61
            !class_exists('JMS\Serializer\Twig\SerializerRuntimeExtension')
62
            || !interface_exists('Twig_RuntimeLoaderInterface')
63
        ) {
64
            $this->markTestSkipped("Lazy extensions are supported only by serializer 1.7.0");
65
        }
66
        $container = $this->getContainer();
67
68
        $container->register('twig.runtime_loader');
69
70
        $pass = new TwigExtensionPass();
71
        $pass->process($container);
72
73
        $extension = $container->getDefinition('jms_serializer.twig_extension.serializer');
74
        $this->assertEquals('%jms_serializer.twig_runtime_extension.class%', (string)$extension->getClass());
75
        $this->assertCount(0, $extension->getArguments());
76
77
        $this->assertTrue($container->hasDefinition('jms_serializer.twig_extension.serializer_runtime_helper'));
78
    }
79
80
    public function testLazyExtensionNotLoadedWhenOldSerializer()
81
    {
82
        $container = $this->getContainer();
83
84
        $container->getParameterBag()->add(array(
85
            'jms_serializer.twig_runtime_extension.class' => 'foo',
86
            'jms_serializer.twig_runtime_extension_helper.class' => 'bar',
87
        ));
88
89
        $container->register('twig.runtime_loader');
90
91
        $pass = new TwigExtensionPass();
92
        $pass->process($container);
93
94
        $extension = $container->getDefinition('jms_serializer.twig_extension.serializer');
95
        $this->assertEquals('%jms_serializer.twig_extension.class%', (string)$extension->getClass());
96
        $this->assertCount(1, $extension->getArguments());
97
98
        $this->assertFalse($container->hasDefinition('jms_serializer.twig_extension.serializer_runtime_helper'));
99
    }
100
}
101
102