22
33require_once 'Migrator.php ' ;
44
5- define ('TEST_MIGRATIONS_DIR ' , './test/migrations ' );
6-
7- $ testMigrationNumber = 0 ;
8- $ testMigrationIncrementedNumber = 0 ;
9- $ testMigrationHasRunCounter = 0 ;
10-
115class 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}
0 commit comments