Completed
Push — master ( 203e38...2cdd54 )
by Gerrit
13:04
created

CallDefinitionExecuterTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 164
Duplicated Lines 39.02 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 64
loc 164
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
B shouldExecuteCallDefinitionOnEntity() 32 32 1
B shouldExecuteCallDefinitionOnStaticEntity() 32 32 1
A createValueObject() 0 4 1
A shouldExecuteCallDefinitionOnService() 0 48 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
 * Copyright (C) 2017  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\Tests\ValueResolver;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\ValueResolver\CallDefinitionExecuter;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface;
17
use Addiks\RDMBundle\Mapping\CallDefinitionInterface;
18
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
19
use Addiks\RDMBundle\Tests\ValueObjectExample;
20
use Addiks\RDMBundle\Tests\Hydration\ServiceExample;
21
use Addiks\RDMBundle\Mapping\MappingInterface;
22
23
final class CallDefinitionExecuterTest extends TestCase
24
{
25
26
    /**
27
     * @var CallDefinitionExecuter
28
     */
29
    private $callDefinitionExecuter;
30
31
    /**
32
     * @var ContainerInterface
33
     */
34
    private $container;
35
36
    /**
37
     * @var ValueResolverInterface
38
     */
39
    private $argumentResolver;
40
41
    /**
42
     * @var null|ValueObjectExample
43
     */
44
    private static $valueObjectToCreate;
45
46
    public function setUp()
47
    {
48
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...tainerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
        $this->argumentResolver = $this->createMock(ValueResolverInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...solverInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\RDMBundle\...ValueResolverInterface> of property $argumentResolver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
51
        $this->callDefinitionExecuter = new CallDefinitionExecuter(
52
            $this->container,
0 ignored issues
show
Documentation introduced by
$this->container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...ion\ContainerInterface>.

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...
53
            $this->argumentResolver
0 ignored issues
show
Documentation introduced by
$this->argumentResolver is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\...ValueResolverInterface>.

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...
54
        );
55
    }
56
57
    /**
58
     * @test
59
     */
60 View Code Duplication
    public function shouldExecuteCallDefinitionOnEntity()
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...
61
    {
62
        /** @var CallDefinitionInterface $callDefinition */
63
        $callDefinition = $this->createMock(CallDefinitionInterface::class);
64
65
        /** @var EntityExample $entity */
66
        $entity = $this->createMock(EntityExample::class);
67
68
        /** @var array<string> $dataFromAdditionalColumns */
69
        $dataFromAdditionalColumns = array(
70
            'lorem' => 'ipsum',
71
            'dolor' => 'sit amet'
72
        );
73
74
        $callDefinition->method('getObjectReference')->willReturn('self');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...allDefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
        $callDefinition->method('getRoutineName')->willReturn('getBoo');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...allDefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
        $callDefinition->method('getArgumentMappings')->willReturn([]);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...allDefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
78
        /** @var ValueObjectExample $expectedResult */
79
        $expectedResult = $this->createMock(ValueObjectExample::class);
80
81
        $entity->method('getBoo')->willReturn($expectedResult);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ydration\EntityExample>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
83
        /** @var mixed $actualResult */
84
        $actualResult = $this->callDefinitionExecuter->executeCallDefinition(
85
            $callDefinition,
86
            $entity,
87
            $dataFromAdditionalColumns
88
        );
89
90
        $this->assertSame($expectedResult, $actualResult);
91
    }
92
93
    /**
94
     * @test
95
     */
96 View Code Duplication
    public function shouldExecuteCallDefinitionOnStaticEntity()
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...
97
    {
98
        /** @var CallDefinitionInterface $callDefinition */
99
        $callDefinition = $this->createMock(CallDefinitionInterface::class);
100
101
        /** @var EntityExample $entity */
102
        $entity = $this->createMock(EntityExample::class);
103
104
        /** @var array<string> $dataFromAdditionalColumns */
105
        $dataFromAdditionalColumns = array(
106
            'lorem' => 'ipsum',
107
            'dolor' => 'sit amet'
108
        );
109
110
        $callDefinition->method('getObjectReference')->willReturn(__CLASS__);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...allDefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
        $callDefinition->method('getRoutineName')->willReturn('createValueObject');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...allDefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
        $callDefinition->method('getArgumentMappings')->willReturn([]);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...allDefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
114
        /** @var ValueObjectExample $expectedResult */
115
        $expectedResult = $this->createMock(ValueObjectExample::class);
116
117
        self::$valueObjectToCreate = $expectedResult;
118
119
        /** @var mixed $actualResult */
120
        $actualResult = $this->callDefinitionExecuter->executeCallDefinition(
121
            $callDefinition,
122
            $entity,
123
            $dataFromAdditionalColumns
124
        );
125
126
        $this->assertSame($expectedResult, $actualResult);
127
    }
128
129
    public static function createValueObject()
130
    {
131
        return self::$valueObjectToCreate;
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function shouldExecuteCallDefinitionOnService()
138
    {
139
        /** @var CallDefinitionInterface $callDefinition */
140
        $callDefinition = $this->createMock(CallDefinitionInterface::class);
141
142
        /** @var EntityExample $entity */
143
        $entity = $this->createMock(EntityExample::class);
144
145
        /** @var ValueObjectExample $expectedResult */
146
        $expectedResult = $this->createMock(ValueObjectExample::class);
147
148
        /** @var ServiceExample $factoryService */
149
        $factoryService = $this->createMock(ServiceExample::class);
150
151
        /** @var array<string> $dataFromAdditionalColumns */
152
        $dataFromAdditionalColumns = array(
153
            'lorem' => 'ipsum',
154
            'dolor' => 'sit amet'
155
        );
156
157
        /** @var MappingInterface $argumentMapping */
158
        $argumentMapping = $this->createMock(MappingInterface::class);
159
160
        $callDefinition->method('getObjectReference')->willReturn('@foo_service');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...allDefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
161
        $callDefinition->method('getRoutineName')->willReturn('getLorem');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...allDefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
        $callDefinition->method('getArgumentMappings')->willReturn([$argumentMapping]);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...allDefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
163
164
        $this->argumentResolver->method('resolveValue')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ValueResolverInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
            [$argumentMapping, $entity, $dataFromAdditionalColumns, 'foo'],
166
        ]));
167
168
        $factoryService->method('getLorem')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...dration\ServiceExample>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
            ['foo', $expectedResult],
170
        ]));
171
172
        $this->container->method('get')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
173
            ['foo_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $factoryService],
174
        ]));
175
176
        /** @var mixed $actualResult */
177
        $actualResult = $this->callDefinitionExecuter->executeCallDefinition(
178
            $callDefinition,
179
            $entity,
180
            $dataFromAdditionalColumns
181
        );
182
183
        $this->assertSame($expectedResult, $actualResult);
184
    }
185
186
}
187