Completed
Push — master ( ba9418...009852 )
by Tomáš
05:39
created

ContainerBuilderTransformerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 24.59 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

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
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
        $this->assertNotEmpty($this->netteContainerBuilder->getByType(Reader::class));
112
    }
113
114
    private function createContainerBuilderTransformer() : ContainerBuilderTransformer
115
    {
116
        $serviceDefinitionTransformer = new ServiceDefinitionTransformer(new ArgumentsTransformer());
117
118
        return new ContainerBuilderTransformer($serviceDefinitionTransformer);
119
    }
120
121
    private function transformFromNetteToSymfonyAndCompile()
122
    {
123
        $this->containerBuilderTransformer->transformFromNetteToSymfony(
124
            $this->netteContainerBuilder,
125
            $this->symfonyContainerBuilder
126
        );
127
128
        $this->symfonyContainerBuilder->compile();
129
    }
130
131
    private function transformFromSymfonyToNette()
132
    {
133
        $this->containerBuilderTransformer->transformFromSymfonyToNette(
134
            $this->symfonyContainerBuilder,
135
            $this->netteContainerBuilder
136
        );
137
    }
138
}
139