Completed
Push — master ( e26eba...178ca9 )
by Eric
37:26 queued 30:42
created

testProcessWithTranslatable()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 56
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 56
rs 9.7251
cc 3
eloc 43
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\TranslationBundle\Tests\DependencyInjection\Compiler;
13
14
use Lug\Bundle\TranslationBundle\DependencyInjection\Compiler\ConfigureFactoryPass;
15
use Lug\Component\Resource\Factory\Factory;
16
use Lug\Component\Translation\Factory\TranslatableFactory;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class ConfigureFactoryPassTest extends \PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * @var ConfigureFactoryPass
28
     */
29
    private $compiler;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function setUp()
35
    {
36
        $this->compiler = new ConfigureFactoryPass();
37
    }
38
39
    public function testProcessWithTranslatable()
40
    {
41
        $container = $this->createContainerBuilderMock();
42
        $container
43
            ->expects($this->once())
44
            ->method('findTaggedServiceIds')
45
            ->with($this->identicalTo('lug.factory'))
46
            ->will($this->returnValue([
47
                $factoryService = 'lug.factory.resource_name' => [['resource' => $resourceName = 'resource_name']],
48
            ]));
49
50
        $container
51
            ->expects($this->exactly(3))
52
            ->method('getDefinition')
53
            ->will($this->returnValueMap([
54
                [$factoryService, $factory = $this->createDefinitionMock()],
55
                [$resourceService = 'lug.resource.resource_name', $resource = $this->createDefinitionMock()],
56
                [$translationService = 'lug.resource.translation_name', $translation = $this->createDefinitionMock()],
57
            ]));
58
59
        $factory
60
            ->expects($this->once())
61
            ->method('getClass')
62
            ->will($this->returnValue(TranslatableFactory::class));
63
64
        $resource
65
            ->expects($this->once())
66
            ->method('getMethodCalls')
67
            ->will($this->returnValue([['addRelation', ['translation', $translationService]]]));
68
69
        $translation
70
            ->expects($this->once())
71
            ->method('getArgument')
72
            ->with($this->identicalTo(0))
73
            ->will($this->returnValue($translationName = 'translation_name'));
74
75
        $factory
76
            ->expects($this->at(1))
77
            ->method('addArgument')
78
            ->with($this->callback(function ($argument) {
79
                return $argument instanceof Reference
80
                    && (string) $argument === 'lug.translation.context.locale';
81
            }))
82
            ->will($this->returnSelf());
83
84
        $factory
85
            ->expects($this->at(2))
86
            ->method('addArgument')
87
            ->with($this->callback(function ($argument) use ($translationName) {
88
                return $argument instanceof Reference
89
                    && (string) $argument === 'lug.factory.'.$translationName;
90
            }))
91
            ->will($this->returnSelf());
92
93
        $this->compiler->process($container);
94
    }
95
96
    public function testProcessWithoutTranslatable()
97
    {
98
        $container = $this->createContainerBuilderMock();
99
        $container
100
            ->expects($this->once())
101
            ->method('findTaggedServiceIds')
102
            ->with($this->identicalTo('lug.factory'))
103
            ->will($this->returnValue([$service = 'service' => []]));
104
105
        $container
106
            ->expects($this->once())
107
            ->method('getDefinition')
108
            ->with($this->identicalTo($service))
109
            ->will($this->returnValue($factory = $this->createDefinitionMock()));
110
111
        $factory
112
            ->expects($this->once())
113
            ->method('getClass')
114
            ->will($this->returnValue(Factory::class));
115
116
        $factory
117
            ->expects($this->never())
118
            ->method('addArgument');
119
120
        $this->compiler->process($container);
121
    }
122
123
    /**
124
     * @return \PHPUnit_Framework_MockObject_MockObject|ContainerBuilder
125
     */
126
    private function createContainerBuilderMock()
127
    {
128
        return $this->createMock(ContainerBuilder::class);
129
    }
130
131
    /**
132
     * @return \PHPUnit_Framework_MockObject_MockObject|Definition
133
     */
134
    private function createDefinitionMock()
135
    {
136
        return $this->createMock(Definition::class);
137
    }
138
}
139