Completed
Push — master ( 88a09b...db2a6e )
by Gerrit
04:31
created

shouldRevertMultidimensionalListValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
cc 1
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\Mapping\ListMapping;
15
use Doctrine\DBAL\Schema\Column;
16
use Addiks\RDMBundle\Mapping\MappingInterface;
17
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
18
use Addiks\RDMBundle\Exception\FailedRDMAssertionException;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
21
final class ListMappingTest extends TestCase
22
{
23
24
    /**
25
     * @var SubjectClass
26
     */
27
    private $mapping;
28
29
    /**
30
     * @var Column
31
     */
32
    private $column;
33
34
    /**
35
     * @var MappingInterface
36
     */
37
    private $entryMapping;
38
39
    public function setUp()
40
    {
41
        $this->column = $this->createMock(Column::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...L\Schema\Column::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\DBAL\Schema\Column> of property $column.

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

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
44
        $this->mapping = new ListMapping($this->column, $this->entryMapping, "some origin");
0 ignored issues
show
Documentation introduced by
$this->column is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Schema\Column>.

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...
Documentation introduced by
$this->entryMapping 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...
Documentation Bug introduced by
It seems like new \Addiks\RDMBundle\Ma...Mapping, 'some origin') of type object<Addiks\RDMBundle\Mapping\ListMapping> is incompatible with the declared type object<Addiks\SubjectClass> of property $mapping.

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...
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function shouldHaveDBALColumn()
51
    {
52
        $this->assertSame($this->column, $this->mapping->getDBALColumn());
53
    }
54
55
    /**
56
     * @test
57
     */
58
    public function shouldHaveEntryMapping()
59
    {
60
        $this->assertSame($this->entryMapping, $this->mapping->getEntryMapping());
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function shouldHaveOrigin()
67
    {
68
        $this->assertSame("some origin", $this->mapping->describeOrigin());
69
    }
70
71
    /**
72
     * @test
73
     */
74
    public function shouldCollectDBALColumns()
75
    {
76
        $this->assertSame([$this->column], $this->mapping->collectDBALColumns());
77
    }
78
79
    /**
80
     * @test
81
     */
82
    public function shouldResolveListValue()
83
    {
84
        /** @var HydrationContextInterface $context */
85
        $context = $this->createMock(HydrationContextInterface::class);
86
87
        $this->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...
88
89
        /** @var array $dataFromAdditionalColumns */
90
        $dataFromAdditionalColumns = [
91
            'some_column' => '{"a":"LOREM","b":"IPSUM","c":"DOLOR","d":"SIT","e":"AMET"}',
92
        ];
93
94
        $this->entryMapping->method('resolveValue')->will($this->returnCallback(
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...
95
            function ($context, $data) {
96
                return strtolower($data['']);
97
            }
98
        ));
99
100
        /** @var mixed $expectedResult */
101
        $expectedResult = [
102
            'a' => 'lorem',
103
            'b' => 'ipsum',
104
            'c' => 'dolor',
105
            'd' => 'sit',
106
            'e' => 'amet'
107
        ];
108
109
        /** @var mixed $actualResult */
110
        $actualResult = $this->mapping->resolveValue(
111
            $context,
112
            $dataFromAdditionalColumns
113
        );
114
115
        $this->assertEquals($expectedResult, $actualResult);
116
    }
117
118
    /**
119
     * @test
120
     */
121
    public function shouldRevertListValue()
122
    {
123
        /** @var HydrationContextInterface $context */
124
        $context = $this->createMock(HydrationContextInterface::class);
125
126
        $this->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...
127
128
        $this->entryMapping->method('revertValue')->will($this->returnCallback(
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...
129
            function ($context, $line) {
130
                return ['' => strtoupper($line)];
131
            }
132
        ));
133
134
        /** @var mixed $valueFromEntityField */
135
        $valueFromEntityField = [
136
            'lorem',
137
            'ipsum',
138
            'dolor',
139
            'sit',
140
            'amet',
141
        ];
142
143
        /** @var mixed $expectedResult */
144
        $expectedResult = [
145
            'some_column' => '["LOREM","IPSUM","DOLOR","SIT","AMET"]',
146
        ];
147
148
        /** @var mixed $actualResult */
149
        $actualResult = $this->mapping->revertValue(
150
            $context,
151
            $valueFromEntityField
152
        );
153
154
        $this->assertEquals($expectedResult, $actualResult);
155
    }
156
157
    /**
158
     * @test
159
     */
160
    public function shouldRevertMultidimensionalListValue()
161
    {
162
        /** @var HydrationContextInterface $context */
163
        $context = $this->createMock(HydrationContextInterface::class);
164
165
        $this->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...
166
167
        $this->entryMapping->method('revertValue')->will($this->returnCallback(
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
            function ($context, $data) {
169
                return $data;
170
            }
171
        ));
172
173
        /** @var mixed $valueFromEntityField */
174
        $valueFromEntityField = [
175
            ['a' => 123, 'b' => true],
176
            ['a' => 456, 'b' => false],
177
            []
178
        ];
179
180
        /** @var mixed $expectedResult */
181
        $expectedResult = [
182
            'some_column' => '[{"a":123,"b":true},{"a":456,"b":false}]',
183
        ];
184
185
        /** @var mixed $actualResult */
186
        $actualResult = $this->mapping->revertValue(
187
            $context,
188
            $valueFromEntityField
189
        );
190
191
        $this->assertEquals($expectedResult, $actualResult);
192
    }
193
194
    /**
195
     * @test
196
     */
197
    public function shouldAssertValue()
198
    {
199
        $this->assertNull($this->mapping->assertValue(
200
            $this->createMock(HydrationContextInterface::class),
201
            [],
202
            null
203
        ));
204
    }
205
206
    /**
207
     * @test
208
     */
209
    public function shouldThrowExceptionOnFailedAssertion()
210
    {
211
        $this->expectException(FailedRDMAssertionException::class);
212
213
        $this->mapping->assertValue(
214
            $this->createMock(HydrationContextInterface::class),
215
            [],
216
            "Lorem ipsum!"
217
        );
218
    }
219
220
    /**
221
     * @test
222
     */
223 View Code Duplication
    public function shouldWakeUpInnerMapping()
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...
224
    {
225
        /** @var ContainerInterface $container */
226
        $container = $this->createMock(ContainerInterface::class);
227
228
        $this->entryMapping->expects($this->once())->method("wakeUpMapping")->with(
0 ignored issues
show
Bug introduced by
The method expects() 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...
229
            $this->equalTo($container)
230
        );
231
232
        $this->mapping->wakeUpMapping($container);
233
    }
234
235
}
236