Passed
Push — master ( 356755...14835f )
by Gerrit
04:25
created

NullableValueResolverTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 232
Duplicated Lines 11.64 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 27
loc 232
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
B shouldResolveValue() 0 43 1
A shouldThrowExceptionOnMissingColumn() 0 18 1
B shouldNotResolveValueOnNull() 0 36 1
B shouldRevertValue() 0 35 1
B dataProviderForShouldRevertValue() 0 26 1
A shouldNotRevertValueForNonNullableMapping() 18 18 1
A shouldAssertValue() 9 9 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;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\ValueResolver\NullableValueResolver;
15
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface;
16
use Addiks\RDMBundle\Mapping\MappingInterface;
17
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
18
use Addiks\RDMBundle\Mapping\NullableMappingInterface;
19
use Addiks\RDMBundle\Exception\InvalidMappingException;
20
use Doctrine\DBAL\Schema\Column;
21
22
final class NullableValueResolverTest extends TestCase
23
{
24
25
    /**
26
     * @var NullableValueResolver
27
     */
28
    private $valueResolver;
29
30
    /**
31
     * @var ValueResolverInterface
32
     */
33
    private $rootValueResolver;
34
35
    public function setUp()
36
    {
37
        $this->rootValueResolver = $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 $rootValueResolver.

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...
38
39
        $this->valueResolver = new NullableValueResolver($this->rootValueResolver);
0 ignored issues
show
Documentation introduced by
$this->rootValueResolver 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...
40
    }
41
42
    /**
43
     * @test
44
     */
45
    public function shouldResolveValue()
46
    {
47
        /** @var NullableMappingInterface $fieldMapping */
48
        $fieldMapping = $this->createMock(NullableMappingInterface::class);
49
50
        /** @var MappingInterface $fieldMapping */
51
        $innerMapping = $this->createMock(MappingInterface::class);
52
53
        /** @var Column $column */
54
        $column = $this->createMock(Column::class);
55
        $column->method("getName")->willReturn("some_column");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\DBAL\Schema\Column>.

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...
56
57
        $fieldMapping->method('getInnerMapping')->willReturn($innerMapping);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\Mapping\MappingInterface>.

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...
58
        $fieldMapping->method('collectDBALColumns')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\Mapping\MappingInterface>.

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...
59
            $column
60
        ]);
61
62
        /** @var HydrationContextInterface $context */
63
        $context = $this->createMock(HydrationContextInterface::class);
64
65
        /** @var array $dataFromAdditionalColumns */
66
        $dataFromAdditionalColumns = [
67
            'some_column' => 'foo'
68
        ];
69
70
        /** @var mixed $expectedResult */
71
        $expectedResult = 'bar';
72
73
        $this->rootValueResolver->expects($this->once())->method('resolveValue')->with(
0 ignored issues
show
Bug introduced by
The method expects() 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...
74
            $this->equalTo($innerMapping),
75
            $this->equalTo($context),
76
            $dataFromAdditionalColumns
77
        )->willReturn($expectedResult);
78
79
        /** @var mixed $actualResult */
80
        $actualResult = $this->valueResolver->resolveValue(
81
            $fieldMapping,
82
            $context,
83
            $dataFromAdditionalColumns
84
        );
85
86
        $this->assertEquals($expectedResult, $actualResult);
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function shouldThrowExceptionOnMissingColumn()
93
    {
94
        $this->expectException(InvalidMappingException::class);
95
96
        /** @var NullableMappingInterface $fieldMapping */
97
        $fieldMapping = $this->createMock(NullableMappingInterface::class);
98
99
        /** @var MappingInterface $fieldMapping */
100
        $innerMapping = $this->createMock(MappingInterface::class);
101
102
        $fieldMapping->method('getInnerMapping')->willReturn($innerMapping);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\Mapping\MappingInterface>.

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...
103
104
        $this->valueResolver->resolveValue(
105
            $fieldMapping,
106
            $this->createMock(HydrationContextInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...ontextInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\...rationContextInterface>.

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...
107
            []
108
        );
109
    }
110
111
    /**
112
     * @test
113
     */
114
    public function shouldNotResolveValueOnNull()
115
    {
116
        /** @var NullableMappingInterface $fieldMapping */
117
        $fieldMapping = $this->createMock(NullableMappingInterface::class);
118
119
        /** @var MappingInterface $fieldMapping */
120
        $innerMapping = $this->createMock(MappingInterface::class);
121
122
        /** @var Column $column */
123
        $column = $this->createMock(Column::class);
124
        $column->method("getName")->willReturn("some_column");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\DBAL\Schema\Column>.

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...
125
126
        $fieldMapping->method('getInnerMapping')->willReturn($innerMapping);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\Mapping\MappingInterface>.

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...
127
        $fieldMapping->method('collectDBALColumns')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\Mapping\MappingInterface>.

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...
128
            $column
129
        ]);
