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() |
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() |
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() |
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
|
|
|
$this->assertCount(0, $this->filterCreateTable($sqls, 'my_entity')); |
62
|
|
|
$this->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) |
82
|
|
|
{ |
83
|
|
|
if (! method_exists(Configuration::class, 'setSchemaAssetsFilter')) { |
84
|
|
|
$this->markTestSkipped(sprintf('Test require %s::setSchemaAssetsFilter method', Configuration::class)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($filterRegex && ! method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) { |
88
|
|
|
$this->markTestSkipped(sprintf('Test require %s::setFilterSchemaAssetsExpression method', Configuration::class)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$classes = [$this->_em->getClassMetadata(MyEntity::class)]; |
92
|
|
|
|
93
|
|
|
$tool = new SchemaTool($this->_em); |
94
|
|
|
$tool->createSchema($classes); |
95
|
|
|
|
96
|
|
|
$config = $this->_em->getConnection()->getConfiguration(); |
97
|
|
|
if ($filterRegex) { |
98
|
|
|
$config->setFilterSchemaAssetsExpression($filterRegex); |
|
|
|
|
99
|
|
|
} else { |
100
|
|
|
$config->setSchemaAssetsFilter($filterCallback); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$sqls = $tool->getUpdateSchemaSql($classes); |
104
|
|
|
$sqls = array_filter($sqls, static function (string $sql) : bool { |
105
|
|
|
return (bool) strpos($sql, 'my_entity'); |
106
|
|
|
}); |
107
|
|
|
$this->assertCount(0, $sqls); |
108
|
|
|
|
109
|
|
|
if ($filterRegex) { |
110
|
|
|
$this->assertEquals($filterRegex, $config->getFilterSchemaAssetsExpression()); |
111
|
|
|
} else { |
112
|
|
|
$this->assertSame($filterCallback, $config->getSchemaAssetsFilter()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @Entity |
119
|
|
|
* @Table(name="my_entity") |
120
|
|
|
*/ |
121
|
|
|
class MyEntity |
122
|
|
|
{ |
123
|
|
|
/** |
124
|
|
|
* @Id @Column(type="integer") |
125
|
|
|
* @GeneratedValue(strategy="AUTO") |
126
|
|
|
*/ |
127
|
|
|
public $id; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @Entity |
132
|
|
|
* @Table(name="my_other_entity") |
133
|
|
|
*/ |
134
|
|
|
class MyOtherEntity |
135
|
|
|
{ |
136
|
|
|
/** |
137
|
|
|
* @Id @Column(type="integer") |
138
|
|
|
* @GeneratedValue(strategy="AUTO") |
139
|
|
|
*/ |
140
|
|
|
public $id; |
141
|
|
|
} |
142
|
|
|
|
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.