Passed
Pull Request — 2.6 (#7317)
by Alex
08:53
created

testManyToManyDefaultOrderByAsc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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