Passed
Pull Request — master (#7725)
by Guilherme
09:09
created

testEmbeddedMappingsWithUseColumnPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Mapping;
6
7
use Doctrine\Common\Collections\Criteria;
8
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService;
0 ignored issues
show
introduced by
Type Doctrine\Common\Persistence\Mapping\RuntimeReflectionService is not used in this file.
Loading history...
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use Doctrine\ORM\Mapping\ClassMetadataFactory;
11
use Doctrine\ORM\Mapping\Driver\XmlDriver;
12
use Doctrine\Tests\Models\DDC117\DDC117Translation;
13
use Doctrine\Tests\Models\DDC3293\DDC3293User;
14
use Doctrine\Tests\Models\DDC3293\DDC3293UserPrefixed;
15
use Doctrine\Tests\Models\DDC889\DDC889Class;
16
use Doctrine\Tests\Models\Generic\SerializationModel;
17
use Doctrine\Tests\Models\GH7141\GH7141Article;
18
use Doctrine\Tests\Models\GH7316\GH7316Article;
19
use Doctrine\Tests\Models\ValueObjects\Name;
20
use Doctrine\Tests\Models\ValueObjects\Person;
21
use DOMDocument;
22
use const DIRECTORY_SEPARATOR;
23
use const PATHINFO_FILENAME;
24
use function array_filter;
25
use function array_map;
26
use function glob;
27
use function in_array;
28
use function iterator_to_array;
29
use function pathinfo;
30
31
class XmlMappingDriverTest extends AbstractMappingDriverTest
32
{
33
    protected function loadDriver()
34
    {
35
        return new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
36
    }
37
38
    public function testClassTableInheritanceDiscriminatorMap() : void
39
    {
40
        $mappingDriver = $this->loadDriver();
41
42
        $class = new ClassMetadata(CTI::class, $this->metadataBuildingContext);
43
44
        $mappingDriver->loadMetadataForClass(CTI::class, $class, $this->metadataBuildingContext);
45
46
        $expectedMap = [
47
            'foo' => CTIFoo::class,
48
            'bar' => CTIBar::class,
49
            'baz' => CTIBaz::class,
50
        ];
51
52
        self::assertCount(3, $class->discriminatorMap);
53
        self::assertEquals($expectedMap, $class->discriminatorMap);
54
    }
55
56
    /**
57
     * @expectedException \Doctrine\ORM\Cache\Exception\CacheException
58
     * @expectedExceptionMessage Entity association field "Doctrine\Tests\ORM\Mapping\XMLSLC#foo" not configured as part of the second-level cache.
59
     */
60
    public function testFailingSecondLevelCacheAssociation() : void
61
    {
62
        $mappingDriver = $this->loadDriver();
63
64
        $class = new ClassMetadata(XMLSLC::class, $this->metadataBuildingContext);
65
66
        $mappingDriver->loadMetadataForClass(XMLSLC::class, $class, $this->metadataBuildingContext);
67
    }
68
69
    public function testIdentifierWithAssociationKey() : void
70
    {
71
        $driver  = $this->loadDriver();
72
        $em      = $this->getTestEntityManager();
73
        $factory = new ClassMetadataFactory();
74
75
        $em->getConfiguration()->setMetadataDriverImpl($driver);
76
        $factory->setEntityManager($em);
77
78
        $class = $factory->getMetadataFor(DDC117Translation::class);
79
80
        self::assertEquals(['language', 'article'], $class->identifier);
81
        self::assertArrayHasKey('article', iterator_to_array($class->getDeclaredPropertiesIterator()));
82
83
        $association = $class->getProperty('article');
84
85
        self::assertTrue($association->isPrimaryKey());
86
    }
87
88
    /**
89
     * @group embedded
90
     */
91
    public function testEmbeddableMapping() : void
92
    {
93
        $class = $this->createClassMetadata(Name::class);
94
95
        self::assertTrue($class->isEmbeddedClass);
96
    }
97
98
    /**
99
     * @group embedded
100
     * @group DDC-3293
101
     * @group DDC-3477
102
     * @group DDC-1238
103
     */
104
    public function testEmbeddedMappingsWithUseColumnPrefix() : void
105
    {
106
        $factory = new ClassMetadataFactory();
107
        $em      = $this->getTestEntityManager();
108
109
        $em->getConfiguration()->setMetadataDriverImpl($this->loadDriver());
110
        $factory->setEntityManager($em);
111
112
        self::assertEquals(
113
            '__prefix__',
114
            $factory->getMetadataFor(DDC3293UserPrefixed::class)
115
                ->embeddedClasses['address']['columnPrefix']
0 ignored issues
show
Bug introduced by
The property embeddedClasses does not seem to exist on Doctrine\ORM\Mapping\ClassMetadata.
Loading history...
116
        );
117
    }
118
119
    /**
120
     * @group embedded
121
     * @group DDC-3293
122
     * @group DDC-3477
123
     * @group DDC-1238
124
     */
125
    public function testEmbeddedMappingsWithFalseUseColumnPrefix() : void
126
    {
127
        $factory = new ClassMetadataFactory();
128
        $em      = $this->getTestEntityManager();
129
130
        $em->getConfiguration()->setMetadataDriverImpl($this->loadDriver());
131
        $factory->setEntityManager($em);
132
133
        self::assertFalse(
134
            $factory->getMetadataFor(DDC3293User::class)
135
                ->embeddedClasses['address']['columnPrefix']
0 ignored issues
show
Bug introduced by
The property embeddedClasses does not seem to exist on Doctrine\ORM\Mapping\ClassMetadata.
Loading history...
136
        );
137
    }
138
139
    /**
140
     * @group embedded
141
     */
142
    public function testEmbeddedMapping() : void
143
    {
144
        $class = $this->createClassMetadata(Person::class);
145
146
        self::assertEquals(
147
            [
148
                'name' => [
149
                    'class'          => Name::class,
150
                    'columnPrefix'   => 'nm_',
151
                    'declaredField'  => null,
152
                    'originalField'  => null,
153
                    'declaringClass' => $class,
154
                ],
155
            ],
156
            $class->embeddedClasses
0 ignored issues
show
Bug introduced by
The property embeddedClasses does not seem to exist on Doctrine\ORM\Mapping\ClassMetadata.
Loading history...
157
        );
158
    }
159
160
    /**
161
     * @group DDC-1468
162
     * @expectedException \Doctrine\Common\Persistence\Mapping\MappingException
163
     * @expectedExceptionMessage Invalid mapping file 'Doctrine.Tests.Models.Generic.SerializationModel.dcm.xml' for class 'Doctrine\Tests\Models\Generic\SerializationModel'.
164
     */
165
    public function testInvalidMappingFileException() : void
166
    {
167
        $this->createClassMetadata(SerializationModel::class);
168
    }
169
170
    /**
171
     * @param string $xmlMappingFile
172
     *
173
     * @dataProvider dataValidSchema
174
     * @group DDC-2429
175
     */
176
    public function testValidateXmlSchema($xmlMappingFile) : void
177
    {
178
        $xsdSchemaFile = __DIR__ . '/../../../../../doctrine-mapping.xsd';
179
        $dom           = new DOMDocument();
180
181
        $dom->load($xmlMappingFile);
182
183
        self::assertTrue($dom->schemaValidate($xsdSchemaFile));
184
    }
185
186
    public static function dataValidSchema()
187
    {
188
        $list    = glob(__DIR__ . '/xml/*.xml');
189
        $invalid = ['Doctrine.Tests.Models.DDC889.DDC889Class.dcm'];
190
191
        $list = array_filter($list, static function ($item) use ($invalid) {
0 ignored issues
show
Bug introduced by
It seems like $list can also be of type false; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

191
        $list = array_filter(/** @scrutinizer ignore-type */ $list, static function ($item) use ($invalid) {
Loading history...
192
            return ! in_array(pathinfo($item, PATHINFO_FILENAME), $invalid, true);
193
        });
194
195
        return array_map(static function ($item) {
196
            return [$item];
197
        }, $list);
198
    }
199
200
    /**
201
     * @group GH-7141
202
     */
203
    public function testOneToManyDefaultOrderByAsc()
204
    {
205
        $class = $this->createClassMetadata(GH7141Article::class);
206
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
207
208
        $this->assertEquals(
209
            Criteria::ASC,
210
            $class->getProperty('tags')->getOrderBy()['position']
0 ignored issues
show
Bug introduced by
The method getOrderBy() does not exist on Doctrine\ORM\Mapping\Property. It seems like you code against a sub-type of Doctrine\ORM\Mapping\Property such as Doctrine\ORM\Mapping\ToManyAssociationMetadata. ( Ignorable by Annotation )

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

210
            $class->getProperty('tags')->/** @scrutinizer ignore-call */ getOrderBy()['position']
Loading history...
211
        );
212
    }
213
214
    public function testManyToManyDefaultOrderByAsc() : void
215
    {
216
        $class = $this->createClassMetadata(GH7316Article::class);
217
218
        self::assertEquals(
219
            Criteria::ASC,
220
            $class->getProperty('tags')->getOrderBy()['position']
221
        );
222
    }
223
224
    /**
225
     * @group DDC-889
226
     * @expectedException \Doctrine\Common\Persistence\Mapping\MappingException
227
     * @expectedExceptionMessage Invalid mapping file 'Doctrine.Tests.Models.DDC889.DDC889Class.dcm.xml' for class 'Doctrine\Tests\Models\DDC889\DDC889Class'.
228
     */
229
    public function testinvalidEntityOrMappedSuperClassShouldMentionParentClasses() : void
230
    {
231
        $this->createClassMetadata(DDC889Class::class);
232
    }
233
}
234
235
class CTI
236
{
237
    public $id;
238
}
239
240
class CTIFoo extends CTI
241
{
242
}
243
class CTIBar extends CTI
244
{
245
}
246
class CTIBaz extends CTI
247
{
248
}
249
250
class XMLSLC
251
{
252
    public $foo;
253
}
254
class XMLSLCFoo
255
{
256
    public $id;
257
}
258