1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventManager; |
6
|
|
|
use Doctrine\DBAL\Configuration; |
7
|
|
|
use Doctrine\DBAL\Platforms\MariaDb102Platform; |
8
|
|
|
use Doctrine\DBAL\Schema\MySqlSchemaManager; |
9
|
|
|
|
10
|
|
|
class MySqlSchemaManagerTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager |
15
|
|
|
*/ |
16
|
|
|
private $manager; |
17
|
|
|
|
18
|
|
|
protected function setUp() |
19
|
|
|
{ |
20
|
|
|
$eventManager = new EventManager(); |
21
|
|
|
$driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
22
|
|
|
$platform = $this->createMock('Doctrine\DBAL\Platforms\MySqlPlatform'); |
23
|
|
|
$this->conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
|
|
|
|
24
|
|
|
->setMethods(array('fetchAll')) |
25
|
|
|
->setConstructorArgs(array(array('platform' => $platform), $driverMock, new Configuration(), $eventManager)) |
26
|
|
|
->getMock(); |
27
|
|
|
$this->manager = new MySqlSchemaManager($this->conn); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testCompositeForeignKeys() |
31
|
|
|
{ |
32
|
|
|
$this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue($this->getFKDefinition())); |
33
|
|
|
$fkeys = $this->manager->listTableForeignKeys('dummy'); |
34
|
|
|
$this->assertEquals(1, count($fkeys), "Table has to have one foreign key."); |
35
|
|
|
|
36
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]); |
37
|
|
|
$this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getLocalColumns())); |
38
|
|
|
$this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getForeignColumns())); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testPortableTableColumnDefinition() |
42
|
|
|
{ |
43
|
|
|
$eventManager = new EventManager(); |
44
|
|
|
$driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
45
|
|
|
|
46
|
|
|
$platforms = [ |
47
|
|
|
'mysql' => new \Doctrine\DBAL\Platforms\MySqlPlatform(), |
48
|
|
|
'mariadb10.2' => new \Doctrine\DBAL\Platforms\MariaDb102Platform() |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
foreach ($platforms as $platformName => $platform) { |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
$conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
55
|
|
|
->setMethods(array('fetchAll')) |
56
|
|
|
->setConstructorArgs(array(array('platform' => $platform), $driverMock, new Configuration(), $eventManager)) |
57
|
|
|
->getMock(); |
58
|
|
|
$this->manager = new MySqlSchemaManager($conn); |
59
|
|
|
|
60
|
|
|
$conn->expects($this->once())->method('fetchAll')->will($this->returnValue($this->getColumnDefinition($platform))); |
61
|
|
|
$columns = $this->manager->listTableColumns('dummy'); |
62
|
|
|
|
63
|
|
|
$this->assertFalse($columns['id']->getNotnull()); |
64
|
|
|
$this->assertNull($columns['id']->getDefault()); |
65
|
|
|
|
66
|
|
|
$this->assertTrue($columns['created_by']->getNotnull()); |
67
|
|
|
$this->assertNull($columns['created_by']->getDefault()); |
68
|
|
|
|
69
|
|
|
$this->assertFalse($columns['text_1']->getNotnull()); |
70
|
|
|
$this->assertEquals('NULL', $columns['text_1']->getDefault()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getColumnDefinition($platform) |
75
|
|
|
{ |
76
|
|
|
$mariaDb102 = ($platform instanceof MariaDb102Platform); |
77
|
|
|
return [ |
78
|
|
|
[ |
79
|
|
|
'Field' => 'id', |
80
|
|
|
'Type' => 'int(10) unsigned', |
81
|
|
|
'Null' => 'YES', |
82
|
|
|
'Key' => '', |
83
|
|
|
'Default' => $mariaDb102 ? 'NULL' : null, |
84
|
|
|
'Extra' => 'auto_increment', |
85
|
|
|
'Comment' => '', |
86
|
|
|
'CharacterSet' => null, |
87
|
|
|
'Collation' => null |
88
|
|
|
],[ |
89
|
|
|
'Field'=> 'created_by', |
90
|
|
|
'Type'=> 'varchar(40)', |
91
|
|
|
'Null'=> 'NO', |
92
|
|
|
'Key'=> '', |
93
|
|
|
'Default'=> null, // both mysql and mariadb will return null (not null column) |
94
|
|
|
'Extra' => '', |
95
|
|
|
'Comment' => 'Creator name', |
96
|
|
|
'CharacterSet' => 'utf8', |
97
|
|
|
'Collation'=> 'utf8_unicode_ci' |
98
|
|
|
], |
99
|
|
|
[ |
100
|
|
|
'Field' => 'text_1', |
101
|
|
|
'Type' => 'varchar(50)', |
102
|
|
|
'Null' => 'YES', |
103
|
|
|
'Key' => '', |
104
|
|
|
'Default' => $mariaDb102 ? "'NULL'" : 'NULL', |
105
|
|
|
'Extra' => '', |
106
|
|
|
'Comment' => '', |
107
|
|
|
'CharacterSet' => 'utf8', |
108
|
|
|
'Collation'=> 'utf8_unicode_ci' |
109
|
|
|
] |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
public function getFKDefinition() |
116
|
|
|
{ |
117
|
|
|
return array( |
118
|
|
|
array( |
119
|
|
|
"CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E", |
120
|
|
|
"COLUMN_NAME" => "column_1", |
121
|
|
|
"REFERENCED_TABLE_NAME" => "dummy", |
122
|
|
|
"REFERENCED_COLUMN_NAME" => "column_1", |
123
|
|
|
"update_rule" => "RESTRICT", |
124
|
|
|
"delete_rule" => "RESTRICT", |
125
|
|
|
), |
126
|
|
|
array( |
127
|
|
|
"CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E", |
128
|
|
|
"COLUMN_NAME" => "column_2", |
129
|
|
|
"REFERENCED_TABLE_NAME" => "dummy", |
130
|
|
|
"REFERENCED_COLUMN_NAME" => "column_2", |
131
|
|
|
"update_rule" => "RESTRICT", |
132
|
|
|
"delete_rule" => "RESTRICT", |
133
|
|
|
), |
134
|
|
|
array( |
135
|
|
|
"CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E", |
136
|
|
|
"COLUMN_NAME" => "column_3", |
137
|
|
|
"REFERENCED_TABLE_NAME" => "dummy", |
138
|
|
|
"REFERENCED_COLUMN_NAME" => "column_3", |
139
|
|
|
"update_rule" => "RESTRICT", |
140
|
|
|
"delete_rule" => "RESTRICT", |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: