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

MySqlSchemaToolTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 83
c 1
b 0
f 0
dl 0
loc 155
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetCreateSchemaSql2() 0 11 1
A testUpdateSchemaSql() 0 17 1
A setUp() 0 6 2
A testGetCreateSchemaSql() 0 31 1
A testGetCreateSchemaSql3() 0 11 1
A provideUpdateSchemaSqlWithSchemaAssetFilter() 0 6 1
A testGetCreateSchemaSql4() 0 10 1
B testUpdateSchemaSqlWithSchemaAssetFilter() 0 31 7
A tearDown() 0 5 1
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
    {
14
        parent::setUp();
15
16
        if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'mysql') {
17
            $this->markTestSkipped('The ' . __CLASS__ .' requires the use of mysql.');
18
        }
19
    }
20
21
    protected function tearDown()
22
    {
23
        $this->_em->getConnection()->exec("DROP TABLE IF EXISTS entity_to_remove");
24
25
        parent::tearDown();
26
    }
27
28
    public function testGetCreateSchemaSql()
29
    {
30
        $classes = [
31
            $this->_em->getClassMetadata(Models\CMS\CmsGroup::class),
32
            $this->_em->getClassMetadata(Models\CMS\CmsUser::class),
33
            $this->_em->getClassMetadata(Models\CMS\CmsTag::class),
34
            $this->_em->getClassMetadata(Models\CMS\CmsAddress::class),
35
            $this->_em->getClassMetadata(Models\CMS\CmsEmail::class),
36
            $this->_em->getClassMetadata(Models\CMS\CmsPhonenumber::class),
37
        ];
38
39
        $tool = new SchemaTool($this->_em);
40
        $sql = $tool->getCreateSchemaSql($classes);
41
42
        $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]);
43
        $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]);
44
        $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]);
45
        $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]);
46
        $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]);
47
        $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]);
48
        $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]);
49
        $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]);
50
        $this->assertEquals("ALTER TABLE cms_users ADD CONSTRAINT FK_3AF03EC5A832C1C9 FOREIGN KEY (email_id) REFERENCES cms_emails (id)", $sql[8]);
51
        $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[9]);
52
        $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AFE54D947 FOREIGN KEY (group_id) REFERENCES cms_groups (id)", $sql[10]);
53
        $this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[11]);
54
        $this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADBAD26311 FOREIGN KEY (tag_id) REFERENCES cms_tags (id)", $sql[12]);
55
        $this->assertEquals("ALTER TABLE cms_addresses ADD CONSTRAINT FK_ACAC157BA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[13]);
56
        $this->assertEquals("ALTER TABLE cms_phonenumbers ADD CONSTRAINT FK_F21F790FA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[14]);
57
58
        $this->assertEquals(15, count($sql));
59
    }
60
61
    public function testGetCreateSchemaSql2()
62
    {
63
        $classes = [
64
            $this->_em->getClassMetadata(Models\Generic\DecimalModel::class)
65
        ];
66
67
        $tool = new SchemaTool($this->_em);
68
        $sql = $tool->getCreateSchemaSql($classes);
69
70
        $this->assertEquals(1, count($sql));
71
        $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]);
72
    }
73
74
    public function testGetCreateSchemaSql3()
75
    {
76
        $classes = [
77
            $this->_em->getClassMetadata(Models\Generic\BooleanModel::class)
78
        ];
79
80
        $tool = new SchemaTool($this->_em);
81
        $sql = $tool->getCreateSchemaSql($classes);
82
83
        $this->assertEquals(1, count($sql));
84
        $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]);
85
    }
86
87
    /**
88
     * @group DBAL-204
89
     */
90
    public function testGetCreateSchemaSql4()
91
    {
92
        $classes = [
93
            $this->_em->getClassMetadata(MysqlSchemaNamespacedEntity::class)
94
        ];
95
96
        $tool = new SchemaTool($this->_em);
97
        $sql = $tool->getCreateSchemaSql($classes);
98
99
        $this->assertEquals(0, count($sql));
100
    }
101
102
    public function testUpdateSchemaSql()
103
    {
104
        $classes = [
105
            $this->_em->getClassMetadata(MyEntityToRemove::class),
106
        ];
107
        $tool = new SchemaTool($this->_em);
108
        $sqls = $tool->getUpdateSchemaSql($classes);
109
        $this->assertCount(1, $sqls);
110
        $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]);
111
112
        $this->_em->getConnection()->exec($sqls[0]);
113
        $sqls = $tool->getUpdateSchemaSql($classes);
114
        $this->assertCount(0, $sqls);
115
116
        $classes[] = $this->_em->getClassMetadata(Models\Generic\BooleanModel::class);
117
        $sqls = $tool->getUpdateSchemaSql($classes);
118
        $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]);
119
    }
120
121
    public function provideUpdateSchemaSqlWithSchemaAssetFilter() : array
122
    {
123
        return [
124
            ['/^(?!entity_to_r)/', null],
125
            [null, function ($assetName) : bool {
126
                return $assetName !== 'entity_to_remove';
127
            }]
128
        ];
129
    }
130
131
    /**
132
     * @dataProvider provideUpdateSchemaSqlWithSchemaAssetFilter
133
     */
134
    public function testUpdateSchemaSqlWithSchemaAssetFilter(?string $filterRegex, ?callable $filterCallback)
135
    {
136
        if ($filterRegex && !method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) {
137
            $this->markTestSkipped(sprintf("Test require %s::setFilterSchemaAssetsExpression method", Configuration::class));
138
        }
139
140
        if ($filterCallback && !method_exists(Configuration::class, 'setSchemaAssetsFilter')) {
141
            $this->markTestSkipped(sprintf("Test require %s::setSchemaAssetsFilter method", Configuration::class));
142
        }
143
144
        $classes = [
145
            $this->_em->getClassMetadata(MyEntityToRemove::class)
146
        ];
147
148
        $tool = new SchemaTool($this->_em);
149
        $tool->createSchema($classes);
150
151
        $config = $this->_em->getConnection()->getConfiguration();
152
        if ($filterRegex) {
153
            $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

153
            /** @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...
154
        } else {
155
            $config->setSchemaAssetsFilter($filterCallback);
156
        }
157
158
        $sqls = $tool->getUpdateSchemaSql($classes);
159
        $this->assertCount(0, $sqls);
160
161
        if ($filterRegex) {
162
            $this->assertEquals($filterRegex, $config->getFilterSchemaAssetsExpression());
163
        } else {
164
            $this->assertSame($filterCallback, $config->getSchemaAssetsFilter());
165
        }
166
    }
167
}
168
169
/**
170
 * @Entity
171
 * @Table("namespace.entity")
172
 */
173
class MysqlSchemaNamespacedEntity
174
{
175
    /** @Column(type="integer") @Id @GeneratedValue */
176
    public $id;
177
}
178
179
/**
180
 * @Entity
181
 * @Table(name="entity_to_remove")
182
 */
183
class MyEntityToRemove
184
{
185
    /**
186
     * @Id @Column(type="integer")
187
     * @GeneratedValue(strategy="AUTO")
188
     */
189
    public $id;
190
}
191
192