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

PostgreSqlSchemaToolTest::tearDown()   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
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\SchemaTool;
4
5
use Doctrine\ORM\Tools\SchemaTool;
6
use Doctrine\Tests\Models;
7
use Doctrine\Tests\OrmFunctionalTestCase;
8
use Doctrine\DBAL\Configuration;
9
10
class PostgreSqlSchemaToolTest extends OrmFunctionalTestCase
11
{
12
    protected function setUp()
13
    {
14
        parent::setUp();
15
16
        if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'postgresql') {
17
            $this->markTestSkipped('The ' . __CLASS__ .' requires the use of postgresql.');
18
        }
19
    }
20
21
    protected function tearDown()
22
    {
23
        $this->_em->getConnection()->exec("DROP TABLE IF EXISTS pg_entity_to_remove");
24
        $this->_em->getConnection()->exec("DROP SEQUENCE IF EXISTS pg_entity_to_remove_id_seq");
25
26
        parent::tearDown();
27
    }
28
29
    public function testPostgresMetadataSequenceIncrementedBy10()
30
    {
31
        $address = $this->_em->getClassMetadata(Models\CMS\CmsAddress::class);
32
33
        $this->assertEquals(1, $address->sequenceGeneratorDefinition['allocationSize']);
34
    }
35
36
    public function testGetCreateSchemaSql()
37
    {
38
        $classes = [
39
            $this->_em->getClassMetadata(Models\CMS\CmsAddress::class),
40
            $this->_em->getClassMetadata(Models\CMS\CmsUser::class),
41
            $this->_em->getClassMetadata(Models\CMS\CmsPhonenumber::class),
42
        ];
43
44
        $tool = new SchemaTool($this->_em);
45
        $sql = $tool->getCreateSchemaSql($classes);
46
        $sqlCount = count($sql);
47
48
        $this->assertEquals("CREATE TABLE cms_addresses (id INT NOT NULL, user_id INT DEFAULT NULL, country VARCHAR(50) NOT NULL, zip VARCHAR(50) NOT NULL, city VARCHAR(50) NOT NULL, PRIMARY KEY(id))", array_shift($sql));
49
        $this->assertEquals("CREATE UNIQUE INDEX UNIQ_ACAC157BA76ED395 ON cms_addresses (user_id)", array_shift($sql));
50
        $this->assertEquals("CREATE TABLE cms_users (id INT NOT NULL, email_id INT DEFAULT NULL, status VARCHAR(50) DEFAULT NULL, username VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id))", array_shift($sql));
51
        $this->assertEquals("CREATE UNIQUE INDEX UNIQ_3AF03EC5F85E0677 ON cms_users (username)", array_shift($sql));
52
        $this->assertEquals("CREATE UNIQUE INDEX UNIQ_3AF03EC5A832C1C9 ON cms_users (email_id)", array_shift($sql));
53
        $this->assertEquals("CREATE TABLE cms_users_groups (user_id INT NOT NULL, group_id INT NOT NULL, PRIMARY KEY(user_id, group_id))", array_shift($sql));
54
        $this->assertEquals("CREATE INDEX IDX_7EA9409AA76ED395 ON cms_users_groups (user_id)", array_shift($sql));
55
        $this->assertEquals("CREATE INDEX IDX_7EA9409AFE54D947 ON cms_users_groups (group_id)", array_shift($sql));
56
        $this->assertEquals("CREATE TABLE cms_users_tags (user_id INT NOT NULL, tag_id INT NOT NULL, PRIMARY KEY(user_id, tag_id))", array_shift($sql));
57
        $this->assertEquals("CREATE INDEX IDX_93F5A1ADA76ED395 ON cms_users_tags (user_id)", array_shift($sql));
58
        $this->assertEquals("CREATE INDEX IDX_93F5A1ADBAD26311 ON cms_users_tags (tag_id)", array_shift($sql));
59
        $this->assertEquals("CREATE TABLE cms_phonenumbers (phonenumber VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, PRIMARY KEY(phonenumber))", array_shift($sql));
60
        $this->assertEquals("CREATE INDEX IDX_F21F790FA76ED395 ON cms_phonenumbers (user_id)", array_shift($sql));
61
        $this->assertEquals("CREATE SEQUENCE cms_addresses_id_seq INCREMENT BY 1 MINVALUE 1 START 1", array_shift($sql));
62
        $this->assertEquals("CREATE SEQUENCE cms_users_id_seq INCREMENT BY 1 MINVALUE 1 START 1", array_shift($sql));
63
        $this->assertEquals("ALTER TABLE cms_addresses ADD CONSTRAINT FK_ACAC157BA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql));
64
        $this->assertEquals("ALTER TABLE cms_users ADD CONSTRAINT FK_3AF03EC5A832C1C9 FOREIGN KEY (email_id) REFERENCES cms_emails (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql));
65
        $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql));
66
        $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AFE54D947 FOREIGN KEY (group_id) REFERENCES cms_groups (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql));
