|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maketok\DataMigration\Storage\Db; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
|
6
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
7
|
|
|
use Maketok\DataMigration\Action\ArrayConfig; |
|
8
|
|
|
|
|
9
|
|
|
class DBALMysqlResourceTest extends \PHPUnit_Framework_TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var DBALMysqlResource |
|
13
|
|
|
*/ |
|
14
|
|
|
private $resource; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ArrayConfig |
|
17
|
|
|
*/ |
|
18
|
|
|
private $config; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->config = new ArrayConfig(); |
|
23
|
|
|
$driverMock = $this->getMockBuilder('\Doctrine\DBAL\Driver\PDOMySql\Driver')->getMock(); |
|
24
|
|
|
$platform = new MySqlPlatform(); |
|
25
|
|
|
$driverMock->expects($this->any())->method('getDatabasePlatform')->willReturn($platform); |
|
26
|
|
|
$this->config['db_driver'] = $driverMock; |
|
27
|
|
|
$con = $this->getMockBuilder('\Doctrine\DBAL\Driver\Connection')->getMock(); |
|
28
|
|
|
$con->expects($this->any())->method('quote')->willReturnCallback(function ($var) { |
|
29
|
|
|
return '\'' . $var . '\''; |
|
30
|
|
|
}); |
|
31
|
|
|
$this->config['db_pdo'] = $con; |
|
32
|
|
|
$this->resource = new DBALMysqlResource($this->config); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testGetDeleteSql() |
|
36
|
|
|
{ |
|
37
|
|
|
$sql = $this->resource->getDeleteUsingTempPkSql('test_table1', 'tmp_test_table1', ['id']); |
|
38
|
|
|
$expected = <<<MYSQL |
|
39
|
|
|
DELETE main_table FROM `test_table1` AS main_table |
|
40
|
|
|
JOIN `tmp_test_table1` AS tmp_table ON `main_table`.`id`=`tmp_table`.`id` |
|
41
|
|
|
MYSQL; |
|
42
|
|
|
$this->assertEquals($expected, $sql); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testGetLoadSql() |
|
46
|
|
|
{ |
|
47
|
|
|
$sql = $this->resource->getLoadDataSql( |
|
48
|
|
|
'test_table1', '/tmp/file1', true, ['name', 'code'], ['name' => 'code', 'code' => 'name'], |
|
49
|
|
|
",", '"', "\\", "\n", true); |
|
50
|
|
|
$expected = <<<MYSQL |
|
51
|
|
|
LOAD DATA LOCAL INFILE '/tmp/file1' |
|
52
|
|
|
INTO TABLE `test_table1` |
|
53
|
|
|
CHARACTER SET UTF8 |
|
54
|
|
|
FIELDS |
|
55
|
|
|
TERMINATED BY ',' |
|
56
|
|
|
OPTIONALLY ENCLOSED BY '"' |
|
57
|
|
|
ESCAPED BY '\\' |
|
58
|
|
|
LINES |
|
59
|
|
|
TERMINATED BY '\n' |
|
60
|
|
|
(name,code) |
|
61
|
|
|
SET name=code,code=name |
|
62
|
|
|
MYSQL; |
|
63
|
|
|
$this->assertEquals($expected, $sql); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testGetMoveSql() |
|
67
|
|
|
{ |
|
68
|
|
|
$sql = $this->resource->getMoveSql('tmp_table1', 'table1', ['id', 'name'], ['id' => 1], ['name']); |
|
69
|
|
|
$expected = <<<MYSQL |
|
70
|
|
|
INSERT INTO `table1` (`id`,`name`) |
|
71
|
|
|
SELECT `id`,`name` FROM `tmp_table1` |
|
72
|
|
|
WHERE `tmp_table1`.`id`='1' |
|
73
|
|
|
ORDER BY `name` ASC |
|
74
|
|
|
ON DUPLICATE KEY UPDATE `id`=VALUES(`id`),`name`=VALUES(`name`) |
|
75
|
|
|
MYSQL; |
|
76
|
|
|
$this->assertEquals($expected, $sql); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testGetMoveSql2() |
|
80
|
|
|
{ |
|
81
|
|
|
$sql = $this->resource->getMoveSql('tmp_table1', 'table1', ['id', 'name'], ['id' => ['eq' => 1]], ['name']); |
|
82
|
|
|
$expected = <<<MYSQL |
|
83
|
|
|
INSERT INTO `table1` (`id`,`name`) |
|
84
|
|
|
SELECT `id`,`name` FROM `tmp_table1` |
|
85
|
|
|
WHERE `tmp_table1`.`id`='1' |
|
86
|
|
|
ORDER BY `name` ASC |
|
87
|
|
|
ON DUPLICATE KEY UPDATE `id`=VALUES(`id`),`name`=VALUES(`name`) |
|
88
|
|
|
MYSQL; |
|
89
|
|
|
$this->assertEquals($expected, $sql); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testGetMoveSql3() |
|
93
|
|
|
{ |
|
94
|
|
|
$sql = $this->resource->getMoveSql('tmp_table1', 'table1', ['id', 'name'], ['id' => ['neq' => 1]], ['name']); |
|
95
|
|
|
$expected = <<<MYSQL |
|
96
|
|
|
INSERT INTO `table1` (`id`,`name`) |
|
97
|
|
|
SELECT `id`,`name` FROM `tmp_table1` |
|
98
|
|
|
WHERE `tmp_table1`.`id`<>'1' |
|
99
|
|
|
ORDER BY `name` ASC |
|
100
|
|
|
ON DUPLICATE KEY UPDATE `id`=VALUES(`id`),`name`=VALUES(`name`) |
|
101
|
|
|
MYSQL; |
|
102
|
|
|
$this->assertEquals($expected, $sql); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testGetMoveSql4() |
|
106
|
|
|
{ |
|
107
|
|
|
$sql = $this->resource->getMoveSql('tmp_table1', 'table1', ['id', 'name'], ['id' => ['in' => [1]]], ['name']); |
|
108
|
|
|
$expected = <<<MYSQL |
|
109
|
|
|
INSERT INTO `table1` (`id`,`name`) |
|
110
|
|
|
SELECT `id`,`name` FROM `tmp_table1` |
|
111
|
|
|
WHERE `tmp_table1`.`id` in ('1') |
|
112
|
|
|
ORDER BY `name` ASC |
|
113
|
|
|
ON DUPLICATE KEY UPDATE `id`=VALUES(`id`),`name`=VALUES(`name`) |
|
114
|
|
|
MYSQL; |
|
115
|
|
|
$this->assertEquals($expected, $sql); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testGetMoveSql5() |
|
119
|
|
|
{ |
|
120
|
|
|
$sql = $this->resource->getMoveSql('tmp_table1', 'table1', ['id', 'name'], ['id' => ['nin' => [1]]], ['name']); |
|
121
|
|
|
$expected = <<<MYSQL |
|
122
|
|
|
INSERT INTO `table1` (`id`,`name`) |
|
123
|
|
|
SELECT `id`,`name` FROM `tmp_table1` |
|
124
|
|
|
WHERE `tmp_table1`.`id` not in ('1') |
|
125
|
|
|
ORDER BY `name` ASC |
|
126
|
|
|
ON DUPLICATE KEY UPDATE `id`=VALUES(`id`),`name`=VALUES(`name`) |
|
127
|
|
|
MYSQL; |
|
128
|
|
|
$this->assertEquals($expected, $sql); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @expectedException \Maketok\DataMigration\Storage\Exception\ParsingException |
|
133
|
|
|
* @expectedExceptionMessage Condition should contain only 1 element |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testGetWrongMoveSql1() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->resource->getMoveSql('tmp_table1', 'table1', ['id', 'name'], ['id' => ['eq',2]], ['name']); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @expectedException \Maketok\DataMigration\Storage\Exception\ParsingException |
|
142
|
|
|
* @expectedExceptionMessage Could not resolve condition operation wrong |
|
143
|
|
|
*/ |
|
144
|
|
|
public function testGetWrongMoveSql2() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->resource->getMoveSql('tmp_table1', 'table1', ['id', 'name'], ['id' => ['wrong' => 2]], ['name']); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @expectedException \Maketok\DataMigration\Storage\Exception\ParsingException |
|
151
|
|
|
* @expectedExceptionMessage Can not use 'in' operation with non array |
|
152
|
|
|
*/ |
|
153
|
|
|
public function testGetWrongMoveSql3() |
|
154
|
|
|
{ |
|
155
|
|
|
$this->resource->getMoveSql('tmp_table1', 'table1', ['id', 'name'], ['id' => ['in' => 2]], ['name']); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @expectedException \Maketok\DataMigration\Storage\Exception\ParsingException |
|
160
|
|
|
* @expectedExceptionMessage Can not use 'nin' operation with non array |
|
161
|
|
|
*/ |
|
162
|
|
|
public function testGetWrongMoveSql4() |
|
163
|
|
|
{ |
|
164
|
|
|
$this->resource->getMoveSql('tmp_table1', 'table1', ['id', 'name'], ['id' => ['nin' => 2]], ['name']); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function testGetDumpSql() |
|
168
|
|
|
{ |
|
169
|
|
|
$sql = $this->resource->getDumpDataSql('tmp_table1', ['id', 'name']); |
|
170
|
|
|
$expected = <<<MYSQL |
|
171
|
|
|
SELECT `id`,`name` FROM `tmp_table1` |
|
172
|
|
|
LIMIT ? OFFSET ? |
|
173
|
|
|
MYSQL; |
|
174
|
|
|
$this->assertEquals($expected, $sql); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testGetCreateTableSql() |
|
178
|
|
|
{ |
|
179
|
|
|
$columns = ['id' => 'integer', 'name' => 'string']; |
|
180
|
|
|
$sql = $this->resource->getCreateTableSql('tmp_table1', $columns); |
|
181
|
|
|
|
|
182
|
|
|
$platform = new MySqlPlatform(); |
|
183
|
|
|
$schema = new Schema(); |
|
184
|
|
|
$table = $schema->createTable('tmp_table1'); |
|
185
|
|
|
foreach ($columns as $column => $type) { |
|
186
|
|
|
$table->addColumn($column, $type); |
|
187
|
|
|
} |
|
188
|
|
|
$table->addOption('temporary', true); |
|
|
|
|
|
|
189
|
|
|
$expected = $platform->getCreateTableSQL($table); |
|
190
|
|
|
|
|
191
|
|
|
$this->assertEquals($expected, $sql); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: