Completed
Push — master ( 6a145e...f79650 )
by Johannes
16:26
created

Tests/DependencyInjection/DoctrinePassTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\DoctrinePass;
22
use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
23
use PHPUnit\Framework\TestCase;
24
use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
25
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
26
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
28
29
class DoctrinePassTest extends TestCase
30
{
31
    /**
32
     *
33
     * @param array $configs
34
     * @return ContainerBuilder
35
     */
36 View Code Duplication
    private function getContainer(array $configs = array())
0 ignored issues
show
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...
37
    {
38
        $loader = new JMSSerializerExtension();
39
        $container = new ContainerBuilder();
40
41
42
        $container->getCompilerPassConfig()->setOptimizationPasses(array(
43
            new ResolveParameterPlaceHoldersPass(),
44
            new ResolveDefinitionTemplatesPass(),
45
        ));
46
        $container->getCompilerPassConfig()->setRemovingPasses(array(new RemoveUnusedDefinitionsPass()));
47
48
        $container->setParameter('kernel.debug', true);
49
        $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer');
50
        $container->setParameter('kernel.bundles', array());
51
52
        $loader->load(['jms_serializer' => $configs], $container);
53
        return $container;
54
    }
55
56
    public function testDoctrineDisabled()
57
    {
58
        $container = $this->getContainer(array(
59
            'metadata' => array('infer_types_from_doctrine_metadata' => false)
60
        ));
61
        $container->register('doctrine.orm.entity_manager');
62
63
        $pass = new DoctrinePass();
64
        $pass->process($container);
65
66
        $this->assertEquals('jms_serializer.unserialize_object_constructor', (string)$container->getAlias('jms_serializer.object_constructor'));
67
    }
68
69
    public function testOrm()
70
    {
71
        $container = $this->getContainer();
72
        $container->register('doctrine.orm.entity_manager');
73
74
        $pass = new DoctrinePass();
75
        $pass->process($container);
76
77
        $this->assertEquals('jms_serializer.doctrine_object_constructor', (string)$container->getAlias('jms_serializer.object_constructor'));
78
    }
79
80
    public function testOdm()
81
    {
82
        $container = $this->getContainer();
83
        $container->register('doctrine_phpcr.odm.document_manager');
84
85
        $pass = new DoctrinePass();
86
        $pass->process($container);
87
88
        $this->assertEquals('jms_serializer.doctrine_phpcr_object_constructor', (string)$container->getAlias('jms_serializer.object_constructor'));
89
90
        $def = $container->getDefinition('jms_serializer.doctrine_phpcr_object_constructor');
91
        $this->assertEquals('jms_serializer.unserialize_object_constructor', (string)$def->getArgument(1));
92
    }
93
94
    public function testOrmAndOdm()
95
    {
96
        $container = $this->getContainer();
97
98
        $container->register('doctrine_phpcr.odm.document_manager');
99
        $container->register('doctrine.orm.entity_manager');
100
101
        $pass = new DoctrinePass();
102
        $pass->process($container);
103
104
        $this->assertEquals('jms_serializer.doctrine_object_constructor', (string)$container->getAlias('jms_serializer.object_constructor'));
105
106
        $def = $container->getDefinition('jms_serializer.doctrine_object_constructor');
107
        $this->assertEquals('jms_serializer.doctrine_phpcr_object_constructor', (string)$def->getArgument(1));
108
109
        $def = $container->getDefinition('jms_serializer.doctrine_phpcr_object_constructor');
110
        $this->assertEquals('jms_serializer.unserialize_object_constructor', (string)$def->getArgument(1));
111
    }
112
}
113
114