Issues (259)

Tests/Mapping/ArrayMappingTest.php (5 issues)

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\Mapping;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\Mapping\ArrayMapping;
15
use Addiks\RDMBundle\Mapping\MappingInterface;
16
use Doctrine\DBAL\Schema\Column;
17
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
18
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
19
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
22
final class ArrayMappingTest extends TestCase
23
{
24
25
    /**
26
     * @var ArrayMapping
27
     */
28
    private $arrayMapping;
29
30
    /**
31
     * @var MappingInterface
32
     */
33
    private $mappingA;
34
35
    /**
36
     * @var MappingInterface
37
     */
38
    private $mappingB;
39
40
    public function setUp(): void
41
    {
42
        $this->mappingA = $this->createMock(MappingInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Addiks...appingInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\Mapping\MappingInterface of property $mappingA.

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...
43
        $this->mappingB = $this->createMock(MappingInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Addiks...appingInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\Mapping\MappingInterface of property $mappingB.

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...
44
45
        $this->arrayMapping = new ArrayMapping([
46
            'foo' => $this->mappingA,
47
            'bar' => $this->mappingB,
48
        ], "Some Origin");
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function shouldHaveOrigin()
55
    {
56
        $this->assertEquals("Some Origin", $this->arrayMapping->describeOrigin());
57
    }
58
59
    /**
60
     * @test
61
     */
62
    public function shouldStoreEntryMappings()
63
    {
64
        $this->assertEquals([
65
            'foo' => $this->mappingA,
66
            'bar' => $this->mappingB,
67
        ], $this->arrayMapping->getEntryMappings());
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function shouldCollectDBALColumns()
74
    {
75
        /** @var Column $columnA */
76
        $columnA = $this->createMock(Column::class);
77
78
        /** @var Column $columnB */
79
        $columnB = $this->createMock(Column::class);
80
81
        $this->mappingA->method('collectDBALColumns')->willReturn([
0 ignored issues
show
The method method() does not exist on Addiks\RDMBundle\Mapping\MappingInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        $this->mappingA->/** @scrutinizer ignore-call */ 
82
                         method('collectDBALColumns')->willReturn([

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
            'lorem' => $columnA,
83
        ]);
84
85
        $this->mappingB->method('collectDBALColumns')->willReturn([
86
            'ipsum' => $columnB,
87
        ]);
88
89
        $this->assertEquals([
90
            'lorem' => $columnA,
91
            'ipsum' => $columnB
92
        ], $this->arrayMapping->collectDBALColumns());
93
    }
94
95
    /**
96
     * @test
97
     */
98
    public function shouldResolveValueToArray()
99
    {
100
        /** @var HydrationContextInterface $context */
101
        $context = $this->createMock(HydrationContextInterface::class);
102
        $context->method('getEntityClass')->willReturn(EntityExample::class);
0 ignored issues
show
The method method() does not exist on Addiks\RDMBundle\Hydrati...drationContextInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
        $context->/** @scrutinizer ignore-call */ 
103
                  method('getEntityClass')->willReturn(EntityExample::class);

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
        /** @var mixed $dataFromAdditionalColumns */
105
        $dataFromAdditionalColumns = array(
106
            'lorem' => 'ipsum',
107
            'dolor' => 'sit',
108
        );
109
110
        /** @var array<mixed> $expectedResult */
111
        $expectedResult = [
112
            'foo' => 'bar',
113
            'bar' => 3.1415
114
        ];
115
116
        $this->mappingA->expects($this->once())->method('resolveValue')->with(
0 ignored issues
show
The method expects() does not exist on Addiks\RDMBundle\Mapping\MappingInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        $this->mappingA->/** @scrutinizer ignore-call */ 
117
                         expects($this->once())->method('resolveValue')->with(

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...
117
            $this->equalTo($context),
118
            $this->equalTo($dataFromAdditionalColumns)
119
        )->willReturn('bar');
120
121
        $this->mappingB->expects($this->once())->method('resolveValue')->with(
122
            $this->equalTo($context),
123
            $this->equalTo($dataFromAdditionalColumns)
124
        )->willReturn(3.1415);
125
126
        /** @var mixed $actualResult */
127
        $actualResult = $this->arrayMapping->resolveValue($context, $dataFromAdditionalColumns);
128
129
        $this->assertEquals($expectedResult, $actualResult);
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function shouldRevertValueFromArray()
136
    {
137
        /** @var HydrationContextInterface $context */
138
        $context = $this->createMock(HydrationContextInterface::class);
139
        $context->method('getEntityClass')->willReturn(EntityExample::class);
140
141
        /** @var mixed $valueFromEntityField */
142
        $valueFromEntityField = array(
143
            'foo' => 'bar',
144
            'bar' => 3.1415
145
        );
146
147
        /** @var array<string, string> $expectedResult */
148
        $expectedResult = array(
149
            'lorem' => 'ipsum',
150
            'dolor' => 'sit',
151
        );
152
153
        $this->mappingA->expects($this->once())->method('revertValue')->with(
154
            $this->equalTo($context),
155
            $this->equalTo('bar')
156
        )->willReturn(['lorem' => 'ipsum']);
157
158
        $this->mappingB->expects($this->once())->method('revertValue')->with(
159
            $this->equalTo($context),
160
            $this->equalTo(3.1415)
161
        )->willReturn(['dolor' => 'sit']);
162
163
        /** @var mixed $actualResult */
164
        $actualResult = $this->arrayMapping->revertValue($context, $valueFromEntityField);
165
166
        $this->assertEquals($expectedResult, $actualResult);
167
    }
168
169
    /**
170
     * @test
171
     */
172
    public function shouldNotRevertForNonArray()
173
    {
174
        /** @var HydrationContextInterface $context */
175
        $context = $this->createMock(HydrationContextInterface::class);
176
        $context->method('getEntityClass')->willReturn(EntityExample::class);
177
178
        $this->mappingA->expects($this->never())->method('revertValue');
179
        $this->mappingB->expects($this->never())->method('revertValue');
180
181
        /** @var mixed $actualResult */
182
        $actualResult = $this->arrayMapping->revertValue($context, "a non-array");
183
184
        $this->assertEquals([], $actualResult);
185
    }
186
187
    /**
188
     * @test
189
     */
190
    public function shouldAssertValue()
191
    {
192
        $this->expectException(FailedRDMAssertionExceptionInterface::class);
193
194
        /** @var HydrationContextInterface $context */
195
        $context = $this->createMock(HydrationContextInterface::class);
196
        $context->method('getEntityClass')->willReturn(EntityExample::class);
197
198
        $this->arrayMapping->assertValue($context, [], "foo");
199
    }
200
201
    /**
202
     * @test
203
     */
204
    public function shouldWakeUpInnerMapping()
205
    {
206
        /** @var ContainerInterface $container */
207
        $container = $this->createMock(ContainerInterface::class);
208
209
        $this->mappingA->expects($this->once())->method("wakeUpMapping")->with(
210
            $this->equalTo($container)
211
        );
212
213
        $this->mappingB->expects($this->once())->method("wakeUpMapping")->with(
214
            $this->equalTo($container)
215
        );
216
217
        $this->arrayMapping->wakeUpMapping($container);
218
    }
219
220
}
221