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

ArrayValueResolverTest::shouldRevertValue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
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\ArrayValueResolver;
15
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface;
16
use Addiks\RDMBundle\Mapping\ArrayMappingInterface;
17
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
18
use Addiks\RDMBundle\Mapping\MappingInterface;
19
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface;
20
21
final class ArrayValueResolverTest extends TestCase
22
{
23
24
    /**
25
     * @var ArrayValueResolver
26
     */
27
    private $arrayValueResolver;
28
29
    /**
30
     * @var ValueResolverInterface
31
     */
32
    private $entryValueResolver;
33
34
    public function setUp()
35
    {
36
        $this->entryValueResolver = $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 $entryValueResolver.

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...
37
38
        $this->arrayValueResolver = new ArrayValueResolver($this->entryValueResolver);
0 ignored issues
show
Documentation introduced by
$this->entryValueResolver 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...
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function shouldResolveValue()
45
    {
46
        /** @var ArrayMappingInterface $arrayMapping */
47
        $arrayMapping = $this->createMock(ArrayMappingInterface::class);
48
49
        /** @var EntityExample $entity */
50
        $entity = $this->createMock(EntityExample::class);
51
52
        /** @var MappingInterface $fooMapping */
53
        $fooMapping = $this->createMock(MappingInterface::class);
54
55
        /** @var MappingInterface $bazMapping */
56
        $bazMapping = $this->createMock(MappingInterface::class);
57
58
        /** @var mixed $dataFromAdditionalColumns */
59
        $dataFromAdditionalColumns = array(
60
            'lorem' => 'ipsum',
61
            'dolor' => 'sit',
62
        );
63
64
        /** @var array<mixed> $expectedResult */
65
        $expectedResult = [
66
            'foo' => 'bar',
67
            'baz' => 3.1415
68
        ];
69
70
        $arrayMapping->method('getEntryMappings')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...\ArrayMappingInterface>.

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...
71
            'foo' => $fooMapping,
72
            'baz' => $bazMapping,
73
        ]);
74
75
        $this->entryValueResolver->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...
76
            [$fooMapping, $entity, $dataFromAdditionalColumns, 'bar'],
77
            [$bazMapping, $entity, $dataFromAdditionalColumns, 3.1415],
78
        ]));
79
80
        /** @var mixed $actualResult */
81
        $actualResult = $this->arrayValueResolver->resolveValue($arrayMapping, $entity, $dataFromAdditionalColumns);
82
83
        $this->assertEquals($expectedResult, $actualResult);
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function shouldRevertValue()
90
    {
91
        /** @var ArrayMappingInterface $arrayMapping */
92
        $arrayMapping = $this->createMock(ArrayMappingInterface::class);
93
94
        /** @var EntityExample $entity */
95
        $entity = $this->createMock(EntityExample::class);
96
97
        /** @var MappingInterface $fooMapping */
98
        $fooMapping = $this->createMock(MappingInterface::class);
99
100
        /** @var MappingInterface $bazMapping */
101
        $bazMapping = $this->createMock(MappingInterface::class);
102
103
        /** @var mixed $valueFromEntityField */
104
        $valueFromEntityField = array(
105
            'foo' => 'bar',
106
            'baz' => 3.1415
107
        );
108
109
        /** @var array<string, string> $expectedResult */
110
        $expectedResult = array(
111
            'lorem' => 'ipsum',
112
            'dolor' => 'sit',
113
        );
114
115
        $arrayMapping->method('getEntryMappings')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...\ArrayMappingInterface>.

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...
116
            'foo' => $fooMapping,
117
            'baz' => $bazMapping,
118
        ]);
119
120
        $this->entryValueResolver->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...
121
            [$fooMapping, $entity, 'bar', ['lorem' => 'ipsum']],
122
            [$bazMapping, $entity, 3.1415, ['dolor' => 'sit']],
123
        ]));
124
125
        /** @var mixed $actualResult */
126
        $actualResult = $this->arrayValueResolver->revertValue($arrayMapping, $entity, $valueFromEntityField);
127
128
        $this->assertEquals($expectedResult, $actualResult);
129
    }
130
131
    /**
132
     * @test
133
     */
134
    public function shouldNotRevertForNonArray()
135
    {
136
        /** @var ArrayMappingInterface $arrayMapping */
137
        $arrayMapping = $this->createMock(ArrayMappingInterface::class);
138
139
        /** @var EntityExample $entity */
140
        $entity = $this->createMock(EntityExample::class);
141
142
        /** @var MappingInterface $fooMapping */
143
        $fooMapping = $this->createMock(MappingInterface::class);
144
145
        /** @var MappingInterface $bazMapping */
146
        $bazMapping = $this->createMock(MappingInterface::class);
147
148
        $arrayMapping->method('getEntryMappings')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...\ArrayMappingInterface>.

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...
149
            'foo' => $fooMapping,
150
            'baz' => $bazMapping,
151
        ]);
152
153
        $this->entryValueResolver->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...
154
            [$fooMapping, $entity, 'bar', ['lorem' => 'ipsum']],
155
            [$bazMapping, $entity, 3.1415, ['dolor' => 'sit']],
156
        ]));
157
158
        /** @var mixed $actualResult */
159
        $actualResult = $this->arrayValueResolver->revertValue($arrayMapping, $entity, "a non-array");
160
161
        $this->assertEquals([], $actualResult);
162
    }
163
164
    /**
165
     * @test
166
     */
167
    public function shouldAssertValue()
168
    {
169
        $this->expectException(FailedRDMAssertionExceptionInterface::class);
170
171
        /** @var ArrayMappingInterface $arrayMapping */
172
        $arrayMapping = $this->createMock(ArrayMappingInterface::class);
173
174
        /** @var EntityExample $entity */
175
        $entity = $this->createMock(EntityExample::class);
176
177
        $this->arrayValueResolver->assertValue($arrayMapping, $entity, [], "foo");
178
    }
179
180
}
181