Passed
Pull Request — 2.7 (#7875)
by
unknown
09:09
created

SqliteSchemaToolTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 6 1
A provideUpdateSchemaSqlWithSchemaAssetFilter() 0 8 1
A testUpdateSchemaSql() 0 21 1
A setUp() 0 6 2
A filterCreateTable() 0 4 1
A testUpdateSchemaSqlWithSchemaAssetFilter() 0 28 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\SchemaTool;
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
class SqliteSchemaToolTest extends OrmFunctionalTestCase
17
{
18
    protected function setUp() : void
19
    {
20
        parent::setUp();
21
22
        if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'sqlite') {
23
            $this->markTestSkipped('The ' . self::class . ' requires the use of sqlite.');
24
        }
25
    }
26
27
    protected function tearDown() : void
28
    {
29
        $this->_em->getConnection()->exec('DROP TABLE IF EXISTS my_entity');
30
        $this->_em->getConnection()->exec('DROP TABLE IF EXISTS my_other_entity');
31
32
        parent::tearDown();
33
    }
34
35
    private function filterCreateTable(array $sqls, string $tableName) : array
36
    {
37
        return array_filter($sqls, static function (string $sql) use ($tableName) : bool {
38
            return strpos($sql, sprintf('CREATE TABLE %s (', $tableName)) === 0;
39
        });
40
    }
41
42
    public function testUpdateSchemaSql() : void
43
    {
44
        $classes = [
45
            $this->_em->getClassMetadata(MyEntity::class),
46
        ];
47
        $tool    = new SchemaTool($this->_em);
48
        $sqls    = $tool->getUpdateSchemaSql($classes);
49
        $sqls    = $this->filterCreateTable($sqls, 'my_entity');
50
        $this->assertCount(1, $sqls);
51
52
        $this->_em->getConnection()->exec(current($sqls));
53
        $sqls = $tool->getUpdateSchemaSql($classes);
54
        $sqls = array_filter($sqls, static function (string $sql) : bool {
55
            return (bool) strpos($sql, 'my_entity');
56
        });
57
        $this->assertCount(0, $sqls);
58
59
        $classes[] = $this->_em->getClassMetadata(MyOtherEntity::class);
60
        $sqls      = $tool->getUpdateSchemaSql($classes);
61
        self::assertCount(0, $this->filterCreateTable($sqls, 'my_entity'));
62
        self::assertCount(1, $this->filterCreateTable($sqls, 'my_other_entity'));
63
    }
64
65
    public function provideUpdateSchemaSqlWithSchemaAssetFilter() : array
66
    {
67
        return [
68
            ['/^(?!my_enti)/', null],
69
            [
70
                null,
71
                static function ($assetName) : bool {
72
                    return $assetName !== 'my_entity';
73
                },
74
            ],
75
        ];
76
    }
77
78
    /**
79
     * @dataProvider provideUpdateSchemaSqlWithSchemaAssetFilter
80
     */
81
    public function testUpdateSchemaSqlWithSchemaAssetFilter(?string $filterRegex, ?callable $filterCallback) : void
82
    {
83
        if ($filterRegex && ! method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) {
84
            self::markTestSkipped(sprintf('Test require %s::setFilterSchemaAssetsExpression method', Configuration::class));
85
        }
86
87
        $classes = [$this->_em->getClassMetadata(MyEntity::class)];
88
89
        $tool = new SchemaTool($this->_em);
90
        $tool->createSchema($classes);
91
92
        $config = $this->_em->getConnection()->getConfiguration();
93
        if ($filterRegex) {
94
            $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

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