130
131
        /** @var HydrationContextInterface $context */
132
        $context = $this->createMock(HydrationContextInterface::class);
133
134
        /** @var array $dataFromAdditionalColumns */
135
        $dataFromAdditionalColumns = [
136
            'some_column' => false
137
        ];
138
139
        /** @var mixed $expectedResult */
140
        $expectedResult = 'bar';
0 ignored issues
show
Unused Code introduced by
$expectedResult is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
141
142
        $this->rootValueResolver->expects($this->never())->method('resolveValue');
0 ignored issues
show
Bug introduced by
The method expects() 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...
143
144
        $this->assertNull($this->valueResolver->resolveValue(
145
            $fieldMapping,
146
            $context,
147
            $dataFromAdditionalColumns
148
        ));
149
    }
150
151
    /**
152
     * @test
153
     * @dataProvider dataProviderForShouldRevertValue
154
     */
155
    public function shouldRevertValue(
156
        $expectedResult,
157
        array $revertedData,
158
        string $columnName
159
    ) {
160
        /** @var NullableMappingInterface $fieldMapping */
161
        $fieldMapping = $this->createMock(NullableMappingInterface::class);
162
163
        /** @var MappingInterface $fieldMapping */
164
        $innerMapping = $this->createMock(MappingInterface::class);
165
166
        $fieldMapping->method('getInnerMapping')->willReturn($innerMapping);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\Mapping\MappingInterface>.

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...
167
        $fieldMapping->method('getDeterminatorColumnName')->willReturn($columnName);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\Mapping\MappingInterface>.

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...
168
169
        /** @var HydrationContextInterface $context */
170
        $context = $this->createMock(HydrationContextInterface::class);
171
172
        /** @var mixed $valueFromEntityField */
173
        $valueFromEntityField = "foo";
174
175
        $this->rootValueResolver->expects($this->once())->method('revertValue')->with(
0 ignored issues
show
Bug introduced by
The method expects() 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...
176
            $this->equalTo($innerMapping),
177
            $this->equalTo($context),
178
            $valueFromEntityField
179
        )->willReturn($revertedData);
180
181
        /** @var mixed $actualResult */
182
        $actualResult = $this->valueResolver->revertValue(
183
            $fieldMapping,
184
            $context,
185
            $valueFromEntityField
186
        );
187
188
        $this->assertEquals($expectedResult, $actualResult);
189
    }
190
191
    public function dataProviderForShouldRevertValue()
192
    {
193
        return array(
194
            [
195
                [
196
                    'some_column' => true
197
                ],
198
                [],
199
                "some_column"
200
            ],
201
            [
202
                [
203
                    'some_column' => 123
204
                ],
205
                [
206
                    'some_column' => 123
207
                ],
208
                "some_column"
209
            ],
210
            [
211
                [],
212
                [],
213
                ""
214
            ],
215
        );
216
    }
217
218
    /**
219
     * @test
220
     */
221 View Code Duplication
    public function shouldNotRevertValueForNonNullableMapping()
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...
222
    {
223
        /** @var MappingInterface $fieldMapping */
224
        $fieldMapping = $this->createMock(MappingInterface::class);
225
226
        /** @var HydrationContextInterface $context */
227
        $context = $this->createMock(HydrationContextInterface::class);
228
229
        /** @var mixed $valueFromEntityField */
230
        $valueFromEntityField = "foo";
231
232
        /** @var mixed $actualResult */
233
        $this->assertEmpty($this->valueResolver->revertValue(
234
            $fieldMapping,
235
            $context,
236
            $valueFromEntityField
237
        ));
238
    }
239
240
    /**
241
     * @test
242
     */
243 View Code Duplication
    public function shouldAssertValue()
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...
244
    {
245
        $this->assertNull($this->valueResolver->assertValue(
246
            $this->createMock(MappingInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...appingInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\Mapping\MappingInterface>.

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...
247
            $this->createMock(HydrationContextInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...ontextInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\...rationContextInterface>.

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...
248
            [],
249
            null
250
        ));
251
    }
252
253
}
254