|
1 <?php |
|
2 |
|
3 namespace Doctrine\DBAL\Migrations\Tests\Functional; |
|
4 |
|
5 use Doctrine\DBAL\DriverManager; |
|
6 use Doctrine\DBAL\Schema\Schema; |
|
7 use Doctrine\DBAL\Migrations\Configuration\Configuration; |
|
8 |
|
9 class FunctionalTest extends \Doctrine\DBAL\Migrations\Tests\MigrationTestCase |
|
10 { |
|
11 /** |
|
12 * @var Configuration |
|
13 */ |
|
14 private $config; |
|
15 |
|
16 /** |
|
17 * @var Connection |
|
18 */ |
|
19 private $connection; |
|
20 |
|
21 public function setUp() |
|
22 { |
|
23 $this->connection = $this->getSqliteConnection(); |
|
24 $this->config = new Configuration($this->connection); |
|
25 $this->config->setMigrationsNamespace('Doctrine\DBAL\Migrations\Tests\Functional'); |
|
26 $this->config->setMigrationsDirectory('.'); |
|
27 } |
|
28 |
|
29 public function testMigrateUp() |
|
30 { |
|
31 $version = new \Doctrine\DBAL\Migrations\Version($this->config, 1, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateUp'); |
|
32 |
|
33 $this->assertFalse($this->config->hasVersionMigrated($version)); |
|
34 $version->execute('up'); |
|
35 |
|
36 $schema = $this->connection->getSchemaManager()->createSchema(); |
|
37 $this->assertTrue($schema->hasTable('foo')); |
|
38 $this->assertTrue($schema->getTable('foo')->hasColumn('id')); |
|
39 $this->assertTrue($this->config->hasVersionMigrated($version)); |
|
40 } |
|
41 |
|
42 public function testMigrateDown() |
|
43 { |
|
44 $version = new \Doctrine\DBAL\Migrations\Version($this->config, 1, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateUp'); |
|
45 |
|
46 $this->assertFalse($this->config->hasVersionMigrated($version)); |
|
47 $version->execute('up'); |
|
48 |
|
49 $schema = $this->connection->getSchemaManager()->createSchema(); |
|
50 $this->assertTrue($schema->hasTable('foo')); |
|
51 $this->assertTrue($schema->getTable('foo')->hasColumn('id')); |
|
52 $this->assertTrue($this->config->hasVersionMigrated($version)); |
|
53 |
|
54 $version->execute('down'); |
|
55 $schema = $this->connection->getSchemaManager()->createSchema(); |
|
56 $this->assertFalse($schema->hasTable('foo')); |
|
57 $this->assertFalse($this->config->hasVersionMigrated($version)); |
|
58 |
|
59 } |
|
60 |
|
61 public function testSkipMigrateUp() |
|
62 { |
|
63 $version = new \Doctrine\DBAL\Migrations\Version($this->config, 1, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationSkipMigration'); |
|
64 |
|
65 $this->assertFalse($this->config->hasVersionMigrated($version)); |
|
66 $version->execute('up'); |
|
67 |
|
68 $schema = $this->connection->getSchemaManager()->createSchema(); |
|
69 $this->assertFalse($schema->hasTable('foo')); |
|
70 |
|
71 $this->assertTrue($this->config->hasVersionMigrated($version)); |
|
72 } |
|
73 |
|
74 public function testMigrateSeveralSteps() |
|
75 { |
|
76 $this->config->registerMigration(1, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateUp'); |
|
77 $this->config->registerMigration(2, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationSkipMigration'); |
|
78 $this->config->registerMigration(3, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateFurther'); |
|
79 |
|
80 $this->assertEquals(0, $this->config->getCurrentVersion()); |
|
81 $migrations = $this->config->getMigrationsToExecute('up', 3); |
|
82 |
|
83 $this->assertEquals(3, count($migrations)); |
|
84 $this->assertInstanceOf('Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateUp', $migrations[1]->getMigration()); |
|
85 $this->assertInstanceOf('Doctrine\DBAL\Migrations\Tests\Functional\MigrationSkipMigration', $migrations[2]->getMigration()); |
|
86 $this->assertInstanceOf('Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateFurther', $migrations[3]->getMigration()); |
|
87 |
|
88 $migration = new \Doctrine\DBAL\Migrations\Migration($this->config); |
|
89 $migration->migrate(3); |
|
90 |
|
91 $schema = $this->config->getConnection()->getSchemaManager()->createSchema(); |
|
92 $this->assertTrue($schema->hasTable('foo')); |
|
93 $this->assertTrue($schema->hasTable('bar')); |
|
94 |
|
95 $this->assertEquals(3, $this->config->getCurrentVersion()); |
|
96 $this->assertTrue($migrations[1]->isMigrated()); |
|
97 $this->assertTrue($migrations[2]->isMigrated()); |
|
98 $this->assertTrue($migrations[3]->isMigrated()); |
|
99 } |
|
100 |
|
101 public function testMigrateToLastVersion() |
|
102 { |
|
103 $this->config->registerMigration(1, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateUp'); |
|
104 $this->config->registerMigration(2, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationSkipMigration'); |
|
105 $this->config->registerMigration(3, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateFurther'); |
|
106 |
|
107 $migration = new \Doctrine\DBAL\Migrations\Migration($this->config); |
|
108 $migration->migrate(); |
|
109 |
|
110 $this->assertEquals(3, $this->config->getCurrentVersion()); |
|
111 $migrations = $this->config->getMigrations(); |
|
112 $this->assertTrue($migrations[1]->isMigrated()); |
|
113 $this->assertTrue($migrations[2]->isMigrated()); |
|
114 $this->assertTrue($migrations[3]->isMigrated()); |
|
115 } |
|
116 |
|
117 public function testDryRunMigration() |
|
118 { |
|
119 $this->config->registerMigration(1, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateUp'); |
|
120 $this->config->registerMigration(2, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationSkipMigration'); |
|
121 $this->config->registerMigration(3, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateFurther'); |
|
122 |
|
123 $migration = new \Doctrine\DBAL\Migrations\Migration($this->config); |
|
124 $migration->migrate(3, true); |
|
125 |
|
126 $schema = $this->config->getConnection()->getSchemaManager()->createSchema(); |
|
127 $this->assertFalse($schema->hasTable('foo')); |
|
128 $this->assertFalse($schema->hasTable('bar')); |
|
129 |
|
130 $this->assertEquals(0, $this->config->getCurrentVersion()); |
|
131 $migrations = $this->config->getMigrations(); |
|
132 $this->assertFalse($migrations[1]->isMigrated()); |
|
133 $this->assertFalse($migrations[2]->isMigrated()); |
|
134 $this->assertFalse($migrations[3]->isMigrated()); |
|
135 } |
|
136 |
|
137 public function testMigrateDownSeveralSteps() |
|
138 { |
|
139 $this->config->registerMigration(1, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateUp'); |
|
140 $this->config->registerMigration(2, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationSkipMigration'); |
|
141 $this->config->registerMigration(3, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateFurther'); |
|
142 |
|
143 $migration = new \Doctrine\DBAL\Migrations\Migration($this->config); |
|
144 $migration->migrate(3); |
|
145 $this->assertEquals(3, $this->config->getCurrentVersion()); |
|
146 $migration->migrate(0); |
|
147 |
|
148 $schema = $this->config->getConnection()->getSchemaManager()->createSchema(); |
|
149 $this->assertFalse($schema->hasTable('foo')); |
|
150 $this->assertFalse($schema->hasTable('bar')); |
|
151 |
|
152 $this->assertEquals(0, $this->config->getCurrentVersion()); |
|
153 $migrations = $this->config->getMigrations(); |
|
154 $this->assertFalse($migrations[1]->isMigrated()); |
|
155 $this->assertFalse($migrations[2]->isMigrated()); |
|
156 $this->assertFalse($migrations[3]->isMigrated()); |
|
157 } |
|
158 |
|
159 public function testAddSql() |
|
160 { |
|
161 $this->config->registerMigration(1, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrateAddSqlTest'); |
|
162 |
|
163 $migration = new \Doctrine\DBAL\Migrations\Migration($this->config); |
|
164 $migration->migrate(1); |
|
165 |
|
166 $migrations = $this->config->getMigrations(); |
|
167 $this->assertTrue($migrations[1]->isMigrated()); |
|
168 |
|
169 $schema = $this->config->getConnection()->getSchemaManager()->createSchema(); |
|
170 $this->assertTrue($schema->hasTable('test_add_sql_table')); |
|
171 $check = $this->config->getConnection()->fetchAll('select * from test_add_sql_table'); |
|
172 $this->assertNotEmpty($check); |
|
173 $this->assertEquals('test', $check[0]['test']); |
|
174 |
|
175 $migration->migrate(0); |
|
176 $this->assertFalse($migrations[1]->isMigrated()); |
|
177 $schema = $this->config->getConnection()->getSchemaManager()->createSchema(); |
|
178 $this->assertFalse($schema->hasTable('test_add_sql_table')); |
|
179 } |
|
180 } |
|
181 |
|
182 class MigrateAddSqlTest extends \Doctrine\DBAL\Migrations\AbstractMigration |
|
183 { |
|
184 public function up(Schema $schema) |
|
185 { |
|
186 $this->addSql("CREATE TABLE test_add_sql_table (test varchar(255))"); |
|
187 $this->addSql("INSERT INTO test_add_sql_table (test) values (?)", array('test')); |
|
188 } |
|
189 |
|
190 public function down(Schema $schema) |
|
191 { |
|
192 $this->addSql("DROP TABLE test_add_sql_table"); |
|
193 } |
|
194 } |
|
195 |
|
196 class MigrationMigrateUp extends \Doctrine\DBAL\Migrations\AbstractMigration |
|
197 { |
|
198 public function down(Schema $schema) |
|
199 { |
|
200 $schema->dropTable('foo'); |
|
201 } |
|
202 |
|
203 public function up(Schema $schema) |
|
204 { |
|
205 $table = $schema->createTable('foo'); |
|
206 $table->addColumn('id', 'integer'); |
|
207 } |
|
208 } |
|
209 |
|
210 class MigrationSkipMigration extends MigrationMigrateUp |
|
211 { |
|
212 |
|
213 public function preUp(Schema $schema) |
|
214 { |
|
215 $this->skipIf(true); |
|
216 } |
|
217 |
|
218 public function preDown(Schema $schema) |
|
219 { |
|
220 $this->skipIf(true); |
|
221 } |
|
222 } |
|
223 |
|
224 class MigrationMigrateFurther extends \Doctrine\DBAL\Migrations\AbstractMigration |
|
225 { |
|
226 |
|
227 public function down(Schema $schema) |
|
228 { |
|
229 $schema->dropTable('bar'); |
|
230 } |
|
231 |
|
232 public function up(Schema $schema) |
|
233 { |
|
234 $table = $schema->createTable('bar'); |
|
235 $table->addColumn('id', 'integer'); |
|
236 } |
|
237 |
|
238 } |