Passed
Pull Request — 2.6 (#7079)
by
unknown
08:23
created

DDC7079Test::createClassMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
11
use Doctrine\Tests\OrmFunctionalTestCase;
12
13
/**
14
 * Class DDC7079Test
15
 *
16
 * @group DDC-7079
17
 */
18
class DDC7079Test extends OrmFunctionalTestCase
19
{
20
    /** @var DefaultQuoteStrategy */
21
    private $strategy;
22
23
    /** @var AbstractPlatform */
24
    private $platform;
25
26
    /**
27
     * {@inheritDoc}
28
     */
29
    protected function setUp()
30
    {
31
        parent::setUp();
32
        $this->platform = $this->_em->getConnection()->getDatabasePlatform();
33
        $this->strategy = new DefaultQuoteStrategy();
34
    }
35
36
    public function testGetTableName()
37
    {
38
        $table = [
39
            'name'=>'cms_user',
40
            'schema' => 'cms',
41
        ];
42
43
        $cm = $this->createClassMetadata(DDC7079CmsUser::class);
44
        $cm->setPrimaryTable($table);
45
46
        $this->assertEquals($this->getTableFullName($table), $this->strategy->getTableName($cm, $this->platform));
47
    }
48
49
    public function testJoinTableName()
50
    {
51
        $table = [
52
            'name'=>'cmsaddress_cmsuser',
53
            'schema' => 'cms',
54
        ];
55
56
        $cm = $this->createClassMetadata(DDC7079CmsAddress::class);
57
        $cm->mapManyToMany(
58
            [
59
                'fieldName'     => 'user',
60
                'targetEntity'  => 'DDC7079CmsUser',
61
                'inversedBy'    => 'users',
62
                'joinTable'     => $table,
63
            ]
64
        );
65
66
        $this->assertEquals($this->getTableFullName($table), $this->strategy->getJoinTableName($cm->associationMappings['user'], $cm, $this->platform));
67
    }
68
69
    private function getTableFullName(array $table) : string
70
    {
71
        $join = '.';
72
        if (! $this->platform->supportsSchemas() && $this->platform->canEmulateSchemas()) {
73
            $join = '__';
74
        }
75
76
        return $table['schema'] . $join . $table['name'];
77
    }
78
79
    private function createClassMetadata(string $className) : ClassMetadata
80
    {
81
        $cm = new ClassMetadata($className);
82
        $cm->initializeReflection(new RuntimeReflectionService());
83
84
        return $cm;
85
    }
86
}
87
88
/**
89
 * @Entity
90
 * @Table(name="cms_users", schema="cms")
91
 */
92
class DDC7079CmsUser
93
{
94
    /**
95
     * @Id @Column(type="integer")
96
     * @GeneratedValue
97
     */
98
    public $id;
99
100
    /** @OneToOne(targetEntity="DDC7079CmsAddress", mappedBy="user", cascade={"persist"}, orphanRemoval=true) */
101
    public $address;
102
}
103
104
/**
105
 * @Entity
106
 * @Table(name="cms_addresses", schema="cms")
107
 */
108
class DDC7079CmsAddress
109
{
110
    /**
111
     * @Column(type="integer")
112
     * @Id @GeneratedValue
113
     */
114
    public $id;
115
116
    /**
117
     * @OneToOne(targetEntity="DDC7079CmsUser", inversedBy="address")
118
     * @JoinColumn(referencedColumnName="id")
119
     */
120
    public $user;
121
}
122