Failed Conditions
Pull Request — 2.6 (#7875)
by
unknown
07:53
created

MySqlSchemaToolTest::testUpdateSchemaSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 17
rs 9.8666
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\SchemaTool;
4
5
use Doctrine\DBAL\Configuration;
6
use Doctrine\ORM\Tools\SchemaTool;
7
use Doctrine\Tests\OrmFunctionalTestCase;
8
use Doctrine\Tests\Models;
9
10
class MySqlSchemaToolTest extends OrmFunctionalTestCase
11
{
12
    protected function setUp() {
13
        parent::setUp();
14
        if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'mysql') {
15
            $this->markTestSkipped('The ' . __CLASS__ .' requires the use of mysql.');
16
        }
17
    }
18
19
    public function testGetCreateSchemaSql()
20
    {
21
        $classes = [
22
            $this->_em->getClassMetadata(Models\CMS\CmsGroup::class),
23
            $this->_em->getClassMetadata(Models\CMS\CmsUser::class),
24
            $this->_em->getClassMetadata(Models\CMS\CmsTag::class),
25
            $this->_em->getClassMetadata(Models\CMS\CmsAddress::class),
26
            $this->_em->getClassMetadata(Models\CMS\CmsEmail::class),
27
            $this->_em->getClassMetadata(Models\CMS\CmsPhonenumber::class),
28
        ];
29
30
        $tool = new SchemaTool($this->_em);
31
        $sql = $tool->getCreateSchemaSql($classes);
32
33
        $this->assertEquals("CREATE TABLE cms_groups (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(50) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[0]);
34
        $this->assertEquals("CREATE TABLE cms_users (id INT AUTO_INCREMENT NOT NULL, email_id INT DEFAULT NULL, status VARCHAR(50) DEFAULT NULL, username VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_3AF03EC5F85E0677 (username), UNIQUE INDEX UNIQ_3AF03EC5A832C1C9 (email_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[1]);
35
        $this->assertEquals("CREATE TABLE cms_users_groups (user_id INT NOT NULL, group_id INT NOT NULL, INDEX IDX_7EA9409AA76ED395 (user_id), INDEX IDX_7EA9409AFE54D947 (group_id), PRIMARY KEY(user_id, group_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[2]);
36
        $this->assertEquals("CREATE TABLE cms_users_tags (user_id INT NOT NULL, tag_id INT NOT NULL, INDEX IDX_93F5A1ADA76ED395 (user_id), INDEX IDX_93F5A1ADBAD26311 (tag_id), PRIMARY KEY(user_id, tag_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[3]);
37
        $this->assertEquals("CREATE TABLE cms_tags (id INT AUTO_INCREMENT NOT NULL, tag_name VARCHAR(50) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[4]);
38
        $this->assertEquals("CREATE TABLE cms_addresses (id INT AUTO_INCREMENT NOT NULL, user_id INT DEFAULT NULL, country VARCHAR(50) NOT NULL, zip VARCHAR(50) NOT NULL, city VARCHAR(50) NOT NULL, UNIQUE INDEX UNIQ_ACAC157BA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[5]);
39
        $this->assertEquals("CREATE TABLE cms_emails (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(250) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[6]);
40
        $this->assertEquals("CREATE TABLE cms_phonenumbers (phonenumber VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, INDEX IDX_F21F790FA76ED395 (user_id), PRIMARY KEY(phonenumber)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[7]);
41
        $this->assertEquals("ALTER TABLE cms_users ADD CONSTRAINT FK_3AF03EC5A832C1C9 FOREIGN KEY (email_id) REFERENCES cms_emails (id)", $sql[8]);
42
        $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[9]);
43
        $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AFE54D947 FOREIGN KEY (group_id) REFERENCES cms_groups (id)", $sql[10]);
44
        $this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[11]);
45
        $this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADBAD26311 FOREIGN KEY (tag_id) REFERENCES cms_tags (id)", $sql[12]);
46
        $this->assertEquals("ALTER TABLE cms_addresses ADD CONSTRAINT FK_ACAC157BA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[13]);
47
        $this->assertEquals("ALTER TABLE cms_phonenumbers ADD CONSTRAINT FK_F21F790FA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[14]);
48
49
        $this->assertEquals(15, count($sql));
50
    }
51
52
    public function testGetCreateSchemaSql2()
53
    {
54
        $classes = [
55
            $this->_em->getClassMetadata(Models\Generic\DecimalModel::class)
56
        ];
57
58
        $tool = new SchemaTool($this->_em);
59
        $sql = $tool->getCreateSchemaSql($classes);
60
61
        $this->assertEquals(1, count($sql));
62
        $this->assertEquals("CREATE TABLE decimal_model (id INT AUTO_INCREMENT NOT NULL, `decimal` NUMERIC(5, 2) NOT NULL, `high_scale` NUMERIC(14, 4) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[0]);
63
    }
64
65
    public function testGetCreateSchemaSql3()
66
    {
67
        $classes = [
68
            $this->_em->getClassMetadata(Models\Generic\BooleanModel::class)
69
        ];
70
71
        $tool = new SchemaTool($this->_em);
72
        $sql = $tool->getCreateSchemaSql($classes);
73
74
        $this->assertEquals(1, count($sql));
75
        $this->assertEquals("CREATE TABLE boolean_model (id INT AUTO_INCREMENT NOT NULL, booleanField TINYINT(1) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[0]);
76
    }
77
78
    /**
79
     * @group DBAL-204
80
     */
81
    public function testGetCreateSchemaSql4()
82
    {
83
        $classes = [
84
            $this->_em->getClassMetadata(MysqlSchemaNamespacedEntity::class)
85
        ];
86
87
        $tool = new SchemaTool($this->_em);
88
        $sql = $tool->getCreateSchemaSql($classes);
89
90
        $this->assertEquals(0, count($sql));
91
    }
92
93
    public function testUpdateSchemaSql(): void
94
    {
95
        $classes = [
96
            $this->_em->getClassMetadata(MyEntityToRemove::class),
97
        ];
98
        $tool = new SchemaTool($this->_em);
99
        $sqls = $tool->getUpdateSchemaSql($classes);
100
        $this->assertCount(1, $sqls);
101
        $this->assertEquals('CREATE TABLE entity_to_remove (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB', $sqls[0]);
102
103
        $this->_em->getConnection()->exec($sqls[0]);
104
        $sqls = $tool->getUpdateSchemaSql($classes);
105
        $this->assertCount(0, $sqls);
106
107
        $classes[] = $this->_em->getClassMetadata(Models\Generic\BooleanModel::class);
108
        $sqls = $tool->getUpdateSchemaSql($classes);
109
        $this->assertEquals('CREATE TABLE boolean_model (id INT AUTO_INCREMENT NOT NULL, booleanField TINYINT(1) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB', $sqls[0]);
110
    }
111
112
    public function provideUpdateSchemaSqlWithSchemaAssetFilter(): array
113
    {
114
        return [
115
            ['/^(?!entity_to_r)/', null],
116
            [null, function ($assetName): bool {
117
                return $assetName != 'entity_to_remove';
118
            }]
119
        ];
120
    }
121
122
    /**
123
     * @dataProvider provideUpdateSchemaSqlWithSchemaAssetFilter
124
     */
125
    public function testUpdateSchemaSqlWithSchemaAssetFilter(?string $filterRegex, ?callable $filterCallback): void
126
    {
127
        if ($filterRegex && !method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) {
128
            $this->markTestSkipped(sprintf("Test require %s::setFilterSchemaAssetsExpression method", Configuration::class));
129
        }
130
131
        if ($filterCallback && !method_exists(Configuration::class, 'setSchemaAssetsFilter')) {
132
            $this->markTestSkipped(sprintf("Test require %s::setSchemaAssetsFilter method", Configuration::class));
133
        }
134
135
        $classes = [
136
            $this->_em->getClassMetadata(MyEntityToRemove::class)
137
        ];
138
139
        $tool = new SchemaTool($this->_em);
140
        $tool->createSchema($classes);
141
142
        $config = $this->_em->getConnection()->getConfiguration();
143
        if ($filterRegex) {
144
            $config->setFilterSchemaAssetsExpression($filterRegex);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

144
            /** @scrutinizer ignore-deprecated */ $config->setFilterSchemaAssetsExpression($filterRegex);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
145
        } else {
146
            $config->setSchemaAssetsFilter($filterCallback);
147
        }
148
149
        $sqls = $tool->getUpdateSchemaSql($classes);
150
        $this->assertCount(0, $sqls);
151
152
        if ($filterRegex) {
153
            $this->assertEquals($filterRegex, $config->getFilterSchemaAssetsExpression());
154
        } else {
155
            $this->assertSame($filterCallback, $config->getSchemaAssetsFilter());
156
        }
157
    }
158
159
    protected function tearDown(): void
160
    {
161
        $this->_em->getConnection()->exec("DROP TABLE IF EXISTS entity_to_remove");
162
        parent::tearDown();
163
    }
164
}
165
166
/**
167
 * @Entity
168
 * @Table("namespace.entity")
169
 */
170
class MysqlSchemaNamespacedEntity
171
{
172
    /** @Column(type="integer") @Id @GeneratedValue */
173
    public $id;
174
}
175
176
/**
177
 * @Entity
178
 * @Table(name="entity_to_remove")
179
 */
180
class MyEntityToRemove
181
{
182
    /**
183
     * @Id @Column(type="integer")
184
     * @GeneratedValue(strategy="AUTO")
185
     */
186
    public $id;
187
}
188
189