Passed
Pull Request — master (#6834)
by Massimiliano
15:53
created

DDC2825Test::getTestedClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\ORM\Tools\ToolsException;
6
use Doctrine\Tests\Models\DDC2825\ExplicitSchemaAndTable;
7
use Doctrine\Tests\Models\DDC2825\SchemaAndTableInTableName;
8
use Doctrine\Tests\TestUtil;
9
10
/**
11
 * This class makes tests on the correct use of a database schema when entities are stored
12
 *
13
 * @group DDC-2825
14
 */
15
class DDC2825Test extends \Doctrine\Tests\OrmFunctionalTestCase
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20
    protected function setUp()
21
    {
22
        parent::setUp();
23
24
        $platform = $this->_em->getConnection()->getDatabasePlatform();
25
26
        if ( ! $platform->supportsSchemas() && ! $platform->canEmulateSchemas()) {
27
            $this->markTestSkipped("This test is only useful for databases that support schemas or can emulate them.");
28
        }
29
    }
30
31
    /**
32
     * @dataProvider getTestedClasses
33
     *
34
     * @param string $className
35
     * @param string $expectedSchemaName
36
     * @param string $expectedTableName
37
     */
38
    public function testClassSchemaMappingsValidity($className, $expectedSchemaName, $expectedTableName)
39
    {
40
        $classMetadata   = $this->_em->getClassMetadata($className);
41
        $platform        = $this->_em->getConnection()->getDatabasePlatform();
42
        $quotedTableName = $this->_em->getConfiguration()->getQuoteStrategy()->getTableName($classMetadata, $platform);
43
44
        // Check if table name and schema properties are defined in the class metadata
45
        $this->assertEquals($expectedTableName, $classMetadata->table['name']);
46
        $this->assertEquals($expectedSchemaName, $classMetadata->table['schema']);
47
48 View Code Duplication
        if ($this->_em->getConnection()->getDatabasePlatform()->supportsSchemas()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            $fullTableName = sprintf('%s.%s', $expectedSchemaName, $expectedTableName);
50
        } else {
51
            $fullTableName = sprintf('%s__%s', $expectedSchemaName, $expectedTableName);
52
        }
53
54
        $this->assertEquals($fullTableName, $quotedTableName);
55
56
        // Checks sequence name validity
57
        $this->assertEquals(
58
            $fullTableName . '_' . $classMetadata->getSingleIdentifierColumnName() . '_seq',
59
            $classMetadata->getSequenceName($platform)
60
        );
61
    }
62
63
    /**
64
     * @dataProvider getTestedClassesWithImplicitlyDefinedSchemaAndTableName
65
     *
66
     * @param string $className
67
     * @param string $expectedSchemaName
68
     * @param string $expectedTableName
69
     * @param string $expectedQuotedTableName
70
     */
71
    public function testClassSchemaMappingsValidityWithImplicitlyDefinedSchemaAndQuotedTableName(
72
        $className,
73
        $expectedSchemaName,
74
        $expectedTableName,
75
        $expectedQuotedTableName
76
    ) {
77
        $classMetadata   = $this->_em->getClassMetadata($className);
78
        $platform        = $this->_em->getConnection()->getDatabasePlatform();
79
        $quotedTableName = $this->_em->getConfiguration()->getQuoteStrategy()->getTableName($classMetadata, $platform);
80
81
        // Check if table name and schema properties are defined in the class metadata
82
        $this->assertEquals($expectedTableName, $classMetadata->table['name']);
83
        $this->assertEquals($expectedSchemaName, $classMetadata->table['schema']);
84
85 View Code Duplication
        if ($this->_em->getConnection()->getDatabasePlatform()->supportsSchemas()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
            $fullTableName = sprintf('%s.%s', $expectedSchemaName, $expectedQuotedTableName);
87
        } else {
88
            $fullTableName = sprintf('%s__%s', $expectedSchemaName, $expectedTableName);
89
        }
90
91
        $this->assertEquals($fullTableName, $quotedTableName);
92
    }
93
    
94
    /**
95
     * @dataProvider getTestedClasses
96
     *
97
     * @param string $className
98
     */
99
    public function testPersistenceOfEntityWithSchemaMapping($className)
100
    {
101
        try {
102
            $this->_schemaTool->createSchema([$this->_em->getClassMetadata($className)]);
103
        } catch (ToolsException $e) {
104
            // table already exists
105
        }
106
107
        $this->_em->persist(new $className());
108
        $this->_em->flush();
109
        $this->_em->clear();
110
111
        $this->assertCount(1, $this->_em->getRepository($className)->findAll());
112
    }
113
114
    /**
115
     * Data provider
116
     *
117
     * @return string[][]
118
     */
119
    public function getTestedClasses()
120
    {
121
        return [
122
            [ExplicitSchemaAndTable::class, 'explicit_schema', 'explicit_table'],
123
            [SchemaAndTableInTableName::class, 'implicit_schema', 'implicit_table'],
124
        ];
125
    }
126
127
    public function getTestedClassesWithImplicitlyDefinedSchemaAndTableName()
128
    {
129
        $connection = TestUtil::getConnection();
130
        $platform = $connection->getDatabasePlatform();
131
132
        $expectedQuotedTableName = 'postgresql' === $platform->getName() ? '"order"' : 'order';
133
134
        return [
135
            [DDC2825ClassWithImplicitlyDefinedSchemaAndQuotedTableName::class, 'myschema', 'order', $expectedQuotedTableName],
136
        ];
137
    }
138
}
139
140
/**
141
 * @Entity
142
 * @Table(name="myschema.order")
143
 */
144
class DDC2825ClassWithImplicitlyDefinedSchemaAndQuotedTableName
145
{
146
    /**
147
     * @Id @GeneratedValue
148
     * @Column(type="integer")
149
     *
150
     * @var integer
151
     */
152
    public $id;
153
}
154