Passed
Push — master ( 064bed...d815ea )
by Gerrit
04:18
created

ChoiceMappingTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 92
c 0
b 0
f 0
dl 0
loc 238
rs 10
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldWakeUpInnerMapping() 0 14 1
A shouldDescribeOrigin() 0 3 1
A shouldHaveChoices() 0 6 1
A setUp() 0 20 2
A shouldHaveDeterminatorColumn() 0 8 1
A assertsChosenValue() 0 30 1
A shouldHaveADeterminatorColumnName() 0 3 1
A shouldCollectColumns() 0 24 1
A choosesTheCorrectValue() 0 27 1
A canRevertAChoice() 0 38 1
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\ChoiceMapping;
15
use Doctrine\DBAL\Schema\Column;
16
use Doctrine\DBAL\Types\Type;
17
use Addiks\RDMBundle\Mapping\MappingInterface;
18
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
19
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
22
final class ChoiceMappingTest extends TestCase
23
{
24
25
    /**
26
     * @var ChoiceMapping
27
     */
28
    private $choiceMapping;
29
30
    /**
31
     * @var MappingInterface
32
     */
33
    private $optionMappingA;
34
35
    /**
36
     * @var MappingInterface
37
     */
38
    private $optionMappingB;
39
40
    public function setUp(
41
        string $column = null
42
    ): void {
43
        if (is_null($column)) {
44
            $column = new Column("some_column_name", Type::getType('string'), [
45
                'notnull' => false,
46
                'length' => 255
47
            ]);
48
        }
49
50
        $this->optionMappingA = $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 $optionMappingA.

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->optionMappingB = $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 $optionMappingB.

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->choiceMapping = new ChoiceMapping(
54
            $column,
55
            [
56
                'foo' => $this->optionMappingA,
57
                'bar' => $this->optionMappingB,
58
            ],
59
            "in foo_file at bar_line!"
60
        );
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function shouldHaveChoices()
67
    {
68
        $this->assertEquals([
69
            'foo' => $this->optionMappingA,
70
            'bar' => $this->optionMappingB
71
        ], $this->choiceMapping->getChoices());
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function shouldDescribeOrigin()
78
    {
79
        $this->assertEquals("in foo_file at bar_line!", $this->choiceMapping->describeOrigin());
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function shouldCollectColumns()
86
    {
87
        $choiceColumn = new Column("some_column_name", Type::getType('string'), [
88
            'notnull' => false,
89
            'length' => 255
90
        ]);
91
92
        $columnA = new Column("foo_column", Type::getType('integer'), []);
93
        $columnB = new Column("bar_column", Type::getType('date'), []);
94
95
        /** @var array<Column> $expectedColumns */
96
        $expectedColumns = array(
97
            $choiceColumn,
98
            $columnA,
99
            $columnB,
100
        );
101
102
        $this->optionMappingA->method('collectDBALColumns')->willReturn([$columnA]);
0 ignored issues
show
Bug introduced by
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

102
        $this->optionMappingA->/** @scrutinizer ignore-call */ 
103
                               method('collectDBALColumns')->willReturn([$columnA]);

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
        $this->optionMappingB->method('collectDBALColumns')->willReturn([$columnB]);
104
105
        /** @var array<Column> $actualColumns */
106
        $actualColumns = $this->choiceMapping->collectDBALColumns();
107
108
        $this->assertEquals($expectedColumns, $actualColumns);
109
    }
110
111
    /**
112
     * @test
113
     */
114
    public function shouldHaveDeterminatorColumn()
115
    {
116
        $expectedColumn = new Column("some_column_name", Type::getType('string'), [
117
            'notnull' => false,
118
            'length' => 255
119
        ]);
120
121
        $this->assertEquals($expectedColumn, $this->choiceMapping->getDeterminatorColumn());
122
    }
123
124
    /**
125
     * @test
126
     */
127
    public function shouldHaveADeterminatorColumnName()
128
    {
129
        $this->assertEquals("some_column_name", $this->choiceMapping->getDeterminatorColumnName());
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function choosesTheCorrectValue()
136
    {
137
        $this->setUp("some_column");
138
139
        /** @var HydrationContextInterface $context */
140
        $context = $this->createMock(HydrationContextInterface::class);
141
        $context->method('getEntityClass')->willReturn(EntityExample::class);
0 ignored issues
show
Bug introduced by
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

141
        $context->/** @scrutinizer ignore-call */ 
142
                  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...
142
143
        /** @var array<scalar> $dataFromAdditionalColumns */
144
        $dataFromAdditionalColumns = array(
145
            'lorem' => 'ipsum',
146
            'some_column' => 'foo',
147
        );
148
149
        /** @var string $expectedValue */
150
        $expectedValue = "lorem ipsum dolor sit amet";
151
152
        $this->optionMappingA->method('resolveValue')->willReturn($expectedValue);
153
        $this->optionMappingB->method('resolveValue')->willReturn("unexpected value");
154
155
        /** @var mixed $actualValue */
156
        $actualValue = $this->choiceMapping->resolveValue(
157
            $context,
158
            $dataFromAdditionalColumns
159
        );
160
161
        $this->assertEquals($expectedValue, $actualValue);
162
    }
163
164
    /**
165
     * @test
166
     */
167
    public function canRevertAChoice()
168
    {
169
        /** @var HydrationContextInterface $context */
170
        $context = $this->createMock(HydrationContextInterface::class);
171
        $context->method('getEntityClass')->willReturn(EntityExample::class);
172
173
        /** @var MappingInterface $optionMappingC */
174
        $optionMappingC = $this->createMock(MappingInterface::class);
175
176
        /** @var scalar $valueFromEntityField */
177
        $valueFromEntityField = 'lorem ipsum';
178
179
        /** @var array<scalar> $expectedValue */
180
        $expectedValue = [
181
            "some_column" => 'bar'
182
        ];
183
184
        $this->optionMappingA->method('resolveValue')->willReturn("unexpected value");
185
        $this->optionMappingB->method('resolveValue')->willReturn("lorem ipsum");
186
        $optionMappingC->method('resolveValue')->willReturn("lorem ipsum");
187
188
        $choiceMapping = new ChoiceMapping(
189
            "some_column",
190
            [
191
                'foo' => $this->optionMappingA,
192
                'bar' => $this->optionMappingB,
193
                'baz' => $optionMappingC,
194
            ],
195
            "in foo_file at bar_line!"
196
        );
197
198
        /** @var mixed $actualValue */
199
        $actualValue = $choiceMapping->revertValue(
200
            $context,
201
            $valueFromEntityField
202
        );
203
204
        $this->assertEquals($expectedValue, $actualValue);
205
    }
206
207
    /**
208
     * @test
209
     */
210
    public function assertsChosenValue()
211
    {
212
        $this->setUp("some_column");
213
214
        /** @var HydrationContextInterface $context */
215
        $context = $this->createMock(HydrationContextInterface::class);
216
        $context->method('getEntityClass')->willReturn(EntityExample::class);
217
218
        /** @var scalar $valueFromEntityField */
219
        $valueFromEntityField = 'lorem ipsum';
0 ignored issues
show
Unused Code introduced by
The assignment to $valueFromEntityField is dead and can be removed.
Loading history...
220
221
        /** @var array<scalar> $dataFromAdditionalColumns */
222
        $dataFromAdditionalColumns = array(
223
            'lorem' => 'ipsum',
224
            'some_column' => 'foo',
225
        );
226
227
        /** @var scalar $actualValue */
228
        $actualValue = "some actual value";
229
230
        $this->optionMappingA->expects($this->once())->method('assertValue')->with(
231
            $this->equalTo($context),
232
            $this->equalTo($dataFromAdditionalColumns),
233
            $this->equalTo($actualValue)
234
        );
235
236
        $this->choiceMapping->assertValue(
237
            $context,
238
            $dataFromAdditionalColumns,
239
            $actualValue
240
        );
241
    }
242
243
    /**
244
     * @test
245
     */
246
    public function shouldWakeUpInnerMapping()
247
    {
248
        /** @var ContainerInterface $container */
249
        $container = $this->createMock(ContainerInterface::class);
250
251
        $this->optionMappingA->expects($this->once())->method("wakeUpMapping")->with(
0 ignored issues
show
Bug introduced by
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

251
        $this->optionMappingA->/** @scrutinizer ignore-call */ 
252
                               expects($this->once())->method("wakeUpMapping")->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...
252
            $this->equalTo($container)
253
        );
254
255
        $this->optionMappingB->expects($this->once())->method("wakeUpMapping")->with(
256
            $this->equalTo($container)
257
        );
258
259
        $this->choiceMapping->wakeUpMapping($container);
260
    }
261
262
}
263