ManagerTest.php
12.3 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
namespace Test\Phinx\Migration;
use Symfony\Component\Console\Output\StreamOutput;
use Phinx\Config\Config;
use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Migration\Manager;
use Phinx\Migration\Manager\Environment;
class ManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Manager
*/
private $manager;
protected function setUp()
{
$config = new Config($this->getConfigArray());
$output = new StreamOutput(fopen('php://memory', 'a', false));
$this->manager = new Manager($config, $output);
}
protected function tearDown()
{
$this->manager = null;
}
private function getCorrectedPath($path)
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
/**
* Returns a sample configuration array for use with the unit tests.
*
* @return array
*/
public function getConfigArray()
{
return array(
'paths' => array(
'migrations' => $this->getCorrectedPath(__DIR__ . '/_files/migrations'),
),
'environments' => array(
'default_migration_table' => 'phinxlog',
'default_database' => 'production',
'production' => array(
'adapter' => 'mysql',
'host' => TESTS_PHINX_DB_ADAPTER_MYSQL_HOST,
'name' => TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE,
'user' => TESTS_PHINX_DB_ADAPTER_MYSQL_USERNAME,
'pass' => TESTS_PHINX_DB_ADAPTER_MYSQL_PASSWORD,
'port' => TESTS_PHINX_DB_ADAPTER_MYSQL_PORT
)
)
);
}
public function testInstantiation()
{
$this->assertTrue($this->manager->getOutput() instanceof StreamOutput);
}
public function testPrintStatusMethod()
{
// stub environment
$envStub = $this->getMock('\Phinx\Migration\Manager\Environment', array(), array('mockenv', array()));
$envStub->expects($this->once())
->method('getVersions')
->will($this->returnValue(array('20120111235330', '20120116183504')));
$this->manager->setEnvironments(array('mockenv' => $envStub));
$this->manager->printStatus('mockenv');
rewind($this->manager->getOutput()->getStream());
$outputStr = stream_get_contents($this->manager->getOutput()->getStream());
$this->assertRegExp('/up 20120111235330 TestMigration/', $outputStr);
$this->assertRegExp('/up 20120116183504 TestMigration2/', $outputStr);
}
public function testPrintStatusMethodWithNoMigrations()
{
// stub environment
$envStub = $this->getMock('\Phinx\Migration\Manager\Environment', array(), array('mockenv', array()));
// override the migrations directory to an empty one
$configArray = $this->getConfigArray();
$configArray['paths']['migrations'] = $this->getCorrectedPath(__DIR__ . '/_files/nomigrations');
$config = new Config($configArray);
$this->manager->setConfig($config);
$this->manager->setEnvironments(array('mockenv' => $envStub));
$this->manager->printStatus('mockenv');
rewind($this->manager->getOutput()->getStream());
$outputStr = stream_get_contents($this->manager->getOutput()->getStream());
$this->assertRegExp('/There are no available migrations. Try creating one using the create command./', $outputStr);
}
public function testPrintStatusMethodWithMissingMigrations()
{
// stub environment
$envStub = $this->getMock('\Phinx\Migration\Manager\Environment', array(), array('mockenv', array()));
$envStub->expects($this->once())
->method('getVersions')
->will($this->returnValue(array('20120103083300', '20120815145812')));
$this->manager->setEnvironments(array('mockenv' => $envStub));
$this->manager->printStatus('mockenv');
rewind($this->manager->getOutput()->getStream());
$outputStr = stream_get_contents($this->manager->getOutput()->getStream());
$this->assertRegExp('/up 20120103083300 \*\* MISSING \*\*/', $outputStr);
$this->assertRegExp('/up 20120815145812 \*\* MISSING \*\*/', $outputStr);
}
public function testGetMigrationsWithDuplicateMigrationVersions()
{
$this->setExpectedException(
'InvalidArgumentException',
'Duplicate migration - "' . $this->getCorrectedPath(__DIR__ . '/_files/duplicateversions/20120111235330_duplicate_migration_2.php') . '" has the same version as "20120111235330"'
);
$config = new Config(array('paths' => array('migrations' => $this->getCorrectedPath(__DIR__ . '/_files/duplicateversions'))));
$output = new StreamOutput(fopen('php://memory', 'a', false));
$manager = new Manager($config, $output);
$manager->getMigrations();
}
public function testGetMigrationsWithDuplicateMigrationNames()
{
$this->setExpectedException(
'InvalidArgumentException',
'Migration "20120111235331_duplicate_migration_name.php" has the same name as "20120111235330_duplicate_migration_name.php"'
);
$config = new Config(array('paths' => array('migrations' => $this->getCorrectedPath(__DIR__ . '/_files/duplicatenames'))));
$output = new StreamOutput(fopen('php://memory', 'a', false));
$manager = new Manager($config, $output);
$manager->getMigrations();
}
public function testGetMigrationsWithInvalidMigrationClassName()
{
$this->setExpectedException(
'InvalidArgumentException',
'Could not find class "InvalidClass" in file "' . $this->getCorrectedPath(__DIR__ . '/_files/invalidclassname/20120111235330_invalid_class.php') . '"'
);
$config = new Config(array('paths' => array('migrations' => $this->getCorrectedPath(__DIR__ . '/_files/invalidclassname'))));
$output = new StreamOutput(fopen('php://memory', 'a', false));
$manager = new Manager($config, $output);
$manager->getMigrations();
}
public function testGetMigrationsWithClassThatDoesntExtendAbstractMigration()
{
$this->setExpectedException(
'InvalidArgumentException',
'The class "InvalidSuperClass" in file "' . $this->getCorrectedPath(__DIR__ . '/_files/invalidsuperclass/20120111235330_invalid_super_class.php') . '" must extend \Phinx\Migration\AbstractMigration'
);
$config = new Config(array('paths' => array('migrations' => $this->getCorrectedPath(__DIR__ . '/_files/invalidsuperclass'))));
$output = new StreamOutput(fopen('php://memory', 'a', false));
$manager = new Manager($config, $output);
$manager->getMigrations();
}
public function testGettingAValidEnvironment()
{
$this->assertTrue($this->manager->getEnvironment('production') instanceof Environment);
}
/**
* Test that migrating by date chooses the correct
* migration to point to.
*
* @dataProvider migrateDateDataProvider
*/
public function testMigrationsByDate($availableMigrations, $dateString, $expectedMigration)
{
// stub environment
$envStub = $this->getMock('\Phinx\Migration\Manager\Environment', array(), array('mockenv', array()));
$envStub->expects($this->once())
->method('getVersions')
->will($this->returnValue($availableMigrations));
$this->manager->setEnvironments(array('mockenv' => $envStub));
$this->manager->migrateToDateTime('mockenv', new \DateTime($dateString));
rewind($this->manager->getOutput()->getStream());
$output = stream_get_contents($this->manager->getOutput()->getStream());
if (is_null($expectedMigration)) {
$this->assertEmpty($output);
} else {
$this->assertContains($expectedMigration, $output);
}
}
/**
* Test that migrating by date chooses the correct
* migration to point to.
*
* @dataProvider rollbackDateDataProvider
*/
public function testRollbacksByDate($availableRollbacks, $dateString, $expectedRollback)
{
// stub environment
$envStub = $this->getMock('\Phinx\Migration\Manager\Environment', array(), array('mockenv', array()));
$envStub->expects($this->any())
->method('getVersions')
->will($this->returnValue($availableRollbacks));
$this->manager->setEnvironments(array('mockenv' => $envStub));
$this->manager->rollbackToDateTime('mockenv', new \DateTime($dateString));
rewind($this->manager->getOutput()->getStream());
$output = stream_get_contents($this->manager->getOutput()->getStream());
if (is_null($expectedRollback)) {
$this->assertEmpty($output);
} else {
$this->assertContains($expectedRollback, $output);
}
}
/**
* Migration lists, dates, and expected migrations to point to.
*
* @return array
*/
public function migrateDateDataProvider()
{
return array(
array(array('20120111235330', '20120116183504'), '20120118', '20120116183504'),
array(array('20120111235330', '20120116183504'), '20120115', '20120111235330'),
array(array('20120111235330', '20120116183504'), '20110115', null),
);
}
/**
* Migration lists, dates, and expected migrations to point to.
*
* @return array
*/
public function rollbackDateDataProvider()
{
return array(
array(array('20120111235330', '20120116183504', '20120120183504'), '20120118', '20120116183504'),
array(array('20120111235330', '20120116183504'), '20120115', '20120111235330'),
array(array('20120111235330', '20120116183504'), '20110115', '20120111235330'),
);
}
public function testGettingOutputObject()
{
$migrations = $this->manager->getMigrations();
$outputObject = $this->manager->getOutput();
$this->assertInstanceOf('\Symfony\Component\Console\Output\OutputInterface', $outputObject);
foreach ($migrations as $migration) {
$this->assertEquals($outputObject, $migration->getOutput());
}
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The environment "invalidenv" does not exist
*/
public function testGettingAnInvalidEnvironment()
{
$this->manager->getEnvironment('invalidenv');
}
public function testReversibleMigrationsWorkAsExpected()
{
if (!TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED) {
$this->markTestSkipped('Mysql tests disabled. See TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED constant.');
}
$configArray = $this->getConfigArray();
$adapter = $this->manager->getEnvironment('production')->getAdapter();
// override the migrations directory to use the reversible migrations
$configArray['paths']['migrations'] = $this->getCorrectedPath(__DIR__ . '/_files/reversiblemigrations');
$config = new Config($configArray);
// ensure the database is empty
$adapter->dropDatabase(TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE);
$adapter->createDatabase(TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE);
$adapter->disconnect();
// migrate to the latest version
$this->manager->setConfig($config);
$this->manager->migrate('production');
// ensure up migrations worked
$this->assertFalse($adapter->hasTable('info'));
$this->assertTrue($adapter->hasTable('statuses'));
$this->assertTrue($adapter->hasTable('users'));
$this->assertTrue($adapter->hasTable('user_logins'));
$this->assertTrue($adapter->hasColumn('users', 'biography'));
$this->assertTrue($adapter->hasForeignKey('user_logins', array('user_id')));
// revert all changes to the first
$this->manager->rollback('production', '20121213232502');
// ensure reversed migrations worked
$this->assertTrue($adapter->hasTable('info'));
$this->assertFalse($adapter->hasTable('statuses'));
$this->assertFalse($adapter->hasTable('user_logins'));
$this->assertTrue($adapter->hasColumn('users', 'bio'));
$this->assertFalse($adapter->hasForeignKey('user_logins', array('user_id')));
}
}