1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventManager; |
6
|
|
|
use Doctrine\DBAL\Configuration; |
7
|
|
|
use Doctrine\DBAL\Schema\MySqlSchemaManager; |
8
|
|
|
|
9
|
|
|
class MySqlSchemaManagerTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager |
14
|
|
|
*/ |
15
|
|
|
private $manager; |
16
|
|
|
|
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
$eventManager = new EventManager(); |
20
|
|
|
$driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
21
|
|
|
$platform = $this->createMock('Doctrine\DBAL\Platforms\MySqlPlatform'); |
22
|
|
|
$this->conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
|
|
|
|
23
|
|
|
->setMethods(array('fetchAll')) |
24
|
|
|
->setConstructorArgs(array(array('platform' => $platform), $driverMock, new Configuration(), $eventManager)) |
25
|
|
|
->getMock(); |
26
|
|
|
$this->manager = new MySqlSchemaManager($this->conn); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testCompositeForeignKeys() |
30
|
|
|
{ |
31
|
|
|
$this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue($this->getFKDefinition())); |
32
|
|
|
$fkeys = $this->manager->listTableForeignKeys('dummy'); |
33
|
|
|
$this->assertEquals(1, count($fkeys), "Table has to have one foreign key."); |
34
|
|
|
|
35
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]); |
36
|
|
|
$this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getLocalColumns())); |
37
|
|
|
$this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getForeignColumns())); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testPortableTableColumnDefinition() |
41
|
|
|
{ |
42
|
|
|
$eventManager = new EventManager(); |
43
|
|
|
$driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
44
|
|
|
$platform = new \Doctrine\DBAL\Platforms\MySqlPlatform(); |
45
|
|
|
$conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
46
|
|
|
->setMethods(array('fetchAll')) |
47
|
|
|
->setConstructorArgs(array(array('platform' => $platform), $driverMock, new Configuration(), $eventManager)) |
48
|
|
|
->getMock(); |
49
|
|
|
$this->manager = new MySqlSchemaManager($conn); |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
$conn->expects($this->once())->method('fetchAll')->will($this->returnValue($this->getColumnDefinition())); |
53
|
|
|
$columns = $this->manager->listTableColumns('dummy'); |
54
|
|
|
$this->assertEquals(count($this->getColumnDefinition()), count($columns)); |
55
|
|
|
$this->assertTrue($columns['id']->getNotnull()); |
56
|
|
|
$this->assertNull($columns['id']->getDefault()); |
57
|
|
|
$this->assertFalse($columns['updated_at']->getNotnull()); |
58
|
|
|
$this->assertNull($columns['updated_at']->getDefault()); |
59
|
|
|
$this->assertFalse($columns['created_by']->getNotnull()); |
60
|
|
|
$this->assertNull($columns['created_by']->getDefault()); |
61
|
|
|
$this->assertEquals(null, $columns['text_2']->getDefault()); |
62
|
|
|
$this->assertEquals("'NULL'", $columns['text_1']->getDefault()); |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getColumnDefinition() |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
[ |
70
|
|
|
'Field' => 'id', |
71
|
|
|
'Type' => 'int(10) unsigned', |
72
|
|
|
'Null' => 'NO', |
73
|
|
|
'Key' => 'PRI', |
74
|
|
|
'Default' => null, |
75
|
|
|
'Extra' => 'auto_increment', |
76
|
|
|
'Comment' => '', |
77
|
|
|
'CharacterSet' => null, |
78
|
|
|
'Collation' => null |
79
|
|
|
],[ |
80
|
|
|
'Field'=> 'created_by', |
81
|
|
|
'Type'=> 'varchar(40)', |
82
|
|
|
'Null'=> 'YES', |
83
|
|
|
'Key'=> '', |
84
|
|
|
'Default'=> null, // MySQL 5.1 - 5.7 sends: null |
85
|
|
|
'Extra' => '', |
86
|
|
|
'Comment' => 'Creator name', |
87
|
|
|
'CharacterSet' => 'utf8', |
88
|
|
|
'Collation'=> 'utf8_unicode_ci' |
89
|
|
|
], |
90
|
|
|
// Mariadb since 10.2.7, see https://jira.mariadb.org/browse/MDEV-13341 |
91
|
|
|
[ |
92
|
|
|
'Field' => 'updated_at', |
93
|
|
|
'Type' => 'datetime', |
94
|
|
|
'Null' => 'YES', |
95
|
|
|
'Key' => '', |
96
|
|
|
'Default' => 'NULL', // MariaDB 10.2.7 return 'NULL' |
|
|
|
|
97
|
|
|
'Extra' => '', |
98
|
|
|
'Comment' => 'Record last update timestamp', |
99
|
|
|
'CharacterSet' => null, |
100
|
|
|
'Collation' => null |
101
|
|
|
], |
102
|
|
|
// Mariadb 10.2 test with defaults that could be |
103
|
|
|
// ambiguous (see text_1 and text_2 Default) |
104
|
|
|
[ |
105
|
|
|
'Field' => 'text_1', |
106
|
|
|
'Type' => 'varchar(50)', |
107
|
|
|
'Null' => 'YES', |
108
|
|
|
'Key' => '', |
109
|
|
|
'Default' => "'NULL'", |
110
|
|
|
'Extra' => '', |
111
|
|
|
'Comment' => '', |
112
|
|
|
'CharacterSet' => 'utf8', |
113
|
|
|
'Collation'=> 'utf8_unicode_ci' |
114
|
|
|
],[ |
115
|
|
|
'Field' => 'text_2', |
116
|
|
|
'Type' => 'varchar(50)', |
117
|
|
|
'Null' => 'YES', |
118
|
|
|
'Key' => '', |
119
|
|
|
'Default' => 'NULL', |
120
|
|
|
'Extra' => '', |
121
|
|
|
'Comment' => '', |
122
|
|
|
'CharacterSet' => 'utf8', |
123
|
|
|
'Collation'=> 'utf8_unicode_ci' |
124
|
|
|
], |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
public function getFKDefinition() |
133
|
|
|
{ |
134
|
|
|
return array( |
135
|
|
|
array( |
136
|
|
|
"CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E", |
137
|
|
|
"COLUMN_NAME" => "column_1", |
138
|
|
|
"REFERENCED_TABLE_NAME" => "dummy", |
139
|
|
|
"REFERENCED_COLUMN_NAME" => "column_1", |
140
|
|
|
"update_rule" => "RESTRICT", |
141
|
|
|
"delete_rule" => "RESTRICT", |
142
|
|
|
), |
143
|
|
|
array( |
144
|
|
|
"CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E", |
145
|
|
|
"COLUMN_NAME" => "column_2", |
146
|
|
|
"REFERENCED_TABLE_NAME" => "dummy", |
147
|
|
|
"REFERENCED_COLUMN_NAME" => "column_2", |
148
|
|
|
"update_rule" => "RESTRICT", |
149
|
|
|
"delete_rule" => "RESTRICT", |
150
|
|
|
), |
151
|
|
|
array( |
152
|
|
|
"CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E", |
153
|
|
|
"COLUMN_NAME" => "column_3", |
154
|
|
|
"REFERENCED_TABLE_NAME" => "dummy", |
155
|
|
|
"REFERENCED_COLUMN_NAME" => "column_3", |
156
|
|
|
"update_rule" => "RESTRICT", |
157
|
|
|
"delete_rule" => "RESTRICT", |
158
|
|
|
) |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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: