MappingXmlDriverTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 149
c 5
b 1
f 1
dl 0
loc 233
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldThrowExceptionOnBrokenImport() 0 11 1
B shouldReadMappingData() 0 176 1
A setUp() 0 12 1
1
<?php
2
/**
3
 * Copyright (C) 2018 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\Drivers;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
15
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface;
16
use Doctrine\Persistence\Mapping\Driver\FileLocator;
17
use Addiks\RDMBundle\Mapping\Drivers\MappingXmlDriver;
18
use Addiks\RDMBundle\Mapping\EntityMapping;
19
use Addiks\RDMBundle\Mapping\ServiceMapping;
20
use Addiks\RDMBundle\Mapping\ChoiceMapping;
21
use Doctrine\DBAL\Schema\Column;
22
use Doctrine\DBAL\Types\Type;
23
use Addiks\RDMBundle\Mapping\ObjectMapping;
24
use Addiks\RDMBundle\Tests\ValueObjectExample;
25
use Addiks\RDMBundle\Mapping\CallDefinition;
26
use Addiks\RDMBundle\Mapping\FieldMapping;
27
use Addiks\RDMBundle\Mapping\ArrayMapping;
28
use Symfony\Component\HttpKernel\KernelInterface;
29
use Addiks\RDMBundle\Mapping\NullMapping;
30
use Addiks\RDMBundle\Mapping\NullableMapping;
31
use Addiks\RDMBundle\Mapping\ListMapping;
32
use Addiks\RDMBundle\Exception\InvalidMappingException;
33
use Symfony\Component\DependencyInjection\ContainerInterface;
34
35
final class MappingXmlDriverTest extends TestCase
36
{
37
38
    /**
39
     * @var MappingXmlDriver
40
     */
41
    private $mappingDriver;
42
43
    /**
44
     * @var MappingDriverInterface
45
     */
46
    private $fileLocator;
47
48
    /**
49
     * @var KernelInterface
50
     */
51
    private $kernel;
52
53
    /**
54
     * @var ContainerInterface
55
     */
56
    private $container;
57
58
    public function setUp(): void
59
    {
60
        $this->fileLocator = $this->createMock(FileLocator::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...ver\FileLocator::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\Mapping...\MappingDriverInterface of property $fileLocator.

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...
61
        $this->kernel = $this->createMock(KernelInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Symfon...KernelInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\HttpKernel\KernelInterface of property $kernel.

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...
62
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Symfon...tainerInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\Depend...tion\ContainerInterface of property $container.

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...
63
64
        $this->kernel->method("getContainer")->willReturn($this->container);
65
66
        $this->mappingDriver = new MappingXmlDriver(
67
            $this->fileLocator,
68
            $this->kernel,
69
            realpath(__DIR__ . "/../../../Resources/mapping-schema.v1.xsd")
70
        );
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function shouldReadMappingData()
77
    {
78
        /** @var string $mappingFilePath */
79
        $mappingFilePath = __DIR__ . "/EntityExample.orm.xml";
80
81
        /** @var string $mappingImportFilePath */
82
        $mappingImportFilePath = __DIR__ . "/EntityExampleImport.orm.xml";
83
84
        $expectedMapping = new EntityMapping(EntityExample::class, [
85
            'foo' => new ServiceMapping($this->container, 'some_service', false, "in file '{$mappingFilePath}' in line 14"),
86
            'bar' => new ServiceMapping($this->container, 'other_service', false, "in file '{$mappingFilePath}' in line 15"),
87
            'baz' => new ChoiceMapping('baz_column', [
88
                'lorem' => new ServiceMapping($this->container, "lorem_service", false, "in file '{$mappingFilePath}' in line 19"),
89
                'ipsum' => new ServiceMapping($this->container, "ipsum_service", true, "in file '{$mappingFilePath}' in line 22"),
90
            ], "in file '{$mappingFilePath}' in line 17"),
91
            'faz' => new ChoiceMapping(new Column("faz_column", Type::getType('string'), ['notnull' => true]), [
92
                'lorem' => new ServiceMapping($this->container, "lorem_service", false, "in file '{$mappingFilePath}' in line 29"),
93
                'ipsum' => new ServiceMapping($this->container, "ipsum_service", false, "in file '{$mappingFilePath}' in line 32"),
94
            ], "in file '{$mappingFilePath}' in line 26"),
95
            'far' => new ChoiceMapping(new Column("far_column", Type::getType('string'), ['notnull' => false]), [
96
                'lorem' => new ServiceMapping($this->container, "lorem_service", false, "in file '{$mappingFilePath}' in line 42"),
97
                'ipsum' => new ServiceMapping($this->container, "ipsum_service", false, "in file '{$mappingFilePath}' in line 45"),
98
                'dolor' => new ObjectMapping(
99
                    ValueObjectExample::class,
100
                    [],
101
                    null,
102
                    "in file '{$mappingFilePath}' in line 52",
103
                    new CallDefinition($this->container, "createFromJson", "self", [], true, "{$mappingFilePath} in line 52"),
104
                    new CallDefinition($this->container, "serializeJson", null, [], false, "{$mappingFilePath} in line 52")
105
                ),
106
            ], "in file '{$mappingFilePath}' in line 39"),
107
            'boo' => new ObjectMapping(ValueObjectExample::class, [
108
                'scalarValue' => new FieldMapping(
109
                    new Column("scalarValue", Type::getType('string'), ['notnull' => false]),
110
                    "in file '{$mappingFilePath}' in line 57"
111
                ),
112
                'lorem' => new FieldMapping(
113
                    new Column("lorem", Type::getType('string'), ['notnull' => false]),
114
                    "in file '{$mappingFilePath}' in line 58"
115
                ),
116
                'dolor' => new FieldMapping(
117
                    new Column("dolor", Type::getType('integer'), ['notnull' => false]),
118
                    "in file '{$mappingFilePath}' in line 59"
119
                ),
120
            ], null, "in file '{$mappingFilePath}' in line 56"),
121
            'abc' => new ObjectMapping(
122
                ValueObjectExample::class,
123
                [],
124
                null,
125
                "in file '{$mappingFilePath}' in line 67",
126
                new CallDefinition($this->container, "createFromJson", "self", [], true, "{$mappingFilePath} in line 67"),
127
                new CallDefinition($this->container, "serializeJson", null, [], false, "{$mappingFilePath} in line 67")
128
            ),
129
            'def' => new ObjectMapping(
130
                ValueObjectExample::class,
131
                [
132
                    'lorem' => new FieldMapping(
133
                        new Column("lorem", Type::getType('string'), ['notnull' => false]),
134
                        "in file '{$mappingFilePath}' in line 76"
135
                    ),
136
                    'dolor' => new FieldMapping(
137
                        new Column("dolor", Type::getType('integer'), ['notnull' => false]),
138
                        "in file '{$mappingFilePath}' in line 77"
139
                    ),
140
                ],
141
                null,
142
                "in file '{$mappingFilePath}' in line 72",
143
                new CallDefinition($this->container, "createValueObject", "@value_object.factory", [
144
                    new FieldMapping(
145
                        new Column("def", Type::getType('integer'), ['notnull' => false]),
146
                        "in file '{$mappingFilePath}' in line 74"
147
                    )
148
                ], false, "{$mappingFilePath} in line 72")
149
            ),
150
            'ghi' => new ObjectMapping(
151
                ValueObjectExample::class,
152
                [],
153
                null,
154
                "in file '{$mappingFilePath}' in line 83",
155
                new CallDefinition($this->container, "createValueObject", "@value_object.factory", [
156
                    new ChoiceMapping('baz_column', [
157
                        'lorem' => new ServiceMapping(
158
                            $this->container,
159
                            "lorem_service",
160
                            false,
161
                            "in file '{$mappingFilePath}' in line 87"
162
                        ),
163
                        'ipsum' => new ServiceMapping(
164
                            $this->container,
165
                            "ipsum_service",
166
                            true,
167
                            "in file '{$mappingFilePath}' in line 90"
168
                        ),
169
                    ], "in file '{$mappingFilePath}' in line 85")
170
                ], false, "{$mappingFilePath} in line 83")
171
            ),
172
            'jkl' => new ArrayMapping(
173
                [
174
                    new ObjectMapping(ValueObjectExample::class, [], null, "in file '{$mappingFilePath}' in line 97"),
175
                    new ObjectMapping(ValueObjectExample::class, [
176
                        'qwe' => new ArrayMapping([], "in file '{$mappingFilePath}' in line 99")
177
                    ], null, "in file '{$mappingFilePath}' in line 98"),
178
                    new ObjectMapping(ValueObjectExample::class, [], null, "in file '{$mappingFilePath}' in line 101"),
179
                ],
180
                "in file '{$mappingFilePath}' in line 96"
181
            ),
182
            'mno' => new ArrayMapping(
183
                [
184
                    'foo' => new ServiceMapping(
185
                        $this->container,
186
                        'some_service',
187
                        false,
188
                        "in file '{$mappingFilePath}' in line 106"
189
                    ),
190
                    'bar' => new ServiceMapping(
191
                        $this->container,
192
                        'other_service',
193
                        false,
194
                        "in file '{$mappingFilePath}' in line 109"
195
                    ),
196
                    'baz' => new NullMapping("in file '{$mappingFilePath}' in line 113"),
197
                    'maz' => new ListMapping(
198
                        new Column("maz_column", Type::getType('string'), ['notnull' => true]),
199
                        new ObjectMapping(
200
                            ValueObjectExample::class,
201
                            [],
202
                            new Column("maz_obj_column", Type::getType('string'), [
203
                                'length' => 255,
204
                                'default' => '#DEFAULT#'
205
                            ]),
206
                            "in file '{$mappingFilePath}' in line 121"
207
                        ),
208
                        "in file '{$mappingFilePath}' in line 116"
209
                    ),
210
                ],
211
                "in file '{$mappingFilePath}' in line 104"
212
            ),
213
            'pqr' => new NullableMapping(
214
                new ServiceMapping($this->container, 'some_service', false, "in file '{$mappingFilePath}' in line 127"),
215
                new Column("pqr_column", Type::getType('boolean'), ['notnull' => false]),
216
                "in file '{$mappingFilePath}' at line 126"
217
            ),
218
            'stu' => new ListMapping(
219
                new Column("stu_column", Type::getType('string'), ['notnull' => true]),
220
                new ObjectMapping(ValueObjectExample::class, [], null, "in file '{$mappingFilePath}' in line 131"),
221
                "in file '{$mappingFilePath}' in line 130"
222
            ),
223
            'vwx' => new ObjectMapping(ValueObjectExample::class, [
224
                'foo' => new ServiceMapping(
225
                    $this->container,
226
                    'some_service',
227
                    false,
228
                    "in file '{$mappingImportFilePath}' in line 9"
229
                ),
230
                'bar' => new ServiceMapping(
231
                    $this->container,
232
                    'other_service',
233
                    false,
234
                    "in file '{$mappingImportFilePath}' in line 10"
235
                ),
236
            ], new Column(
237
                "vwx_column",
238
                Type::getType('integer'),
239
                ['notnull' => true, 'length' => 255]
240
            ), "in file '{$mappingImportFilePath}' in line 8")
241
        ]);
242
243
        $this->fileLocator->method('fileExists')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Addiks\RDMBundle\Mapping...\MappingDriverInterface. ( Ignorable by Annotation )

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

243
        $this->fileLocator->/** @scrutinizer ignore-call */ 
244
                            method('fileExists')->willReturn(true);

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...
244
        $this->fileLocator->method('findMappingFile')->willReturn("@foobarbaz");
245
246
        $this->kernel->method("locateResource")->willReturn($mappingFilePath);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Symfony\Component\HttpKernel\KernelInterface. ( Ignorable by Annotation )

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

246
        $this->kernel->/** @scrutinizer ignore-call */ 
247
                       method("locateResource")->willReturn($mappingFilePath);

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...
247
248
        /** @var EntityMapping $actualMapping */
249
        $actualMapping = $this->mappingDriver->loadRDMMetadataForClass(EntityExample::class);
250
251
        $this->assertEquals($expectedMapping, $actualMapping);
252
    }
253
254
    /**
255
     * @test
256
     */
257
    public function shouldThrowExceptionOnBrokenImport()
258
    {
259
        $this->expectException(InvalidMappingException::class);
260
261
        /** @var string $mappingFilePath */
262
        $mappingFilePath = __DIR__ . "/EntityExampleBroken.orm.xml";
263
264
        $this->fileLocator->method('fileExists')->willReturn(true);
265
        $this->fileLocator->method('findMappingFile')->willReturn($mappingFilePath);
266
267
        $this->mappingDriver->loadRDMMetadataForClass(EntityExample::class);
268
    }
269
270
}
271