Skip to content

Commit f0880dc

Browse files
author
Alan Pinstein
committed
Re-factor tests to be much cleaner and rely on internal migration audit trail.
1 parent 751c0c0 commit f0880dc

14 files changed

Lines changed: 107 additions & 198 deletions

Migrator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ class Migrator
271271
* @var array An array of all migrations installed for this app.
272272
*/
273273
protected $migrationList = array();
274+
/**
275+
* @var array An audit trail of the migrations applied during this invocation.
276+
*/
277+
protected $migrationAuditTrail = array();
274278

275279
/**
276280
* Create a migrator instance.
@@ -360,6 +364,11 @@ public function __construct($opts = array())
360364
}
361365
}
362366

367+
public function getMigrationAuditTrail()
368+
{
369+
return $this->migrationAuditTrail;
370+
}
371+
363372
protected function initializeMigrationsDir()
364373
{
365374
// initialize migrations dir
@@ -686,6 +695,8 @@ public function listMigrations()
686695
*/
687696
public function runMigration($migrationName, $direction)
688697
{
698+
$this->migrationAuditTrail[] = "{$migrationName}:{$direction}";
699+
689700
if ($direction === Migrator::DIRECTION_UP)
690701
{
691702
$info = array(

test/MigratorTest.php

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,33 @@
22

33
require_once 'Migrator.php';
44

5-
define('TEST_MIGRATIONS_DIR', './test/migrations');
6-
7-
$testMigrationNumber = 0;
8-
$testMigrationIncrementedNumber = 0;
9-
$testMigrationHasRunCounter = 0;
10-
115
class MigratorTest extends PHPUnit_Framework_TestCase
126
{
137
protected $migrator;
148

159
function setup()
1610
{
1711
$opts = array(
18-
Migrator::OPT_MIGRATIONS_DIR => TEST_MIGRATIONS_DIR,
19-
Migrator::OPT_QUIET => true,
12+
Migrator::OPT_MIGRATIONS_DIR => __DIR__ . '/migrations',
13+
Migrator::OPT_QUIET => true,
2014
);
2115
$this->migrator = new Migrator($opts);
2216
$this->migrator->getVersionProvider()->setVersion($this->migrator, 0); // hard-reset to version 0
23-
24-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
25-
$testMigrationNumber = 0;
26-
$testMigrationIncrementedNumber = 0;
2717
}
2818

2919
function testFreshMigrationsStartAtVersionZero()
3020
{
3121
$this->assertEquals(Migrator::VERSION_ZERO, $this->migrator->getVersion());
3222
}
3323

34-
private function assertAtVersion($version, $counter, $numMigrationsRun = NULL)
24+
private function assertAtVersion($version)
25+
{
26+
$this->assertEquals($version, $this->migrator->getVersion(), "At wrong version.");
27+
}
28+
29+
private function assertAuditTrail($expectedAuditTrail)
3530
{
36-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
37-
$this->assertEquals($version, $this->migrator->getVersion(), "At wrong version #");
38-
$this->assertEquals($counter, $testMigrationNumber, "testMigrationNumber wrong");
39-
$this->assertEquals($counter, $testMigrationIncrementedNumber, "testMigrationIncrementedNumber wrong");
40-
if ($numMigrationsRun !== NULL)
41-
{
42-
$this->assertEquals($numMigrationsRun, $testMigrationHasRunCounter);
43-
}
31+
$this->assertEquals($expectedAuditTrail, $this->migrator->getMigrationAuditTrail());
4432
}
4533

4634
function testLatestVersion()
@@ -51,89 +39,119 @@ function testLatestVersion()
5139
function testCleanGoesToVersionZero()
5240
{
5341
$this->migrator->clean();
54-
$this->assertAtVersion(Migrator::VERSION_ZERO, 0, 0);
42+
$this->assertAtVersion(Migrator::VERSION_ZERO);
43+
$this->assertAuditTrail(array());
5544
}
5645

5746
function testMigratingToVersionZero()
5847
{
59-
$this->migrator->migrateToVersion(Migrator::VERSION_HEAD);
48+
$this->migrator->getVersionProvider()->setVersion($this->migrator, '20090719_000005');
6049
$this->migrator->migrateToVersion(Migrator::VERSION_ZERO);
61-
$this->assertAtVersion(Migrator::VERSION_ZERO, 0, 10);
50+
$this->assertAtVersion(Migrator::VERSION_ZERO);
51+
$this->assertAuditTrail(array(
52+
'20090719_000005:down',
53+
'20090719_000003:down',
54+
'20090719_000004:down',
55+
'20090719_000002:down',
56+
'20090719_000001:down',
57+
));
6258
}
6359

6460
function testMigratingToHead()
6561
{
6662
$this->migrator->migrateToVersion(Migrator::VERSION_HEAD);
67-
$this->assertAtVersion('20090719_000005', 5, 5);
63+
$this->assertAtVersion('20090719_000005');
64+
$this->assertAuditTrail(array(
65+
'20090719_000001:up',
66+
'20090719_000002:up',
67+
'20090719_000004:up',
68+
'20090719_000003:up',
69+
'20090719_000005:up',
70+
));
6871
}
6972

7073
function testMigrateUp()
7174
{
7275
// mock out migrator; make sure UP calls migrate to appropriate version
73-
$this->migrator->migrateToVersion('20090719_000002');
74-
// $mock = $this->getMock($this->migrator);
75-
// $mock->expects($this->once())
76-
// ->method('migrateToVersion')
77-
// ->with($this->equalTo('20090719_000003'));
78-
// $this->migrator->migrateToVersion(Migrator::VERSION_UP);
76+
$this->migrator->getVersionProvider()->setVersion($this->migrator, '20090719_000001');
7977

80-
global $testMigrationIncrementedNumber;
81-
$expectedMigrationIncrementedNumber = $testMigrationIncrementedNumber + 1;
82-
$this->migrator->migrateToVersion(Migrator::VERSION_UP);
83-
$this->assertAtVersion('20090719_000003', 3, $expectedMigrationIncrementedNumber);
78+
$this->migrator->migrateToVersion(Migrator::VERSION_UP);
79+
$this->assertAtVersion('20090719_000002');
80+
$this->assertAuditTrail(array(
81+
'20090719_000002:up'
82+
));
8483
}
8584

8685
function testMigrateDown()
8786
{
88-
// mock out migrator; make sure UP calls migrate to appropriate version
89-
$this->migrator->migrateToVersion('20090719_000002');
90-
// $mock = $this->getMock($this->migrator);
91-
// $mock->expects($this->once())
92-
// ->method('migrateToVersion')
93-
// ->with($this->equalTo('20090719_000001'));
94-
// $this->migrator->migrateToVersion(Migrator::VERSION_DOWN);
95-
96-
global $testMigrationIncrementedNumber;
97-
$expectedMigrationIncrementedNumber = $testMigrationIncrementedNumber + 1;
87+
$this->migrator->getVersionProvider()->setVersion($this->migrator, '20090719_000002');
9888
$this->migrator->migrateToVersion(Migrator::VERSION_DOWN);
99-
$this->assertAtVersion('20090719_000001', 1, $expectedMigrationIncrementedNumber);
89+
$this->assertAtVersion('20090719_000001');
90+
$this->assertAuditTrail(array(
91+
'20090719_000002:down'
92+
));
10093
}
10194

10295
function testMigratingToCurrentVersionRunsNoMigrations()
10396
{
97+
$this->migrator->getVersionProvider()->setVersion($this->migrator, '20090719_000002');
10498
$this->migrator->migrateToVersion('20090719_000002');
105-
global $testMigrationIncrementedNumber;
106-
$this->migrator->migrateToVersion('20090719_000002');
107-
$this->assertAtVersion('20090719_000002', 2, $testMigrationIncrementedNumber);
99+
$this->assertAtVersion('20090719_000002');
100+
$this->assertAuditTrail(array());
108101
}
109102

110103
function testMigrateToVersion1()
111104
{
112105
$this->migrator->migrateToVersion('20090719_000001');
113-
$this->assertAtVersion('20090719_000001', 1, 1);
106+
$this->assertAtVersion('20090719_000001');
107+
$this->assertAuditTrail(array(
108+
'20090719_000001:up',
109+
));
114110
}
115111

116112
function testMigrateToVersion2()
117113
{
118114
$this->migrator->migrateToVersion('20090719_000002');
119-
$this->assertAtVersion('20090719_000002', 2, 2);
115+
$this->assertAtVersion('20090719_000002');
116+
$this->assertAuditTrail(array(
117+
'20090719_000001:up',
118+
'20090719_000002:up',
119+
));
120120
}
121121

122122
function testMigrateToVersion3()
123123
{
124-
$this->migrator->migrateToVersion('20090719_000003');
125-
$this->assertAtVersion('20090719_000003', 3, 3);
124+
$this->migrator->migrateToVersion('20090719_000004');
125+
$this->assertAtVersion('20090719_000004');
126+
$this->assertAuditTrail(array(
127+
'20090719_000001:up',
128+
'20090719_000002:up',
129+
'20090719_000004:up',
130+
));
126131
}
127132

128133
function testMigrateToVersion4()
129134
{
130-
$this->migrator->migrateToVersion('20090719_000004');
131-
$this->assertAtVersion('20090719_000004', 4, 4);
135+
$this->migrator->migrateToVersion('20090719_000003');
136+
$this->assertAtVersion('20090719_000003');
137+
$this->assertAuditTrail(array(
138+
'20090719_000001:up',
139+
'20090719_000002:up',
140+
'20090719_000004:up',
141+
'20090719_000003:up',
142+
));
132143
}
133144

134145
function testMigrateToVersion5()
135146
{
136147
$this->migrator->migrateToVersion('20090719_000005');
137-
$this->assertAtVersion('20090719_000005', 5, 5);
148+
$this->assertAtVersion('20090719_000005');
149+
$this->assertAuditTrail(array(
150+
'20090719_000001:up',
151+
'20090719_000002:up',
152+
'20090719_000004:up',
153+
'20090719_000003:up',
154+
'20090719_000005:up',
155+
));
138156
}
139157
}

test/migrations-without-manifest/20090719_000001.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
<?php
22
class Migration20090719_000001 extends Migration
33
{
4-
public function up()
5-
{
6-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
7-
$testMigrationHasRunCounter++;
8-
$testMigrationNumber = 1;
9-
$testMigrationIncrementedNumber++;
10-
}
11-
public function down()
12-
{
13-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
14-
$testMigrationHasRunCounter++;
15-
$testMigrationNumber = 0;
16-
$testMigrationIncrementedNumber--;
17-
}
4+
public function up() {}
5+
public function down() {}
186
public function description()
197
{
208
return "Version 1";

test/migrations-without-manifest/20090719_000002.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
<?php
22
class Migration20090719_000002 extends Migration
33
{
4-
public function up()
5-
{
6-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
7-
$testMigrationHasRunCounter++;
8-
$testMigrationNumber = 2;
9-
$testMigrationIncrementedNumber++;
10-
}
11-
public function down()
12-
{
13-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
14-
$testMigrationHasRunCounter++;
15-
$testMigrationNumber = 1;
16-
$testMigrationIncrementedNumber--;
17-
}
4+
public function up() {}
5+
public function down() {}
186
public function description()
197
{
208
return "Version 2";

test/migrations-without-manifest/20090719_000003.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
<?php
22
class Migration20090719_000003 extends Migration
33
{
4-
public function up()
5-
{
6-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
7-
$testMigrationHasRunCounter++;
8-
$testMigrationNumber = 3;
9-
$testMigrationIncrementedNumber++;
10-
}
11-
public function down()
12-
{
13-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
14-
$testMigrationHasRunCounter++;
15-
$testMigrationNumber = 2;
16-
$testMigrationIncrementedNumber--;
17-
}
4+
public function up() {}
5+
public function down() {}
186
public function description()
197
{
208
return "Version 3";

test/migrations-without-manifest/20090719_000004.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
<?php
22
class Migration20090719_000004 extends Migration
33
{
4-
public function up()
5-
{
6-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
7-
$testMigrationHasRunCounter++;
8-
$testMigrationNumber = 4;
9-
$testMigrationIncrementedNumber++;
10-
}
11-
public function down()
12-
{
13-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
14-
$testMigrationHasRunCounter++;
15-
$testMigrationNumber = 3;
16-
$testMigrationIncrementedNumber--;
17-
}
4+
public function up() {}
5+
public function down() {}
186
public function description()
197
{
208
return "Version 4";

test/migrations-without-manifest/20090719_000005.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
<?php
22
class Migration20090719_000005 extends Migration
33
{
4-
public function up()
5-
{
6-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
7-
$testMigrationHasRunCounter++;
8-
$testMigrationNumber = 5;
9-
$testMigrationIncrementedNumber++;
10-
}
11-
public function down()
12-
{
13-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
14-
$testMigrationHasRunCounter++;
15-
$testMigrationNumber = 4;
16-
$testMigrationIncrementedNumber--;
17-
}
4+
public function up() {}
5+
public function down() {}
186
public function description()
197
{
208
return "Version 5";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0
1+
20090719_000005

test/migrations/20090719_000001.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
<?php
22
class Migration20090719_000001 extends Migration
33
{
4-
public function up()
5-
{
6-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
7-
$testMigrationHasRunCounter++;
8-
$testMigrationNumber = 1;
9-
$testMigrationIncrementedNumber++;
10-
}
11-
public function down()
12-
{
13-
global $testMigrationNumber, $testMigrationIncrementedNumber, $testMigrationHasRunCounter;
14-
$testMigrationHasRunCounter++;
15-
$testMigrationNumber = 0;
16-
$testMigrationIncrementedNumber--;
17-
}
4+
public function up() {}
5+
public function down() {}
186
public function description()
197
{
208
return "Version 1";

0 commit comments

Comments
 (0)