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

ObjectValueResolverTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 131
Duplicated Lines 45.8 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 60
loc 131
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 1
B shouldResolveValue() 32 32 1
B shouldRevertValue() 28 28 1
A shouldFailAssertionOnWrongObject() 0 22 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\ObjectValueResolver;
15
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Addiks\RDMBundle\Tests\ValueObjectExample;
18
use Addiks\RDMBundle\Mapping\ObjectMappingInterface;
19
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
20
use Addiks\RDMBundle\ValueResolver\CallDefinitionExecuterInterface;
21
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface;
22
use Addiks\RDMBundle\Mapping\MappingInterface;
23
24
final class ObjectValueResolverTest extends TestCase
25
{
26
27
    /**
28
     * @var ObjectValueResolver
29
     */
30
    private $valueResolver;
31
32
    /**
33
     * @var ContainerInterface
34
     */
35
    private $container;
36
37
    /**
38
     * @var ValueResolverInterface
39
     */
40
    private $fieldValueResolver;
41
42
    /**
43
     * @var CallDefinitionExecuterInterface
44
     */
45
    private $callDefinitionExecuter;
46
47
    public function setUp()
48
    {
49
        $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...
50
        $this->fieldValueResolver = $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 $fieldValueResolver.

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...
51
        $this->callDefinitionExecuter = $this->createMock(CallDefinitionExecuterInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...ecuterInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\RDMBundle\...itionExecuterInterface> of property $callDefinitionExecuter.

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...
52
53
        $this->valueResolver = new ObjectValueResolver(
54
            $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...
55
            $this->fieldValueResolver,
0 ignored issues
show
Documentation introduced by
$this->fieldValueResolver 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...
56
            $this->callDefinitionExecuter
0 ignored issues
show
Documentation introduced by
$this->callDefinitionExecuter is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\...itionExecuterInterface>.

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...
57
        );
58
    }
59
60
    /**
61
     * @test
62
     */
63 View Code Duplication
    public function shouldResolveValue()
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
        /** @var ObjectMappingInterface $objectMapping */
66
        $objectMapping = $this->createMock(ObjectMappingInterface::class);
67
68
        /** @var MappingInterface $fieldMapping */
69
        $fieldMapping = $this->createMock(MappingInterface::class);
70
71
        /** @var EntityExample $entity */
72
        $entity = $this->createMock(EntityExample::class);
73
74
        /** @var mixed $dataFromAdditionalColumns */
75
        $dataFromAdditionalColumns = array(
76
            'lorem' => 'ipsum',
77
            'dolor' => 'sit amet',
78
        );
79
80
        $objectMapping->method('getClassName')->willReturn(ValueObjectExample::class);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ObjectMappingInterface>.

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...
81
        $objectMapping->method('getFieldMappings')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ObjectMappingInterface>.

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
            'amet' => $fieldMapping
83
        ]);
84
85
        $this->fieldValueResolver->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...
86
            [$fieldMapping, $entity, $dataFromAdditionalColumns, "FOO BAR BAZ"],
87
        ]));
88
89
        /** @var mixed $actualObject */
90
        $actualObject = $this->valueResolver->resolveValue($objectMapping, $entity, $dataFromAdditionalColumns);
91
92
        $this->assertTrue($actualObject instanceof ValueObjectExample);
93
        $this->assertEquals("FOO BAR BAZ", $actualObject->getAmet());
94
    }
95
96
    /**
97
     * @test
98
     */
99 View Code Duplication
    public function shouldRevertValue()
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...
100
    {
101
        /** @var ObjectMappingInterface $objectMapping */
102
        $objectMapping = $this->createMock(ObjectMappingInterface::class);
103
104
        /** @var MappingInterface $fieldMapping */
105
        $fieldMapping = $this->createMock(MappingInterface::class);
106
107
        /** @var EntityExample $entity */
108
        $entity = $this->createMock(EntityExample::class);
109
110
        $actualObject = new ValueObjectExample("foo");
111
        $actualObject->setAmet('sit amet');
112
113
        $objectMapping->method('getClassName')->willReturn(ValueObjectExample::class);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ObjectMappingInterface>.

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...
114
        $objectMapping->method('getFieldMappings')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ObjectMappingInterface>.

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...
115
            'amet' => $fieldMapping
116
        ]);
117
118
        $this->fieldValueResolver->method('revertValue')->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...
119
            [$fieldMapping, $entity, 'sit amet', ['amet' => "FOO BAR BAZ"]],
120
        ]));
121
122
        /** @var array $actualData */
123
        $actualData = $this->valueResolver->revertValue($objectMapping, $entity, $actualObject);
124
125
        $this->assertEquals(['amet' => "FOO BAR BAZ"], $actualData);
126
    }
127
128
    /**
129
     * @test
130
     */
131
    public function shouldFailAssertionOnWrongObject()
132
    {
133
        /** @var ObjectMappingInterface $objectMapping */
134
        $objectMapping = $this->createMock(ObjectMappingInterface::class);
135
136
        /** @var EntityExample $entity */
137
        $entity = $this->createMock(EntityExample::class);
138
139
        $dataFromAdditionalColumns = array(
140
            'lorem' => 'ipsum',
141
            'dolor' => 'sit amet',
142
        );
143
144
        /** @var mixed $actualValue */
145
        $actualValue = $this->createMock(EntityExample::class);
146
147
        $objectMapping->method('getClassName')->willReturn(ValueObjectExample::class);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ObjectMappingInterface>.

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...
148
149
        $this->expectException(FailedRDMAssertionExceptionInterface::class);
150
151
        $this->valueResolver->assertValue($objectMapping, $entity, $dataFromAdditionalColumns, $actualValue);
152
    }
153
154
}
155