Completed
Push — master ( 5302a2...ba9418 )
by Tomáš
06:32 queued 04:07
created

ContainerBuilderTransformerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 127
Duplicated Lines 23.62 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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