67
        $this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql));
68
        $this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADBAD26311 FOREIGN KEY (tag_id) REFERENCES cms_tags (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql));
69
        $this->assertEquals("ALTER TABLE cms_phonenumbers ADD CONSTRAINT FK_F21F790FA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql));
70
71
        $this->assertEquals([], $sql, "SQL Array should be empty now.");
72
        $this->assertEquals(22, $sqlCount, "Total of 22 queries should be executed");
73
    }
74
75
    public function testGetCreateSchemaSql2()
76
    {
77
        $classes = [
78
            $this->_em->getClassMetadata(Models\Generic\DecimalModel::class)
79
        ];
80
81
        $tool = new SchemaTool($this->_em);
82
        $sql = $tool->getCreateSchemaSql($classes);
83
84
        $this->assertEquals(2, count($sql));
85
86
        $this->assertEquals('CREATE TABLE decimal_model (id INT NOT NULL, "decimal" NUMERIC(5, 2) NOT NULL, "high_scale" NUMERIC(14, 4) NOT NULL, PRIMARY KEY(id))', $sql[0]);
87
        $this->assertEquals("CREATE SEQUENCE decimal_model_id_seq INCREMENT BY 1 MINVALUE 1 START 1", $sql[1]);
88
    }
89
90
    public function testGetCreateSchemaSql3()
91
    {
92
        $classes = [
93
            $this->_em->getClassMetadata(Models\Generic\BooleanModel::class)
94
        ];
95
96
        $tool = new SchemaTool($this->_em);
97
        $sql = $tool->getCreateSchemaSql($classes);
98
99
        $this->assertEquals(2, count($sql));
100
        $this->assertEquals("CREATE TABLE boolean_model (id INT NOT NULL, booleanField BOOLEAN NOT NULL, PRIMARY KEY(id))", $sql[0]);
101
        $this->assertEquals("CREATE SEQUENCE boolean_model_id_seq INCREMENT BY 1 MINVALUE 1 START 1", $sql[1]);
102
    }
103
104
    public function testGetDropSchemaSql()
105
    {
106
        $classes = [
107
            $this->_em->getClassMetadata(Models\CMS\CmsAddress::class),
108
            $this->_em->getClassMetadata(Models\CMS\CmsUser::class),
109
            $this->_em->getClassMetadata(Models\CMS\CmsPhonenumber::class),
110
        ];
111
112
        $tool = new SchemaTool($this->_em);
113
        $sql = $tool->getDropSchemaSQL($classes);
114
115
        $this->assertEquals(17, count($sql));
116
117
        $dropSequenceSQLs = 0;
118
119
        foreach ($sql AS $stmt) {
120
            if (strpos($stmt, "DROP SEQUENCE") === 0) {
121
                $dropSequenceSQLs++;
122
            }
123
        }
124
        $this->assertEquals(4, $dropSequenceSQLs, "Expect 4 sequences to be dropped.");
125
    }
126
127
    /**
128
     * @group DDC-1657
129
     */
130
    public function testUpdateSchemaWithPostgreSQLSchema()
131
    {
132
        $classes = [
133
            $this->_em->getClassMetadata(DDC1657Screen::class),
134
            $this->_em->getClassMetadata(DDC1657Avatar::class),
135
        ];
136
137
        $tool = new SchemaTool($this->_em);
138
        $tool->createSchema($classes);
139
140
        $sql = $tool->getUpdateSchemaSql($classes);
141
        $sql = array_filter($sql, function($sql) { return (strpos($sql, "DROP SEQUENCE stonewood.") === 0); });
142
143
        $this->assertCount(0, $sql, implode("\n", $sql));
144
    }
145
146
    public function provideUpdateSchemaSqlWithSchemaAssetFilter() : array
147
    {
148
        return [
149
            ['/^(?!pg_entity_to_r)/', null],
150
            [null, function ($assetName) : bool {
151
                return $assetName !== 'pg_entity_to_remove';
152
            }]
153
        ];
154
    }
