Passed
Pull Request — 2.7 (#7875)
by Luís
07:27
created

GH7875Test   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 41
c 1
b 0
f 0
dl 0
loc 97
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A provideUpdateSchemaSqlWithSchemaAssetFilter() 0 8 1
A cleanUpSchema() 0 10 2
A filterCreateTable() 0 4 1
A testUpdateSchemaSqlWithSchemaAssetFilter() 0 27 4
A testUpdateSchemaSql() 0 23 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\DBAL\Configuration;
8
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
9
use Doctrine\ORM\Tools\SchemaTool;
10
use Doctrine\Tests\OrmFunctionalTestCase;
11
use function array_filter;
12
use function current;
13
use function method_exists;
14
use function sprintf;
15
use function strpos;
16
17
/** @group GH7875 */
18
final class GH7875Test extends OrmFunctionalTestCase
19
{
20
    /** @after */
21
    public function cleanUpSchema() : void
22
    {
23
        $connection = $this->_em->getConnection();
24
25
        $connection->exec('DROP TABLE IF EXISTS gh7875_my_entity');
26
        $connection->exec('DROP TABLE IF EXISTS gh7875_my_other_entity');
27
28
        if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
29
            $connection->exec('DROP SEQUENCE IF EXISTS gh7875_my_entity_id_seq');
30
            $connection->exec('DROP SEQUENCE IF EXISTS gh7875_my_other_entity_id_seq');
31
        }
32
    }
33
34
    /**
35
     * @param string[] $sqls
36
     *
37
     * @return string[]
38
     */
39
    private function filterCreateTable(array $sqls, string $tableName) : array
40
    {
41
        return array_filter($sqls, static function (string $sql) use ($tableName) : bool {
42
            return strpos($sql, sprintf('CREATE TABLE %s (', $tableName)) === 0;
43
        });
44
    }
45
46
    public function testUpdateSchemaSql() : void
47
    {
48
        $classes = [$this->_em->getClassMetadata(GH7875MyEntity::class)];
49
50
        $tool = new SchemaTool($this->_em);
51
        $sqls = $this->filterCreateTable($tool->getUpdateSchemaSql($classes), 'gh7875_my_entity');
52
53
        self::assertCount(1, $sqls);
54
55
        $this->_em->getConnection()->exec(current($sqls));
56
57
        $sqls = array_filter($tool->getUpdateSchemaSql($classes), static function (string $sql) : bool {
58
            return strpos($sql, ' gh7875_my_entity ') !== false;
59
        });
60
61
        self::assertSame([], $sqls);
62
63
        $classes[] = $this->_em->getClassMetadata(GH7875MyOtherEntity::class);
64
65
        $sqls = $tool->getUpdateSchemaSql($classes);
66
67
        self::assertCount(0, $this->filterCreateTable($sqls, 'gh7875_my_entity'));
68
        self::assertCount(1, $this->filterCreateTable($sqls, 'gh7875_my_other_entity'));
69
    }
70
71
    /**
72
     * @return array<array<string|callable|null>>
73
     */
74
    public function provideUpdateSchemaSqlWithSchemaAssetFilter() : array
75
    {
76
        return [
77
            ['/^(?!my_enti)/', null],
78
            [
79
                null,
80
                static function ($assetName) : bool {
81
                    return $assetName !== 'gh7875_my_entity';
82
                },
83
            ],
84
        ];
85
    }
86
87
    /** @dataProvider provideUpdateSchemaSqlWithSchemaAssetFilter */
88
    public function testUpdateSchemaSqlWithSchemaAssetFilter(?string $filterRegex, ?callable $filterCallback) : void
89
    {
90
        if ($filterRegex && ! method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) {
91
            self::markTestSkipped(sprintf('Test require %s::setFilterSchemaAssetsExpression method', Configuration::class));
92
        }
93
94
        $classes = [$this->_em->getClassMetadata(GH7875MyEntity::class)];
95
96
        $tool = new SchemaTool($this->_em);
97
        $tool->createSchema($classes);
98
99
        $config = $this->_em->getConnection()->getConfiguration();
100
        if ($filterRegex) {
101
            $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

101
            /** @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...
102
        } else {
103
            $config->setSchemaAssetsFilter($filterCallback);
104
        }
105
106
        $previousFilter = $config->getSchemaAssetsFilter();
107
108
        $sqls = $tool->getUpdateSchemaSql($classes);
109
        $sqls = array_filter($sqls, static function (string $sql) : bool {
110
            return strpos($sql, ' gh7875_my_entity ') !== false;
111
        });
112
113
        self::assertCount(0, $sqls);
114
        self::assertSame($previousFilter, $config->getSchemaAssetsFilter());
115
    }
116
}
117
118
/**
119
 * @Entity
120
 * @Table(name="gh7875_my_entity")
121
 */
122
class GH7875MyEntity
123
{
124
    /** @Id @Column(type="integer") @GeneratedValue(strategy="AUTO") */
125
    public $id;
126
}
127
128
/**
129
 * @Entity
130
 * @Table(name="gh7875_my_other_entity")
131
 */
132
class GH7875MyOtherEntity
133
{
134
    /** @Id @Column(type="integer") @GeneratedValue(strategy="AUTO") */
135
    public $id;
136
}
137