1
|
|
|
<?php |
2
|
|
|
namespace FwlibTest\Db; |
3
|
|
|
|
4
|
|
|
use Fwlib\Db\SyncDbSchema; |
5
|
|
|
use Fwlib\Test\AbstractDbRelateTest; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @copyright Copyright 2013-2015 Fwolf |
9
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
10
|
|
|
*/ |
11
|
|
|
class SyncDbSchemaTest extends AbstractDbRelateTest |
12
|
|
|
{ |
13
|
|
|
protected static $dbUsing = 'default'; |
14
|
|
|
private static $logTable = 'log_sync_db_schema'; |
15
|
|
|
|
16
|
|
|
/** @var SyncDbSchema */ |
17
|
|
|
private static $sds = null; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
public static function setUpBeforeClass() |
21
|
|
|
{ |
22
|
|
|
parent::setUpBeforeClass(); |
23
|
|
|
|
24
|
|
|
// Delete log table if exists |
25
|
|
|
if (self::$db->isTableExist(self::$logTable)) { |
26
|
|
|
self::$db->execute('DROP TABLE ' . self::$logTable); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
public function testConstruct() |
32
|
|
|
{ |
33
|
|
|
$this->assertFalse(self::$db->isTableExist(self::$logTable)); |
34
|
|
|
|
35
|
|
|
$this->expectOutputRegex( |
36
|
|
|
'/Log table \w+ does not exists, create it, done\./' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
self::$sds = (new SyncDbSchema) |
40
|
|
|
->setLogTable(self::$logTable) |
41
|
|
|
->setDb($this->getServiceContainer()->getDb()); |
42
|
|
|
|
43
|
|
|
$this->assertEquals(self::$logTable, self::$sds->getLogTable()); |
44
|
|
|
$this->assertTrue(self::$db->isTableExist(self::$logTable)); |
45
|
|
|
|
46
|
|
|
self::$sds->charsetPhp = 'UTF-8'; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function testConstruct2() |
51
|
|
|
{ |
52
|
|
|
$this->expectOutputRegex('/Log table \w+ already exists\./'); |
53
|
|
|
$sds = (new SyncDbSchema) |
54
|
|
|
->setLogTable(self::$logTable) |
55
|
|
|
->setDb($this->getServiceContainer()->getDb()); |
56
|
|
|
unset($sds); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* 1 |
62
|
|
|
*/ |
63
|
|
|
public function testExecuteFirstSql() |
64
|
|
|
{ |
65
|
|
|
$arColumn = self::$db->getMetaColumn(self::$tableUser); |
66
|
|
|
$this->assertFalse(isset($arColumn['temp1'])); |
67
|
|
|
|
68
|
|
|
$this->expectOutputRegex('/Total \d+ SQL executed successful./'); |
69
|
|
|
self::$sds->setSql( |
70
|
|
|
42, |
71
|
|
|
'ALTER TABLE ' . self::$tableUser . ' |
72
|
|
|
ADD COLUMN temp1 INT NOT NULL DEFAULT 0' |
73
|
|
|
); |
74
|
|
|
self::$sds->execute(); |
75
|
|
|
|
76
|
|
|
$arColumn = self::$db->getMetaColumn(self::$tableUser, true); |
77
|
|
|
$this->assertTrue(isset($arColumn['temp1'])); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* 2 No more SQL |
83
|
|
|
*/ |
84
|
|
|
public function testExecuteNoUnDoneSql() |
85
|
|
|
{ |
86
|
|
|
$this->expectOutputRegex('/No un-done SQL to do./'); |
87
|
|
|
self::$sds->execute(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* 3 Add an error SQL |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function testExecuteErrorSql() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$this->expectOutputRegex('/Execute abort./'); |
97
|
|
|
self::$sds->setSql( |
98
|
|
|
43, |
99
|
|
|
'ALTER TABLE ' . self::$tableUser . ' |
100
|
|
|
ADD COLUMN temp1 INT NOT NULL DEFAULT 0' |
101
|
|
|
); |
102
|
|
|
self::$sds->execute(); |
103
|
|
|
|
104
|
|
|
$this->assertEquals(43, self::$sds->lastId); |
105
|
|
|
$this->assertEquals(42, self::$sds->lastIdDone); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* 4 Add SQL with smaller id, will not execute |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
public function testExecuteSmallId() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$this->expectOutputRegex('/No un-done SQL to do./'); |
115
|
|
|
self::$sds->setSql( |
116
|
|
|
22, |
117
|
|
|
'ALTER TABLE ' . self::$tableUser . ' |
118
|
|
|
ADD COLUMN temp2 INT NOT NULL DEFAULT 0' |
119
|
|
|
); |
120
|
|
|
self::$sds->execute(); |
121
|
|
|
|
122
|
|
|
// Error SQL 43 is cleared by execute() |
123
|
|
|
$this->assertEquals(42, self::$sds->getLastId()); |
124
|
|
|
$this->assertEquals(42, self::$sds->getLastIdDone()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.