|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE |
|
6
|
|
|
|
|
7
|
|
|
This program is free software; you can redistribute it and/or modify |
|
8
|
|
|
it under the terms of the GNU General Public License as published by |
|
9
|
|
|
the Free Software Foundation; either version 2 of the License, or |
|
10
|
|
|
(at your option) any later version. |
|
11
|
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
|
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
GNU General Public License for more details. |
|
16
|
|
|
|
|
17
|
|
|
You should have received a copy of the GNU General Public License |
|
18
|
|
|
along with this program; if not, write to the Free Software |
|
19
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace TheCodingMachine\TDBM; |
|
23
|
|
|
|
|
24
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
25
|
|
|
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; |
|
26
|
|
|
use TheCodingMachine\TDBM\Utils\ImmutableCaster; |
|
27
|
|
|
|
|
28
|
|
|
class TDBMSchemaAnalyzerTest extends TDBMAbstractServiceTest |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var TDBMSchemaAnalyzer |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $tdbmSchemaAnalyzer; |
|
34
|
|
|
|
|
35
|
|
|
protected function setUp(): void |
|
36
|
|
|
{ |
|
37
|
|
|
parent::setUp(); |
|
38
|
|
|
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_'); |
|
39
|
|
|
$this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), new ArrayCache(), $schemaAnalyzer, Configuration::getDefaultLockFilePath()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testSchemaLock(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$schemaFromConnec = self::getConnection()->getSchemaManager()->createSchema(); |
|
45
|
|
|
$tableNames = []; |
|
46
|
|
|
//lock file doesn't save the database name so we have to replace it manually. |
|
47
|
|
|
ImmutableCaster::castSchemaToImmutable($schemaFromConnec); |
|
48
|
|
|
foreach ($schemaFromConnec->getTableNames() as $tableName) { |
|
49
|
|
|
$tableNames[] = str_replace(['tdbm_testcase', 'postgres'], 'public', $tableName); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_'); |
|
53
|
|
|
$cache = new ArrayCache(); |
|
54
|
|
|
$tdbmSchemaAnalyzer1 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer); |
|
55
|
|
|
|
|
56
|
|
|
$schemaFromAnalyser = $tdbmSchemaAnalyzer1->getSchema(true); |
|
57
|
|
|
$schemaFromAnalyserCached = $tdbmSchemaAnalyzer1->getSchema(); |
|
58
|
|
|
$this->assertEquals($tableNames, $schemaFromAnalyser->getTableNames()); |
|
59
|
|
|
$this->assertEquals($schemaFromAnalyser->getTableNames(), $schemaFromAnalyserCached->getTableNames()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testGetSchema(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_'); |
|
65
|
|
|
$cache = new ArrayCache(); |
|
66
|
|
|
$tdbmSchemaAnalyzer1 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer); |
|
|
|
|
|
|
67
|
|
|
$tdbmSchemaAnalyzer2 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer); |
|
68
|
|
|
|
|
69
|
|
|
// Why don't we go in all lines of code???? |
|
70
|
|
|
$schema1 = $tdbmSchemaAnalyzer1->getSchema(); |
|
71
|
|
|
// One more time to go through cache! |
|
72
|
|
|
$schema2 = $tdbmSchemaAnalyzer2->getSchema(); |
|
73
|
|
|
$this->assertTrue($schema1 === $schema2); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testGetIncomingForeignKeys(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_'); |
|
79
|
|
|
$cache = new ArrayCache(); |
|
80
|
|
|
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('users'); |
|
83
|
|
|
$this->assertCount(1, $fks); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testGetIncomingForeignKeys2(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_'); |
|
89
|
|
|
$cache = new ArrayCache(); |
|
90
|
|
|
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('contact'); |
|
93
|
|
|
$this->assertCount(1, $fks); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testGetIncomingForeignKeys3(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_'); |
|
99
|
|
|
$cache = new ArrayCache(); |
|
100
|
|
|
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('country'); |
|
103
|
|
|
$this->assertCount(5, $fks); |
|
104
|
|
|
$tables = [$fks[0]->getLocalTableName(), $fks[1]->getLocalTableName(), $fks[2]->getLocalTableName(), $fks[3]->getLocalTableName(), $fks[4]->getLocalTableName()]; |
|
105
|
|
|
$this->assertContains('users', $tables); |
|
106
|
|
|
$this->assertContains('all_nullable', $tables); |
|
107
|
|
|
$this->assertContains('boats', $tables); |
|
108
|
|
|
$this->assertContains('states', $tables); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testGetPivotTableLinkedToTable(): void |
|
112
|
|
|
{ |
|
113
|
|
|
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_'); |
|
114
|
|
|
$cache = new ArrayCache(); |
|
115
|
|
|
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer); |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
$pivotTables = $tdbmSchemaAnalyzer->getPivotTableLinkedToTable('rights'); |
|
118
|
|
|
$this->assertCount(1, $pivotTables); |
|
119
|
|
|
$this->assertEquals('roles_rights', $pivotTables[0]); |
|
120
|
|
|
|
|
121
|
|
|
$pivotTables = $tdbmSchemaAnalyzer->getPivotTableLinkedToTable('animal'); |
|
122
|
|
|
$this->assertCount(0, $pivotTables); |
|
123
|
|
|
|
|
124
|
|
|
$pivotTables = $tdbmSchemaAnalyzer->getPivotTableLinkedToTable('animal'); |
|
125
|
|
|
$this->assertCount(0, $pivotTables); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/*public function testGetCompulsoryColumnsWithNoInheritance() { |
|
129
|
|
|
$table = $this->tdbmSchemaAnalyzer->getSchema()->getTable('country'); |
|
130
|
|
|
$compulsoryColumns = $this->tdbmSchemaAnalyzer->getCompulsoryProperties($table); |
|
131
|
|
|
$this->assertCount(1, $compulsoryColumns); |
|
132
|
|
|
$this->assertArrayHasKey("label", $compulsoryColumns); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testGetCompulsoryColumnsWithInheritance() { |
|
136
|
|
|
$table = $this->tdbmSchemaAnalyzer->getSchema()->getTable('users'); |
|
137
|
|
|
$compulsoryColumns = $this->tdbmSchemaAnalyzer->getCompulsoryProperties($table); |
|
138
|
|
|
$this->assertCount(5, $compulsoryColumns); |
|
139
|
|
|
$this->assertEquals(['name', 'created_at', 'email', 'country_id', 'login'], array_keys($compulsoryColumns)); |
|
140
|
|
|
}*/ |
|
141
|
|
|
} |
|
142
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.