Completed
Pull Request — master (#792)
by
unknown
02:08
created

TestCaseAllPublicCompilerPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 17
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 14 5
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests;
4
5
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
6
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestType;
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use PHPUnit\Framework\TestCase as BaseTestCase;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
11
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Definition;
14
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
15
16
class TestCase extends BaseTestCase
17
{
18
    protected function setUp()
19
    {
20
        if (! class_exists('Doctrine\\Common\\Version')) {
21
            $this->markTestSkipped('Doctrine is not available.');
22
        }
23
    }
24
25
    public function createYamlBundleTestContainer()
26
    {
27
        $container = new ContainerBuilder(new ParameterBag([
28
            'kernel.name' => 'app',
29
            'kernel.debug' => false,
30
            'kernel.bundles' => ['YamlBundle' => 'Fixtures\Bundles\YamlBundle\YamlBundle'],
31
            'kernel.cache_dir' => sys_get_temp_dir(),
32
            'kernel.environment' => 'test',
33
            'kernel.root_dir' => __DIR__ . '/../../../../', // src dir
34
        ]));
35
        $container->set('annotation_reader', new AnnotationReader());
36
        $extension = new DoctrineExtension();
37
        $container->registerExtension($extension);
38
        $extension->load([[
39
            'dbal' => [
40
                'connections' => [
41
                    'default' => [
42
                        'driver' => 'pdo_mysql',
43
                        'charset' => 'UTF8',
44
                        'platform-service' => 'my.platform',
45
                    ],
46
                ],
47
                'default_connection' => 'default',
48
                'types' => [
49
                    'test' => TestType::class,
50
                ],
51
            ], 'orm' => [
52
                'default_entity_manager' => 'default',
53
                'entity_managers' => [
54
                    'default' => [
55
                        'mappings' => [
56
            'YamlBundle' => [
57
                            'type' => 'yml',
58
                            'dir' => __DIR__ . '/DependencyInjection/Fixtures/Bundles/YamlBundle/Resources/config/doctrine',
59
                            'prefix' => 'Fixtures\Bundles\YamlBundle\Entity',
60
                        ],
61
                        ],
62
                    ],
63
                ],
64
                'resolve_target_entities' => ['Symfony\Component\Security\Core\User\UserInterface' => 'stdClass'],
65
            ],
66
        ],
67
        ], $container);
68
69
        $container->setDefinition('my.platform', new Definition('Doctrine\DBAL\Platforms\MySqlPlatform'));
70
71
        $container->getCompilerPassConfig()->setOptimizationPasses([class_exists(ResolveChildDefinitionsPass::class) ? new ResolveChildDefinitionsPass() : new ResolveDefinitionTemplatesPass()]);
0 ignored issues
show
Documentation introduced by
array(class_exists(\Symf...initionTemplatesPass()) is of type array<integer,object<Sym...nitionTemplatesPass>"}>, but the function expects a array<integer,object<Sym...CompilerPassInterface>>.

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...
72
        $container->getCompilerPassConfig()->setRemovingPasses([]);
73
        // make all Doctrine services public, so we can fetch them in the test
74
        $container->getCompilerPassConfig()->addPass(new TestCaseAllPublicCompilerPass());
75
        $container->compile();
76
77
        return $container;
78
    }
79
}
80
81
class TestCaseAllPublicCompilerPass implements CompilerPassInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
82
{
83
    public function process(ContainerBuilder $container)
84
    {
85
        foreach ($container->getDefinitions() as $id => $definition) {
86
            if (false !== strpos($id, 'doctrine')) {
87
                $definition->setPublic(true);
88
            }
89
        }
90
91
        foreach ($container->getAliases() as $id => $alias) {
92
            if (false !== strpos($id, 'doctrine')) {
93
                $alias->setPublic(true);
94
            }
95
        }
96
    }
97
}