155
156
    /**
157
     * @dataProvider provideUpdateSchemaSqlWithSchemaAssetFilter
158
     */
159
    public function testUpdateSchemaSqlWithSchemaAssetFilter(?string $filterRegex, ?callable $filterCallback)
160
    {
161
        if ($filterRegex && !method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) {
162
            $this->markTestSkipped(sprintf("Test require %s::setFilterSchemaAssetsExpression method", Configuration::class));
163
        }
164
165
        if ($filterCallback && !method_exists(Configuration::class, 'setSchemaAssetsFilter')) {
166
            $this->markTestSkipped(sprintf("Test require %s::setSchemaAssetsFilter method", Configuration::class));
167
        }
168
169
        $classes = [
170
            $this->_em->getClassMetadata(PgMyEntityToRemove::class)
171
        ];
172
173
        $tool = new SchemaTool($this->_em);
174
        $tool->createSchema($classes);
175
176
        $config = $this->_em->getConnection()->getConfiguration();
177
        if ($filterRegex) {
178
            $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

178
            /** @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...
179
        } else {
180
            $config->setSchemaAssetsFilter($filterCallback);
181
        }
182
183
        $sqls = $tool->getUpdateSchemaSql($classes);
184
        $sqls = array_filter($sqls, function($sql) { return (strpos($sql, "pg_entity_to_remove") !== false); });
185
        $this->assertCount(0, $sqls);
186
187
        if ($filterRegex) {
188
            $this->assertEquals($filterRegex, $config->getFilterSchemaAssetsExpression());
189
        } else {
190
            $this->assertSame($filterCallback, $config->getSchemaAssetsFilter());
191
        }
192
    }
193
}
194
195
/**
196
 * @Entity
197
 * @Table(name="stonewood.screen")
198
 */
199
class DDC1657Screen
200
{
201
    /**
202
     * Identifier
203
     * @var int
204
     *
205
     * @Id
206
     * @GeneratedValue(strategy="IDENTITY")
207
     * @Column(name="pk", type="integer", nullable=false)
208
     */
209
    private $pk;
0 ignored issues
show
introduced by
The private property $pk is not used, and could be removed.
Loading history...
210
211
    /**
212
     * Title
213
     * @var string
214
     *
215
     * @Column(name="title", type="string", length=255, nullable=false)
216
     */
217
    private $title;
0 ignored issues
show
introduced by
The private property $title is not used, and could be removed.
Loading history...
218
219
    /**
220
     * Path
221
     * @var string
222
     *
223
     * @Column(name="path", type="string", length=255, nullable=false)
224
     */
225
    private $path;
0 ignored issues
show
introduced by
The private property $path is not used, and could be removed.
Loading history...
226
227
    /**
228
     * Register date
229
     * @var Date
0 ignored issues
show
Bug introduced by
The type Doctrine\Tests\ORM\Functional\SchemaTool\Date was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
230
     *
231
     * @Column(name="ddate", type="date", nullable=false)
232
     */
233
    private $ddate;
0 ignored issues
show
introduced by
The private property $ddate is not used, and could be removed.
Loading history...
234
235
    /**
236
     * Avatar
237
     * @var Stonewood\Model\Entity\Avatar
0 ignored issues
show
Bug introduced by
The type Doctrine\Tests\ORM\Funct...ood\Model\Entity\Avatar was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
238
     *
239
     * @ManyToOne(targetEntity="DDC1657Avatar")
240
     * @JoinColumn(name="pk_avatar", referencedColumnName="pk", nullable=true, onDelete="CASCADE")
241
     */
242
    private $avatar;
0 ignored issues
show
introduced by
The private property $avatar is not used, and could be removed.
Loading history...
243
}
244
245
/**
246
 * @Entity
247
 * @Table(name="stonewood.avatar")
248
 */
249
class DDC1657Avatar
250
{
251
    /**
252
     * Identifier
253
     * @var int
254
     *
255
     * @Id
256
     * @GeneratedValue(strategy="IDENTITY")
257
     * @Column(name="pk", type="integer", nullable=false)
258
     */
259
    private $pk;
260
}
261
262
/**
263
 * @Entity
264
 * @Table(name="pg_entity_to_remove")
265
 */
266
class PgMyEntityToRemove
267
{
268
    /**
269
     * @Id @Column(type="integer")
270
     * @GeneratedValue(strategy="AUTO")
271
     */
272
    public $id;
273
}
274