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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
<?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()
{
$this->before = isset($GLOBALS['CACHING_ENABLE'])
? $GLOBALS['CACHING_ENABLE']
: null;
$GLOBALS['CACHING_ENABLE'] = false;
require_once 'lib/classes/StudipCache.class.php';
require_once 'lib/classes/StudipMemoryCache.class.php';
require_once 'lib/classes/StudipCacheFactory.class.php';
require_once 'lib/models/SimpleORMap.class.php';
require_once 'lib/migrations/Migration.php';
require_once 'lib/migrations/Migrator.php';
require_once 'lib/migrations/SchemaVersion.php';
}
public function tearDown()
{
if ($this->before !== null) {
$GLOBALS['CACHING_ENABLE'] = $this->before;
} else {
unset($GLOBALS['CACHING_ENABLE']);
}
}
private function getSchemaVersion()
{
return new class() implements SchemaVersion
{
private $versions = [];
public function get()
{
return count($this->versions) > 0 ? max($this->versions) : 0;
}
public function contains($version)
{
return in_array($version, $this->versions);
}
public function add($version)
{
if (!$this->contains($version)) {
$this->versions[] = $version;
}
}
public function remove($version)
{
if ($this->contains($version)) {
$this->versions = array_diff($this->versions, [$version]);
}
}
};
}
private function getMigrator($schema_version = null)
{
return new Migrator(
__DIR__ . '/test-migrations',
$schema_version ?: $this->getSchemaVersion()
);
}
public function testSchemaVersion()
{
$schema_version = $this->getSchemaVersion();
$this->assertSame(0, $schema_version->get());
$schema_version->add(1);
$this->assertTrue($schema_version->contains(1));
$this->assertSame(1, $schema_version->get());
$schema_version->add(2);
$this->assertTrue($schema_version->contains(2));
$this->assertSame(2, $schema_version->get());
$schema_version->remove(1);
$this->assertFalse($schema_version->contains(1));
$this->assertSame(2, $schema_version->get());
}
public function testRelevance()
{
$migrator = $this->getMigrator();
$relevant = $migrator->relevantMigrations(null);
$this->assertSame(4, count($relevant));
$migrator->migrateTo(10);
$relevant = $migrator->relevantMigrations(null);
$this->assertSame(1, count($relevant));
}
public function testMigrationUp()
{
$schema_version = $this->getSchemaVersion();
$migrator = $this->getMigrator($schema_version);
$migrator->migrateTo(null);
$this->assertSame(20190417, $schema_version->get());
$this->assertSame(0, count($migrator->relevantMigrations(null)));
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, count($migrator->relevantMigrations(null)));
}
public function testGaps()
{
$schema_version = $this->getSchemaVersion();
$schema_version->add(2);
$schema_version->add(10);
$migrator = $this->getMigrator($schema_version);
$relevant = $migrator->relevantMigrations(null);
$this->assertSame(2, count($relevant));
$this->assertEquals([1, 20190417], array_keys($relevant));
}
}
|