Completed
Push — master ( 23a060...e747f7 )
by Luís
45s queued 37s
created

XmlMappingDriverTest::testEmbeddedMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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

190
        $list = array_filter(/** @scrutinizer ignore-type */ $list, static function ($item) use ($invalid) {
Loading history...
191
            return ! in_array(pathinfo($item, PATHINFO_FILENAME), $invalid, true);
192
        });
193
194
        return array_map(static function ($item) {
195
            return [$item];
196
        }, $list);
197
    }
198
199
    /**
200
     * @group GH-7141
201
     */
202
    public function testOneToManyDefaultOrderByAsc()
203
    {
204
        $class = $this->createClassMetadata(GH7141Article::class);
205
206
        $this->assertEquals(
207
            Criteria::ASC,
208
            $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

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