ContainerBuilderTransformerTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 24.59 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 30
loc 122
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testAddingAlreadyExistingService() 0 18 1
A testTags() 15 15 1
A testAutowiringStep2() 15 15 1
A testPreventDuplicating() 0 20 1
A createContainerBuilderTransformer() 0 6 1
A transformFromNetteToSymfonyAndCompile() 0 9 1
A transformFromSymfonyToNette() 0 7 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
declare(strict_types=1);
4
5
namespace Symplify\NetteAdapterForSymfonyBundles\Tests\Transformer;
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\Common\Annotations\CachedReader;
9
use Doctrine\Common\Annotations\Reader;
10
use Nette\DI\ContainerBuilder;
11
use PHPUnit\Framework\TestCase;
12
use stdClass;
13
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
14
use Symplify\NetteAdapterForSymfonyBundles\Tests\Transformer\ContainerBuilderTransformerSource\AutowireReader;
15
use Symplify\NetteAdapterForSymfonyBundles\Transformer\ArgumentsTransformer;
16
use Symplify\NetteAdapterForSymfonyBundles\Transformer\ContainerBuilderTransformer;
17
use Symplify\NetteAdapterForSymfonyBundles\Transformer\ServiceDefinitionTransformer;
18
19
final class ContainerBuilderTransformerTest extends TestCase
20
{
21
    /**
22
     * @var ContainerBuilder
23
     */
24
    private $netteContainerBuilder;
25
26
    /**
27
     * @var SymfonyContainerBuilder
28
     */
29
    private $symfonyContainerBuilder;
30
31
    /**
32
     * @var ContainerBuilderTransformer
33
     */
34
    private $containerBuilderTransformer;
35
36
    protected function setUp()
37
    {
38
        $this->containerBuilderTransformer = $this->createContainerBuilderTransformer();
39
40
        $this->netteContainerBuilder = new ContainerBuilder();
41
        $this->symfonyContainerBuilder = new SymfonyContainerBuilder();
42
    }
43
44
    public function testAddingAlreadyExistingService()
45
    {
46
        $this->netteContainerBuilder->addDefinition('someservice')
47
            ->setClass(stdClass::class)
48
            ->setAutowired(false);
49
50
        $this->transformFromNetteToSymfonyAndCompile();
51
52
        $this->transformFromSymfonyToNette();
53
54
        $netteDefinition = $this->netteContainerBuilder->getDefinition('someservice');
55
        $this->assertSame(stdClass::class, $netteDefinition->getClass());
56
57
        $symfonyDefinition = $this->symfonyContainerBuilder->getDefinition('someservice');
58
        $this->assertSame(stdClass::class, $symfonyDefinition->getClass());
59
60
        $this->assertSame($netteDefinition->getClass(), $symfonyDefinition->getClass());
61
    }
62
63 View Code Duplication
    public function testTags()
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...
64
    {
65
        $netteDefinition = $this->netteContainerBuilder->addDefinition('someService')
66
            ->setClass(stdClass::class)
67
            ->addTag('someTag');
68
69
        $this->transformFromNetteToSymfonyAndCompile();
70
71
        $symfonyDefinition = $this->symfonyContainerBuilder->getDefinition('someService');
72
        $this->assertSame(['someTag' => [[true]]], $symfonyDefinition->getTags());
73
74
        $this->transformFromSymfonyToNette();
75
76
        $this->assertSame($netteDefinition, $this->netteContainerBuilder->getDefinition('someService'));
77
    }
78
79 View Code Duplication
    public function testAutowiringStep2()
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...
80
    {
81
        $netteDefinition = $this->netteContainerBuilder->addDefinition('someService')
82
            ->setClass(stdClass::class)
83
            ->addTag('someTag');
84
85
        $this->transformFromNetteToSymfonyAndCompile();
86
87
        $symfonyDefinition = $this->symfonyContainerBuilder->getDefinition('someService');
88
        $this->assertSame(['someTag' => [[true]]], $symfonyDefinition->getTags());
89
90
        $this->transformFromSymfonyToNette();
91
92
        $this->assertSame($netteDefinition, $this->netteContainerBuilder->getDefinition('someService'));
93
    }
94
95
    public function testPreventDuplicating()
96
    {
97
        $this->netteContainerBuilder->addDefinition('annotationReader')
98
            ->setClass(AnnotationReader::class)
99
            ->setAutowired(false);
100
101
        $this->netteContainerBuilder->addDefinition('reader')
102
            ->setClass(Reader::class)
103
            ->setFactory(CachedReader::class);
104
105
        $this->netteContainerBuilder->addDefinition('autowireReader')
106
            ->setClass(AutowireReader::class);
107
108
        $this->transformFromNetteToSymfonyAndCompile();
109
110
        $this->transformFromSymfonyToNette();
111
112
        $this->assertCount(3, $this->netteContainerBuilder->getDefinitions());
113
        $this->assertNotEmpty($this->netteContainerBuilder->getByType(Reader::class));
114
    }
115
116
    private function createContainerBuilderTransformer() : ContainerBuilderTransformer
117
    {
118
        $serviceDefinitionTransformer = new ServiceDefinitionTransformer(new ArgumentsTransformer());
119
120
        return new ContainerBuilderTransformer($serviceDefinitionTransformer);
121
    }
122
123
    private function transformFromNetteToSymfonyAndCompile()
124
    {
125
        $this->containerBuilderTransformer->transformFromNetteToSymfony(
126
            $this->netteContainerBuilder,
127
            $this->symfonyContainerBuilder
128
        );
129
130
        $this->symfonyContainerBuilder->compile();
131
    }
132
133
    private function transformFromSymfonyToNette()
134
    {
135
        $this->containerBuilderTransformer->transformFromSymfonyToNette(
136
            $this->symfonyContainerBuilder,
137
            $this->netteContainerBuilder
138
        );
139
    }
140
}
141