Passed
Pull Request — 2.7 (#7281)
by Michael
10:04
created

XmlMappingDriverTest::testEmbeddedMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Mapping;
4
5
use Doctrine\Common\Collections\Criteria;
6
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Doctrine\ORM\Mapping\ClassMetadataFactory;
9
use Doctrine\ORM\Mapping\Driver\XmlDriver;
10
use Doctrine\Tests\Models\DDC117\DDC117Translation;
11
use Doctrine\Tests\Models\DDC3293\DDC3293User;
12
use Doctrine\Tests\Models\DDC3293\DDC3293UserPrefixed;
13
use Doctrine\Tests\Models\DDC889\DDC889Class;
14
use Doctrine\Tests\Models\Generic\SerializationModel;
15
use Doctrine\Tests\Models\GH7141\GH7141Article;
16
use Doctrine\Tests\Models\ValueObjects\Name;
17
use Doctrine\Tests\Models\ValueObjects\Person;
18
19
class XmlMappingDriverTest extends AbstractMappingDriverTest
20
{
21
    protected function _loadDriver()
22
    {
23
        return new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
24
    }
25
26
    public function testClassTableInheritanceDiscriminatorMap()
27
    {
28
        $mappingDriver = $this->_loadDriver();
29
30
        $class = new ClassMetadata(CTI::class);
31
        $class->initializeReflection(new RuntimeReflectionService());
32
        $mappingDriver->loadMetadataForClass(CTI::class, $class);
33
34
        $expectedMap = [
35
            'foo' => CTIFoo::class,
36
            'bar' => CTIBar::class,
37
            'baz' => CTIBaz::class,
38
        ];
39
40
        $this->assertEquals(3, count($class->discriminatorMap));
41
        $this->assertEquals($expectedMap, $class->discriminatorMap);
42
    }
43
44
    /**
45
     * @expectedException \Doctrine\ORM\Cache\CacheException
46
     * @expectedExceptionMessage Entity association field "Doctrine\Tests\ORM\Mapping\XMLSLC#foo" not configured as part of the second-level cache.
47
     */
48
    public function testFailingSecondLevelCacheAssociation()
49
    {
50
        $mappingDriver = $this->_loadDriver();
51
52
        $class = new ClassMetadata(XMLSLC::class);
53
        $mappingDriver->loadMetadataForClass(XMLSLC::class, $class);
54
    }
55
56
    public function testIdentifierWithAssociationKey()
57
    {
58
        $driver  = $this->_loadDriver();
59
        $em      = $this->_getTestEntityManager();
60
        $factory = new ClassMetadataFactory();
61
62
        $em->getConfiguration()->setMetadataDriverImpl($driver);
63
        $factory->setEntityManager($em);
64
65
        $class = $factory->getMetadataFor(DDC117Translation::class);
66
67
        $this->assertEquals(['language', 'article'], $class->identifier);
0 ignored issues
show
Bug introduced by
Accessing identifier on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
68
        $this->assertArrayHasKey('article', $class->associationMappings);
0 ignored issues
show
Bug introduced by
Accessing associationMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
69
70
        $this->assertArrayHasKey('id', $class->associationMappings['article']);
71
        $this->assertTrue($class->associationMappings['article']['id']);
72
    }
73
74
    public function testEmbeddableMapping()
75
    {
76
        $class = $this->createClassMetadata(Name::class);
77
78
        $this->assertEquals(true, $class->isEmbeddedClass);
79
    }
80
81
    /**
82
     * @group DDC-3293
83
     * @group DDC-3477
84
     * @group 1238
85
     */
86
    public function testEmbeddedMappingsWithUseColumnPrefix()
87
    {
88
        $factory = new ClassMetadataFactory();
89
        $em      = $this->_getTestEntityManager();
90
91
        $em->getConfiguration()->setMetadataDriverImpl($this->_loadDriver());
92
        $factory->setEntityManager($em);
93
94
        $this->assertEquals(
95
            '__prefix__',
96
            $factory->getMetadataFor(DDC3293UserPrefixed::class)
97
                ->embeddedClasses['address']['columnPrefix']
0 ignored issues
show
Bug introduced by
Accessing embeddedClasses on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
98
        );
99
    }
100
101
    /**
102
     * @group DDC-3293
103
     * @group DDC-3477
104
     * @group 1238
105
     */
106
    public function testEmbeddedMappingsWithFalseUseColumnPrefix()
107
    {
108
        $factory = new ClassMetadataFactory();
109
        $em      = $this->_getTestEntityManager();
110
111
        $em->getConfiguration()->setMetadataDriverImpl($this->_loadDriver());
112
        $factory->setEntityManager($em);
113
114
        $this->assertFalse(
115
            $factory->getMetadataFor(DDC3293User::class)
116
                ->embeddedClasses['address']['columnPrefix']
0 ignored issues
show
Bug introduced by
Accessing embeddedClasses on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
117
        );
118
    }
119
120
    public function testEmbeddedMapping()
121
    {
122
        $class = $this->createClassMetadata(Person::class);
123
124
        $this->assertEquals(
125
            [
126
                'name' => [
127
                    'class' => Name::class,
128
                    'columnPrefix' => 'nm_',
129
                    'declaredField' => null,
130
                    'originalField' => null,
131
                ]
132
            ],
133
            $class->embeddedClasses
134
        );
135
    }
136
137
    /**
138
     * @group DDC-1468
139
     *
140
     * @expectedException \Doctrine\Common\Persistence\Mapping\MappingException
141
     * @expectedExceptionMessage Invalid mapping file 'Doctrine.Tests.Models.Generic.SerializationModel.dcm.xml' for class 'Doctrine\Tests\Models\Generic\SerializationModel'.
142
     */
143
    public function testInvalidMappingFileException()
144
    {
145
        $this->createClassMetadata(SerializationModel::class);
146
    }
147
148
    /**
149
     * @param string $xmlMappingFile
150
     * @dataProvider dataValidSchema
151
     * @group DDC-2429
152
     */
153
    public function testValidateXmlSchema($xmlMappingFile)
154
    {
155
        $xsdSchemaFile  = __DIR__ . '/../../../../../doctrine-mapping.xsd';
156
        $dom            = new \DOMDocument('UTF-8');
157
158
        $dom->load($xmlMappingFile);
159
160
        $this->assertTrue($dom->schemaValidate($xsdSchemaFile));
161
    }
162
163
    static public function dataValidSchema()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
164
    {
165
        $list    = glob(__DIR__ . '/xml/*.xml');
166
        $invalid = [
167
            'Doctrine.Tests.Models.DDC889.DDC889Class.dcm'
168
        ];
169
170
        $list = array_filter($list, function($item) use ($invalid){
171
            return ! in_array(pathinfo($item, PATHINFO_FILENAME), $invalid);
172
        });
173
174
        return array_map(function($item){
175
            return [$item];
176
        }, $list);
177
    }
178
179
    /**
180
     * @group GH-7141
181
     */
182
    public function testOneToManyDefaultOrderByAsc()
183
    {
184
        $driver = $this->_loadDriver();
185
        $class  = new ClassMetadata(GH7141Article::class);
186
187
        $class->initializeReflection(new RuntimeReflectionService());
188
        $driver->loadMetadataForClass(GH7141Article::class, $class);
189
190
191
        $this->assertEquals(
192
            Criteria::ASC,
193
            $class->getMetadataValue('associationMappings')['tags']['orderBy']['position']
194
        );
195
    }
196
197
    /**
198
     * @group DDC-889
199
     * @expectedException \Doctrine\Common\Persistence\Mapping\MappingException
200
     * @expectedExceptionMessage Invalid mapping file 'Doctrine.Tests.Models.DDC889.DDC889Class.dcm.xml' for class 'Doctrine\Tests\Models\DDC889\DDC889Class'.
201
     */
202
    public function testinvalidEntityOrMappedSuperClassShouldMentionParentClasses()
203
    {
204
        $this->createClassMetadata(DDC889Class::class);
205
    }
206
}
207
208
class CTI
209
{
210
    public $id;
211
}
212
213
class CTIFoo extends CTI {}
214
class CTIBar extends CTI {}
215
class CTIBaz extends CTI {}
216
217
class XMLSLC
218
{
219
    public $foo;
220
}
221
class XMLSLCFoo
222
{
223
    public $id;
224
}
225