Skip to content

Commit 19aeecf

Browse files
committed
Added missing functionality
1 parent 7e8a7d9 commit 19aeecf

File tree

4 files changed

+283
-7
lines changed

4 files changed

+283
-7
lines changed

src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ public function getMigratedVersions()
295295
return $versions;
296296
}
297297

298+
/**
299+
* Return all migrated versions from versions collection that have migration files deleted.
300+
*
301+
* @return array
302+
*/
303+
public function getUnavailableMigratedVersions()
304+
{
305+
return array_diff($this->getMigratedVersions(), $this->getAvailableVersions());
306+
}
307+
298308
/**
299309
* @param string $name
300310
*/
@@ -620,12 +630,11 @@ public function getDetailsMap()
620630
$availableMigrations = $this->getAvailableVersions();
621631
$numAvailableMigrations = count($availableMigrations);
622632

623-
// New migration count
624-
$numNewMigrations = count($availableMigrations) - count($executedMigrations);
625-
626633
// Executed Unavailable migration count
627-
$executedUnavailableMigrations = array_diff($executedMigrations, $availableMigrations);
628-
$numExecutedUnavailableMigrations = count($executedUnavailableMigrations);
634+
$numExecutedUnavailableMigrations = count($this->getUnavailableMigratedVersions());
635+
636+
// New migration count
637+
$numNewMigrations = $numAvailableMigrations - ($numExecutedMigrations - $numExecutedUnavailableMigrations);
629638

630639
return array(
631640
'name' => $this->getName(),

src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/StatusCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,15 @@ public function execute(InputInterface $input, OutputInterface $output)
110110
foreach ($migrations as $version) {
111111
$isMigrated = in_array($version->getVersion(), $migratedVersions);
112112
$status = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>';
113-
$output->writeln(' <comment>>></comment> '.$configuration->formatVersion($version->getVersion()).' (<comment>'.$version->getVersion().'</comment>)'.str_repeat(' ', 30 - strlen($name)).$status);
113+
$output->writeln(' <comment>>></comment> '.Configuration::formatVersion($version->getVersion()).' (<comment>'.$version->getVersion().'</comment>)'.str_repeat(' ', 30 - strlen($name)).$status);
114114
}
115115
}
116116

117+
$executedUnavailableMigrations = $configuration->getUnavailableMigratedVersions();
117118
if ($executedUnavailableMigrations) {
118119
$output->writeln("\n <info>==</info> Previously Executed Unavailable Migration Versions\n");
119120
foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
120-
$output->writeln(' <comment>>></comment> '.$configuration->formatVersion($executedUnavailableMigration).' (<comment>'.$executedUnavailableMigration.'</comment>)');
121+
$output->writeln(' <comment>>></comment> '.Configuration::formatVersion($executedUnavailableMigration).' (<comment>'.$executedUnavailableMigration.'</comment>)');
121122
}
122123
}
123124
}

tests/AntiMattr/Tests/MongoDB/Migrations/Configuration/ConfigurationTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,22 @@ public function testValidateThrowsConfigurationValidationException()
250250
$this->configuration->validate();
251251
}
252252

