blob: 4a45e71598c2396b1275c8fc3e499b2e1f20dc23 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
<?php
/**
* MigrationTest.php - unit tests for the migrations
*
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @license GPL2 or any later version
*/
class MigrationTest extends \Codeception\Test\Unit
{
protected $migrator;
protected $before = null;
public function setUp(): void
{
$this->before = $GLOBALS['CACHING_ENABLE'] ?? null;
$GLOBALS['CACHING_ENABLE'] = false;
require_once 'lib/classes/SimpleORMap.class.php';
require_once 'lib/classes/StudipCache.class.php';
require_once 'lib/classes/StudipMemoryCache.class.php';
require_once 'lib/classes/StudipCacheFactory.class.php';
require_once 'lib/migrations/Migration.php';
require_once 'lib/migrations/Migrator.php';
require_once 'lib/migrations/SchemaVersion.php';
}
public function tearDown(): void
{
if ($this->before !== null) {
$GLOBALS['CACHING_ENABLE'] = $this->before;
} else {
unset($GLOBALS['CACHING_ENABLE']);
}
}
private function getSchemaVersion()
{
return new class() implements SchemaVersion
{
private $versions = [0];
public function getDomain()
{
return 'test';
}
public function getBranch()
{
return 0;
}
public function getAllBranches()
{
return array_keys($this->versions);
}
public function get($branch = 0)
{
return $this->versions[$branch] ?? 0;
}
public function set($version, $branch = 0)
{
$this->versions[$branch] = (int) $version;
}
};
}
private function getMigrator($schema_version = null)
{
return new Migrator(
TEST_FIXTURES_PATH . 'migrations',
$schema_version ?: $this->getSchemaVersion()
);
}
public function testRelevance()
{
$migrator = $this->getMigrator();
$relevant = $migrator->relevantMigrations(null);
$this->assertSame(4, count($relevant));
$migrator->migrateTo(2);
$relevant = $migrator->relevantMigrations(null);
$this->assertSame(2, count($relevant));
}
public function testMigrationUp()
{
$schema_version = $this->getSchemaVersion();
$migrator = $this->getMigrator($schema_version);
$migrator->migrateTo(null);
$this->assertSame(10, $schema_version->get());
$this->assertSame(0, $migrator->pendingMigrations());
return $schema_version;
}
/**
* @depends testMigrationUp
*/
public function testMigrationDown($schema_version)
{
$migrator = $this->getMigrator($schema_version);
$migrator->migrateTo(0);
$this->assertSame(0, $schema_version->get());
$this->assertSame(4, $migrator->pendingMigrations());
}
public function testGaps()
{
$schema_version = $this->getSchemaVersion();
$schema_version->set(10);
$migrator = $this->getMigrator($schema_version);
$relevant = $migrator->relevantMigrations(null);
$this->assertSame(1, count($relevant));
$this->assertEquals(['2.1'], array_keys($relevant));
}
}
|