Completed
Pull Request — 2.7 (#7875)
by Luís
11:38
created

GH7875Test::testUpdateSchemaSql()   A

Complexity

Conditions 1
Paths 1

Size

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

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