Passed
Pull Request — 2.6 (#7875)
by
unknown
10:22
created

MySqlSchemaToolTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 93
c 1
b 0
f 0
dl 0
loc 174
rs 10

10 Methods

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

164
            /** @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...
165
        } else {
166
            $config->setSchemaAssetsFilter($filterCallback);
167
        }
168
169
        $sqls = $tool->getUpdateSchemaSql($classes);
170
        $sqls = $this->filterSqls($sqls, ['entity_to_remove']);
171
        $this->assertCount(0, $sqls);
172
173
        if ($filterRegex) {
174
            $this->assertEquals($filterRegex, $config->getFilterSchemaAssetsExpression());
175
        } else {
176
            $this->assertSame($filterCallback, $config->getSchemaAssetsFilter());
177
        }
178
    }
179
180
    private function filterSqls(array $sqls, array $needles) : array
181
    {
182
        return array_filter($sqls, static function ($sql) use ($needles) {
183
            foreach ($needles as $needle) {
184
                if (strpos($sql, $needle) !== false) {
185
                    return true;
186
                }
187
            }
188
            return false;
189
        });
190
    }
191
}
192
193
/**
194
 * @Entity
195
 * @Table("namespace.entity")
196
 */
197
class MysqlSchemaNamespacedEntity
198
{
199
    /** @Column(type="integer") @Id @GeneratedValue */
200
    public $id;
201
}
202
203
/**
204
 * @Entity
205
 * @Table(name="entity_to_remove")
206
 */
207
class MyEntityToRemove
208
{
209
    /**
210
     * @Id @Column(type="integer")
211
     * @GeneratedValue(strategy="AUTO")
212
     */
213
    public $id;
214
}
215
216
/**
217
 * @Entity
218
 * @Table(name="other_entity_to_remove")
219
 */
220
class MyOtherEntityToRemove
221
{
222
    /**
223
     * @Id @Column(type="integer")
224
     * @GeneratedValue(strategy="AUTO")
225
     */
226
    public $id;
227
}
228