From a53d7c83b319341c985d2a21950e260fa55b0b8d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 27 Oct 2015 12:14:41 +0100 Subject: [PATCH] Confinguration: check if migration class exists added --- .../Configuration/Configuration.php | 19 ++++++++++++++ .../MigrationClassNotFoundException.php | 26 +++++++++++++++++++ .../Tests/Configuration/ConfigurationTest.php | 14 ++++++++++ .../Migrations/Version123.php | 8 ++++++ 4 files changed, 67 insertions(+) create mode 100644 lib/Doctrine/DBAL/Migrations/MigrationClassNotFoundException.php create mode 100644 tests/Doctrine/DBAL/Migrations/Tests/Configuration/ConfigurationTestSource/Migrations/Version123.php diff --git a/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php b/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php index 7094b0b2dd..4fde8bf127 100644 --- a/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php @@ -22,6 +22,7 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Migrations\Finder\MigrationDeepFinderInterface; use Doctrine\DBAL\Migrations\MigrationException; +use Doctrine\DBAL\Migrations\MigrationClassNotFoundException; use Doctrine\DBAL\Migrations\OutputWriter; use Doctrine\DBAL\Migrations\Version; use Doctrine\DBAL\Migrations\Finder\MigrationFinderInterface; @@ -371,6 +372,8 @@ public function registerMigrationsFromDirectory($path) */ public function registerMigration($version, $class) { + $this->ensureMigrationClassExists($class); + $version = (string) $version; $class = (string) $class; if (isset($this->migrations[$version])) { @@ -779,4 +782,20 @@ private function shouldExecuteMigration($direction, Version $version, $to, $migr return $version->getVersion() <= $to; } } + + /** + * @param string $class + */ + private function ensureMigrationClassExists($class) + { + if ( ! class_exists($class)) { + throw new MigrationClassNotFoundException( + sprintf( + 'Migration class "%s" was not found. Is it placed in "%s" namespace?', + $class, + $this->getMigrationsNamespace() + ) + ); + } + } } diff --git a/lib/Doctrine/DBAL/Migrations/MigrationClassNotFoundException.php b/lib/Doctrine/DBAL/Migrations/MigrationClassNotFoundException.php new file mode 100644 index 0000000000..0eb083b30e --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/MigrationClassNotFoundException.php @@ -0,0 +1,26 @@ +. + */ + +namespace Doctrine\DBAL\Migrations; + +use Exception; + +class MigrationClassNotFoundException extends Exception +{ +} diff --git a/tests/Doctrine/DBAL/Migrations/Tests/Configuration/ConfigurationTest.php b/tests/Doctrine/DBAL/Migrations/Tests/Configuration/ConfigurationTest.php index ef45149f58..477e9932d8 100644 --- a/tests/Doctrine/DBAL/Migrations/Tests/Configuration/ConfigurationTest.php +++ b/tests/Doctrine/DBAL/Migrations/Tests/Configuration/ConfigurationTest.php @@ -37,6 +37,20 @@ public function testOutputWriterCanBeSet() $this->assertSame($outputWriter, $configuration->getOutputWriter()); } + public function testRegisterMigrationsClassExistCheck() + { + $migrationsDir = __DIR__ . '/ConfigurationTestSource/Migrations'; + + $configuration = new Configuration($this->getConnectionMock()); + $configuration->setMigrationsNamespace('Migrations'); + $configuration->setMigrationsDirectory($migrationsDir); + + $this->setExpectedException( + 'Doctrine\DBAL\Migrations\MigrationClassNotFoundException', + 'Migration class "Migrations\Version123" was not found. Is it placed in "Migrations" namespace?'); + $configuration->registerMigrationsFromDirectory($migrationsDir); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject|Connection */ diff --git a/tests/Doctrine/DBAL/Migrations/Tests/Configuration/ConfigurationTestSource/Migrations/Version123.php b/tests/Doctrine/DBAL/Migrations/Tests/Configuration/ConfigurationTestSource/Migrations/Version123.php new file mode 100644 index 0000000000..4bad3231e1 --- /dev/null +++ b/tests/Doctrine/DBAL/Migrations/Tests/Configuration/ConfigurationTestSource/Migrations/Version123.php @@ -0,0 +1,8 @@ +