Skip to content

Commit

Permalink
Merge pull request #8 from gruberro/disallow-empty-contexts
Browse files Browse the repository at this point in the history
Disallow empty contexts
  • Loading branch information
gruberro authored Jun 14, 2017
2 parents 5464e64 + 4b5a5a7 commit 2a7ea5e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/Console/Command/MigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
$progress->advance();

if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface && $input->getOption('contexts') !== []) {
if ($migration->getContexts() === []) {
throw new \InvalidArgumentException('An empty context specification is not allowed');
}

if (array_intersect($migration->getContexts(), $input->getOption('contexts')) === []) {
continue;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Console/Command/EmptyContextualMigrations/MigrationA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace TestMigrations\ContextualMigrations;

use Gruberro\MongoDbMigrations;
use MongoDB\Database;

class MigrationA implements MongoDbMigrations\MigrationInterface, MongoDbMigrations\ContextualMigrationInterface
{
public function getId(): string
{
return 'some-context-migration';
}

public function getCreateDate(): \DateTime
{
return new \DateTime('2016-02-25 16:30:00');
}

public function execute(Database $db)
{
}

public function getContexts(): array
{
return [];
}
}
44 changes: 36 additions & 8 deletions tests/Console/Command/MigrationsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Gruberro\MongoDbMigrations;
use MongoDB;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;

class MigrationsCommandTest extends MongoDbMigrations\Tests\TestCase
Expand All @@ -17,6 +19,7 @@ public function testExecuteProjectExamples(): MongoDbMigrations\Console\Command\
$application = new Application();
$application->add(new MongoDbMigrations\Console\Command\MigrationsCommand());

/** @var MongoDbMigrations\Console\Command\MigrationsCommand $command */
$command = $application->find('php-mongodb-migrations:migrate');
$commandTester = new CommandTester($command);
$commandTester->execute(
Expand Down Expand Up @@ -143,8 +146,8 @@ public function testExecuteIsAbortedOnLockedDatabase()
$databaseMigrationsLockCollection = $this->getTestDatabase()->selectCollection('DATABASE_MIGRATIONS_LOCK');
$databaseMigrationsLockCollection->insertOne(['locked' => true, 'last_locked_date' => new MongoDB\BSON\UTCDatetime((new \DateTime())->getTimestamp() * 1000)]);

$this->setExpectedExceptionRegExp(
\Symfony\Component\Console\Exception\RunTimeException::class,
$this->expectException(RunTimeException::class);
$this->expectExceptionMessageRegExp(
'/Concurrent migrations are not allowed/'
);

Expand All @@ -165,8 +168,8 @@ public function testExecuteIsAbortedOnLockedDatabase()

public function testExecuteWithInvalidMigrationDirectory()
{
$this->setExpectedExceptionRegExp(
\Symfony\Component\Console\Exception\InvalidArgumentException::class,
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageRegExp(
'/\'invalid\/dir\' is no valid directory/'
);

Expand Down Expand Up @@ -195,8 +198,8 @@ public function testExecuteWithInvalidMigrationDirectory()
*/
public function testExecuteWithDuplicatedMigrationIds()
{
$this->setExpectedExceptionRegExp(
\Symfony\Component\Console\Exception\RuntimeException::class,
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageRegExp(
'/Found a non unique migration id \'migration\' in \'TestMigrations\\\\DuplicateMigrationIds\\\\MigrationB\', already defined by migration class \'TestMigrations\\\\DuplicateMigrationIds\\\\MigrationA\'/'
);

Expand Down Expand Up @@ -225,8 +228,8 @@ public function testExecuteWithDuplicatedMigrationIds()
*/
public function testExecuteWithFailingMigrations()
{
$this->setExpectedExceptionRegExp(
\Symfony\Component\Console\Exception\RuntimeException::class,
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageRegExp(
'/Error while executing migrations/'
);

Expand Down Expand Up @@ -272,4 +275,29 @@ public function testExecuteANonIncludedContextMigration()
$databaseMigrationsCollection = $this->getTestDatabase()->selectCollection('DATABASE_MIGRATIONS');
$this->assertNull($databaseMigrationsCollection->findOne(['migration_id' => md5('some-context-migration')]));
}

/**
* @runInSeparateProcess
*/
public function testExecuteAnEmptyContextSpecification()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp(
'/Error while executing migrations/'
);

$application = new Application();
$application->add(new MongoDbMigrations\Console\Command\MigrationsCommand());

$command = $application->find('php-mongodb-migrations:migrate');
$commandTester = new CommandTester($command);
$commandTester->execute(
[
'command' => $command->getName(),
'database' => $this->getTestDatabaseName(),
'migration-directories' => [__DIR__ . '/EmptyContextualMigrations/'],
'--contexts' => ['testing']
]
);
}
}

0 comments on commit 2a7ea5e

Please sign in to comment.