1
|
|
|
<?php |
2
|
|
|
namespace Test; |
3
|
|
|
|
4
|
|
|
use ByJG\DbMigration\Exception\InvalidMigrationFile; |
5
|
|
|
use ByJG\DbMigration\Migration; |
6
|
|
|
use ByJG\Util\Uri; |
7
|
|
|
|
8
|
|
|
class MigrationTest extends \PHPUnit\Framework\TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Migration |
12
|
|
|
*/ |
13
|
|
|
protected $object; |
14
|
|
|
|
15
|
|
|
public function setUp(): void |
16
|
|
|
{ |
17
|
|
|
$this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/dirstructure'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function tearDown(): void |
21
|
|
|
{ |
22
|
|
|
$this->object = null; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testGetBaseSql() |
26
|
|
|
{ |
27
|
|
|
$base = $this->object->getBaseSql(); |
28
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/base.sql', $base); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testGetBaseSqlNotFound() |
32
|
|
|
{ |
33
|
|
|
$this->expectException(InvalidMigrationFile::class); |
34
|
|
|
$this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/invalid'); |
35
|
|
|
$this->object->getBaseSql(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetBaseSqlNotFoundAndNotRequired() |
39
|
|
|
{ |
40
|
|
|
$this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/invalid', false); |
41
|
|
|
$this->object->getBaseSql(); |
42
|
|
|
$this->assertTrue(true); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testGetMigrationSql1() |
46
|
|
|
{ |
47
|
|
|
$version = $this->object->getMigrationSql(1, 1); |
48
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00001.sql', $version); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testGetMigrationSql2() |
52
|
|
|
{ |
53
|
|
|
$version = $this->object->getMigrationSql(2, 1); |
54
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00002.sql', $version); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testGetMigrationSql3() |
58
|
|
|
{ |
59
|
|
|
$version = $this->object->getMigrationSql(12, 1); |
60
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00012-dev.sql', $version); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetMigrationSql4() |
64
|
|
|
{ |
65
|
|
|
$this->expectException(InvalidMigrationFile::class); |
66
|
|
|
$this->expectExceptionMessage("version number '13'"); |
67
|
|
|
$this->object->getMigrationSql(13, 1); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGetMigrationSqlDown1() |
71
|
|
|
{ |
72
|
|
|
$version = $this->object->getMigrationSql(1, -1); |
73
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00001.sql', $version); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testGetMigrationSqlDown2() |
77
|
|
|
{ |
78
|
|
|
$version = $this->object->getMigrationSql(2, -1); |
79
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00002.sql', $version); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testGetMigrationSqlDown3() |
83
|
|
|
{ |
84
|
|
|
$version = $this->object->getMigrationSql(12, -1); |
85
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00012-dev.sql', $version); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testGetMigrationSqlDown4() |
89
|
|
|
{ |
90
|
|
|
$this->expectException(InvalidMigrationFile::class); |
91
|
|
|
$this->expectExceptionMessage("version number '13'"); |
92
|
|
|
$this->object->getMigrationSql(13, -1); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testGetFileContent_NonExists() |
96
|
|
|
{ |
97
|
|
|
$this->assertEquals( |
98
|
|
|
[ |
99
|
|
|
"file" => "non-existent", |
100
|
|
|
"description" => "no description provided. Pro tip: use `-- @description:` to define one.", |
101
|
|
|
"exists" => false, |
102
|
|
|
"checksum" => null, |
103
|
|
|
"content" => null, |
104
|
|
|
], |
105
|
|
|
$this->object->getFileContent("non-existent") |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testGetFileContent_1() |
110
|
|
|
{ |
111
|
|
|
$this->assertEquals( |
112
|
|
|
[ |
113
|
|
|
"file" => __DIR__ . '/dirstructure/migrations/up/00001.sql', |
114
|
|
|
"description" => "this is a test", |
115
|
|
|
"exists" => true, |
116
|
|
|
"checksum" => "b937afa57e363c9244fa30844dd11d312694f697", |
117
|
|
|
"content" => "-- @description: this is a test\n\nselect * from mysql.users;\n", |
118
|
|
|
], |
119
|
|
|
$this->object->getFileContent(__DIR__ . '/dirstructure/migrations/up/00001.sql') |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testGetFileContent_2() |
124
|
|
|
{ |
125
|
|
|
$this->assertEquals( |
126
|
|
|
[ |
127
|
|
|
"file" => __DIR__ . '/dirstructure/migrations/up/00002.sql', |
128
|
|
|
"description" => "another test", |
129
|
|
|
"exists" => true, |
130
|
|
|
"checksum" => "fd8ab8176291c2dcbf0d91564405e0f98f0cd77e", |
131
|
|
|
"content" => "-- @description: another test\n\nselect * from dual;", |
132
|
|
|
], |
133
|
|
|
$this->object->getFileContent(__DIR__ . '/dirstructure/migrations/up/00002.sql') |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testGetFileContent_3() |
138
|
|
|
{ |
139
|
|
|
$this->assertEquals( |
140
|
|
|
[ |
141
|
|
|
"file" => __DIR__ . '/dirstructure/migrations/up/00003.sql', |
142
|
|
|
"description" => "no description provided. Pro tip: use `-- @description:` to define one.", |
143
|
|
|
"exists" => true, |
144
|
|
|
"checksum" => "73faaa68e2f60c11e75a9ccc18528e0ffa15127a", |
145
|
|
|
"content" => "select something from sometable;", |
146
|
|
|
], |
147
|
|
|
$this->object->getFileContent(__DIR__ . '/dirstructure/migrations/up/00003.sql') |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|