1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace Test; |
3
|
|
|
|
4
|
|
|
use ByJG\AnyDataset\Store\PdoMysql; |
5
|
|
|
use ByJG\DbMigration\Migration; |
6
|
|
|
use ByJG\Util\Uri; |
7
|
|
|
|
8
|
|
|
// backward compatibility |
9
|
|
|
if (!class_exists('\PHPUnit\Framework\TestCase')) { |
10
|
|
|
class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase'); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
class MigrationTest extends \PHPUnit\Framework\TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Migration |
17
|
|
|
*/ |
18
|
|
|
protected $object; |
19
|
|
|
|
20
|
|
|
public function setUp() |
21
|
|
|
{ |
22
|
|
|
$this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/dirstructure'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function tearDown() |
26
|
|
|
{ |
27
|
|
|
$this->object = null; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testGetBaseSql() |
31
|
|
|
{ |
32
|
|
|
$base = $this->object->getBaseSql(); |
33
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/base.sql', $base); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile |
38
|
|
|
*/ |
39
|
|
|
public function testGetBaseSqlNotFound() |
40
|
|
|
{ |
41
|
|
|
$this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/invalid'); |
42
|
|
|
$this->object->getBaseSql(); |
|
|
|
|
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
|
|
|
/** |
64
|
|
|
* @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile |
65
|
|
|
* @expectedExceptionMessage version number '13' |
66
|
|
|
*/ |
67
|
|
|
public function testGetMigrationSql4() |
68
|
|
|
{ |
69
|
|
|
$this->object->getMigrationSql(13, 1); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetMigrationSqlDown1() |
73
|
|
|
{ |
74
|
|
|
$version = $this->object->getMigrationSql(1, -1); |
75
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00001.sql', $version); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGetMigrationSqlDown2() |
79
|
|
|
{ |
80
|
|
|
$version = $this->object->getMigrationSql(2, -1); |
81
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00002.sql', $version); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGetMigrationSqlDown3() |
85
|
|
|
{ |
86
|
|
|
$version = $this->object->getMigrationSql(12, -1); |
87
|
|
|
$this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00012-dev.sql', $version); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile |
92
|
|
|
* @expectedExceptionMessage version number '13' |
93
|
|
|
*/ |
94
|
|
|
public function testGetMigrationSqlDown4() |
95
|
|
|
{ |
96
|
|
|
$this->object->getMigrationSql(13, -1); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.