253+
public function testGetUnavailableMigratedVersions()
254+
{
255+
$configuration = $this->getMockBuilder('AntiMattr\MongoDB\Migrations\Configuration\Configuration')
256+
->disableOriginalConstructor()
257+
->setMethods(array('getMigratedVersions', 'getAvailableVersions'))
258+
->getMock();
259+
$configuration->expects($this->once())
260+
->method('getMigratedVersions')
261+
->will($this->returnValue(array('1', '2')));
262+
$configuration->expects($this->once())
263+
->method('getAvailableVersions')
264+
->will($this->returnValue(array('2', '3')));
265+
266+
$this->assertEquals(array('1'), $configuration->getUnavailableMigratedVersions());
267+
}
268+
253269
public function testValidate()
254270
{
255271
$this->prepareValidConfiguration();

tests/AntiMattr/Tests/MongoDB/Migrations/Tools/Console/Command/StatusCommandTest.php

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class StatusCommandTest extends AntiMattrTestCase
1919
private $config;
2020
private $migration;
2121
private $version;
22+
private $version2;
2223

2324
protected function setUp()
2425
{
@@ -27,6 +28,7 @@ protected function setUp()
2728
$this->config = $this->buildMock('AntiMattr\MongoDB\Migrations\Configuration\Configuration');
2829
$this->migration = $this->buildMock('AntiMattr\MongoDB\Migrations\Migration');
2930
$this->version = $this->buildMock('AntiMattr\MongoDB\Migrations\Version');
31+
$this->version2 = $this->buildMock('AntiMattr\MongoDB\Migrations\Version');
3032

3133
$this->command->setMigrationConfiguration($this->config);
3234
}
@@ -212,6 +214,254 @@ public function testExecuteWithoutShowingVersions()
212214
$this->output
213215
);
214216
}
217+
218+
public function testExecuteWithShowingVersions()
219+
{
220+
$input = new ArgvInput(
221+
array(
222+
StatusCommand::NAME,
223+
'--show-versions',
224+
)
225+
);
226+
227+
$configName = 'config-name';
228+
$databaseDriver = 'MongoDB';
229+
$migrationsDatabaseName = ' migrations-database-name';
230+
$migrationsCollectionName = 'migrations-collection-name';
231+
$migrationsNamespace = 'migrations-namespace';
232+
$migrationsDirectory = 'migrations-directory';
233+
$currentVersion = 'abcdefghijk';
234+
$latestVersion = '1234567890';
235+
$numExecutedMigrations = 2;
236+
$numExecutedUnavailableMigrations = 1;
237+
$numAvailableMigrations = 2;
238+
$numNewMigrations = 1;
239+
$notMigratedVersion = '20140822185743';
240+
$migratedVersion = '20140822185745';
241+
$unavailableMigratedVersion = '20140822185744';
242+
243+
// Expectations
244+
$this->version->expects($this->exactly(3))
245+
->method('getVersion')
246+
->will($this->returnValue($notMigratedVersion));
247+
248+
$this->version2->expects($this->exactly(3))
249+
->method('getVersion')
250+
->will($this->returnValue($migratedVersion));
251+
252+
$this->config->expects($this->once())
253+
->method('getDetailsMap')
254+
->will(
255+
$this->returnValue(
256+
array(
257+
'name' => $configName,
258+
'database_driver' => $databaseDriver,
259+
'migrations_database_name' => $migrationsDatabaseName,
260+
'migrations_collection_name' => $migrationsCollectionName,
261+
'migrations_namespace' => $migrationsNamespace,
262+
'migrations_directory' => $migrationsDirectory,
263+
'current_version' => $currentVersion,
264+
'latest_version' => $latestVersion,
265+
'num_executed_migrations' => $numExecutedMigrations,
266+
'num_executed_unavailable_migrations' => $numExecutedUnavailableMigrations,
267+
'num_available_migrations' => $numAvailableMigrations,
268+
'num_new_migrations' => $numNewMigrations,
269+
)
270+
)
271+
)
272+
;
273+
$this->config->expects($this->once())
274+
->method('getMigrations')
275+
->will(
276+
$this->returnValue(
277+
array($this->version, $this->version2)
278+
)
279+
)
280+
;
281+
$this->config->expects($this->once())
282+
->method('getMigratedVersions')
283+
->will(
284+
$this->returnValue(
285+
array($unavailableMigratedVersion, $migratedVersion)
286+
)
287+
)
288+
;
289+
$this->config->expects($this->once())
290+
->method('getUnavailableMigratedVersions')
291+
->will(
292+
$this->returnValue(
293+
array($unavailableMigratedVersion)
294+
)
295+
)
296+
;
297+
298+
$this->output->expects($this->at(0))
299+
->method('writeln')
300+
->with(
301+
"\n <info>==</info> Configuration\n"
302+
)
303+
;
304+
$this->output->expects($this->at(1))
305+
->method('writeln')
306+
->with(
307+
sprintf(
308+
'%s::%s',
309+
'Name',
310+
$configName
311+
)
312+
)
313+
;
314+
$this->output->expects($this->at(2))
315+
->method('writeln')
316+
->with(
317+
sprintf(
318+
'%s::%s',
319+
'Database Driver',
320+
'MongoDB'
321+
)
322+
)
323+
;
324+
$this->output->expects($this->at(3))
325+
->method('writeln')
326+
->with(
327+
sprintf(
328+
'%s::%s',
329+
'Database Name',
330+
$migrationsDatabaseName
331+
)
332+
)
333+
;
334+
$this->output->expects($this->at(4))
335+
->method('writeln')
336+
->with(
337+
sprintf(
338+
'%s::%s',
339+
'Configuration Source',
340+
'manually configured'
341+
)
342+
)
343+
;
344+
$this->output->expects($this->at(5))
345+
->method('writeln')
346+
->with(
347+
sprintf(
348+
'%s::%s',
349+
'Version Collection Name',
350+
$migrationsCollectionName
351+
)
352+
)
353+
;
354+
$this->output->expects($this->at(6))
355+
->method('writeln')
356+
->with(
357+
sprintf(
358+
'%s::%s',
359+
'Migrations Namespace',
360+
$migrationsNamespace
361+
)
362+
)
363+
;
364+
$this->output->expects($this->at(7))
365+
->method('writeln')
366+
->with(
367+
sprintf(
368+
'%s::%s',
369+
'Migrations Directory',
370+
$migrationsDirectory
371+
)
372+
)
373+
;
374+
$this->output->expects($this->at(8)) // current version formatted
375+
->method('writeln')
376+
;
377+
$this->output->expects($this->at(9)) // latest version formatted
378+
->method('writeln')
379+
;
380+
$this->output->expects($this->at(10))
381+
->method('writeln')
382+
->with(
383+
sprintf(
384+
'%s::%s',
385+
'Executed Migrations',
386+
$numExecutedMigrations
387+
)
388+
)
389+
;
390+
$this->output->expects($this->at(11))
391+
->method('writeln')
392+
->with(
393+
sprintf(
394+
'%s::<error>%s</error>',
395+
'Executed Unavailable Migrations',
396+
$numExecutedUnavailableMigrations
397+
)
398+
)
399+
;
400+
$this->output->expects($this->at(12))
401+
->method('writeln')
402+
->with(
403+
sprintf(
404+
'%s::%s',
405+
'Available Migrations',
406+
$numAvailableMigrations
407+
)
408+
)
409+
;
410+
$this->output->expects($this->at(13))
411+
->method('writeln')
412+
->with(
413+
sprintf(
414+
'%s::<question>%s</question>',
415+
'New Migrations',
416+
$numNewMigrations
417+
)
418+
)
419+
;
420+
$this->output->expects($this->at(14))
421+
->method('writeln')
422+
->with("\n <info>==</info> Available Migration Versions\n")
423+
;
424+
$this->output->expects($this->at(15))
425+
->method('writeln')
426+
->with(
427+
sprintf(
428+
' <comment>>></comment> %s (<comment>%s</comment>) <error>not migrated</error>',
429+
\DateTime::createFromFormat('YmdHis', $notMigratedVersion)->format('Y-m-d H:i:s'),
430+
$notMigratedVersion
431+
)
432+
)
433+
;
434+
$this->output->expects($this->at(16))
435+
->method('writeln')
436+
->with(
437+
sprintf(
438+
' <comment>>></comment> %s (<comment>%s</comment>) <info>migrated</info>',
439+
\DateTime::createFromFormat('YmdHis', $migratedVersion)->format('Y-m-d H:i:s'),
440+
$migratedVersion
441+
)
442+
)
443+
;
444+
$this->output->expects($this->at(17))
445+
->method('writeln')
446+
->with("\n <info>==</info> Previously Executed Unavailable Migration Versions\n")
447+
;
448+
$this->output->expects($this->at(18))
449+
->method('writeln')
450+
->with(
451+
sprintf(
452+
' <comment>>></comment> %s (<comment>%s</comment>)',
453+
\DateTime::createFromFormat('YmdHis', $unavailableMigratedVersion)->format('Y-m-d H:i:s'),
454+
$unavailableMigratedVersion
455+
)
456+
)
457+
;
458+
459+
// Run command, run.
460+
$this->command->run(
461+
$input,
462+
$this->output
463+
);
464+
}
215465
}
216466

217467
class StatusCommandStub extends StatusCommand

0 commit comments

Comments
 (0)