From 6d1075c0076abcfab12039bcd2c718e3deb3646c Mon Sep 17 00:00:00 2001 From: Michael Moravec Date: Sun, 6 May 2018 03:56:48 +0200 Subject: [PATCH] Split and move around exceptions, make MigrationException an interface --- .../Migrations/AbortMigrationException.php | 9 -- lib/Doctrine/Migrations/AbstractMigration.php | 13 +- .../AbstractFileConfiguration.php | 16 +-- .../Configuration/Configuration.php | 22 +-- .../Exception/ConfigurationException.php | 11 ++ .../Exception/FileAlreadyLoaded.php | 15 ++ .../Exception/InvalidConfigurationKey.php | 16 +++ .../Configuration/Exception/JsonNotValid.php | 15 ++ .../Exception/MigrationsNamespaceRequired.php | 18 +++ .../ParameterIncompatibleWithFinder.php | 25 ++++ .../Exception/UnknownConfigurationValue.php | 27 ++++ .../Configuration/Exception/XmlNotValid.php | 15 ++ .../Exception/YamlNotAvailable.php | 18 +++ .../Configuration/Exception/YamlNotValid.php | 15 ++ .../Configuration/JsonConfiguration.php | 4 +- .../Configuration/XmlConfiguration.php | 4 +- .../Configuration/YamlConfiguration.php | 7 +- .../Migrations/Exception/AbortMigration.php | 11 ++ .../Migrations/Exception/AlreadyAtVersion.php | 22 +++ .../Migrations/Exception/ControlException.php | 9 ++ .../Exception/DuplicateMigrationVersion.php | 23 +++ .../Exception/IrreversibleMigration.php | 11 ++ .../Exception/MigrationClassNotFound.php | 22 +++ .../Exception/MigrationException.php | 11 ++ .../MigrationNotConvertibleToSql.php | 22 +++ .../Exception/MigrationsDirectoryRequired.php | 18 +++ .../Exception/NoMigrationsToExecute.php | 18 +++ .../Migrations/Exception/SkipMigration.php | 11 ++ .../Exception/UnknownMigrationVersion.php | 22 +++ .../IrreversibleMigrationException.php | 11 -- lib/Doctrine/Migrations/Migration.php | 7 +- .../Migrations/MigrationException.php | 135 ------------------ .../Migrations/SkipMigrationException.php | 9 -- .../Tools/Console/Command/VersionCommand.php | 5 +- lib/Doctrine/Migrations/Version.php | 6 +- phpcs.xml.dist | 8 -- .../Tests/AbstractMigrationTest.php | 16 +-- .../AbstractConfigurationTest.php | 2 +- .../Tests/Configuration/ConfigurationTest.php | 2 +- .../Migrations/Tests/ConfigurationTest.php | 2 +- .../Migrations/Tests/MigrationTest.php | 2 +- .../Doctrine/Migrations/Tests/VersionTest.php | 2 +- 42 files changed, 437 insertions(+), 220 deletions(-) delete mode 100644 lib/Doctrine/Migrations/AbortMigrationException.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/ConfigurationException.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/FileAlreadyLoaded.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/InvalidConfigurationKey.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/JsonNotValid.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/MigrationsNamespaceRequired.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/ParameterIncompatibleWithFinder.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/UnknownConfigurationValue.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/XmlNotValid.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/YamlNotAvailable.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/YamlNotValid.php create mode 100644 lib/Doctrine/Migrations/Exception/AbortMigration.php create mode 100644 lib/Doctrine/Migrations/Exception/AlreadyAtVersion.php create mode 100644 lib/Doctrine/Migrations/Exception/ControlException.php create mode 100644 lib/Doctrine/Migrations/Exception/DuplicateMigrationVersion.php create mode 100644 lib/Doctrine/Migrations/Exception/IrreversibleMigration.php create mode 100644 lib/Doctrine/Migrations/Exception/MigrationClassNotFound.php create mode 100644 lib/Doctrine/Migrations/Exception/MigrationException.php create mode 100644 lib/Doctrine/Migrations/Exception/MigrationNotConvertibleToSql.php create mode 100644 lib/Doctrine/Migrations/Exception/MigrationsDirectoryRequired.php create mode 100644 lib/Doctrine/Migrations/Exception/NoMigrationsToExecute.php create mode 100644 lib/Doctrine/Migrations/Exception/SkipMigration.php create mode 100644 lib/Doctrine/Migrations/Exception/UnknownMigrationVersion.php delete mode 100644 lib/Doctrine/Migrations/IrreversibleMigrationException.php delete mode 100644 lib/Doctrine/Migrations/MigrationException.php delete mode 100644 lib/Doctrine/Migrations/SkipMigrationException.php diff --git a/lib/Doctrine/Migrations/AbortMigrationException.php b/lib/Doctrine/Migrations/AbortMigrationException.php deleted file mode 100644 index 8f5571436e..0000000000 --- a/lib/Doctrine/Migrations/AbortMigrationException.php +++ /dev/null @@ -1,9 +0,0 @@ - $configurationValue) { if (! in_array($configurationKey, self::ALLOWED_CONFIGURATION_KEYS, true)) { - $msg = sprintf('Migrations configuration key "%s" does not exist.', $configurationKey); - - throw MigrationException::configurationNotValid($msg); + throw InvalidConfigurationKey::new($configurationKey); } } @@ -107,8 +106,7 @@ private function setMigrationOrganization(string $migrationOrganization) : void } elseif (strcasecmp($migrationOrganization, static::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) === 0) { $this->setMigrationsAreOrganizedByYearAndMonth(); } else { - $msg = 'Unknown ' . var_export($migrationOrganization, true) . ' for configuration "organize_migrations".'; - throw MigrationException::configurationNotValid($msg); + throw UnknownConfigurationValue::new('organize_migrations', $migrationOrganization); } } @@ -116,7 +114,7 @@ private function setMigrationOrganization(string $migrationOrganization) : void public function load(string $file) : void { if ($this->loaded) { - throw MigrationException::configurationFileAlreadyLoaded(); + throw FileAlreadyLoaded::new(); } $path = getcwd() . '/' . $file; diff --git a/lib/Doctrine/Migrations/Configuration/Configuration.php b/lib/Doctrine/Migrations/Configuration/Configuration.php index 3da6f51fd9..2f90e74b08 100644 --- a/lib/Doctrine/Migrations/Configuration/Configuration.php +++ b/lib/Doctrine/Migrations/Configuration/Configuration.php @@ -13,11 +13,17 @@ use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Types\Type; +use Doctrine\Migrations\Configuration\Exception\MigrationsNamespaceRequired; +use Doctrine\Migrations\Configuration\Exception\ParameterIncompatibleWithFinder; +use Doctrine\Migrations\Exception\DuplicateMigrationVersion; +use Doctrine\Migrations\Exception\MigrationClassNotFound; +use Doctrine\Migrations\Exception\MigrationException; +use Doctrine\Migrations\Exception\MigrationsDirectoryRequired; +use Doctrine\Migrations\Exception\UnknownMigrationVersion; use Doctrine\Migrations\FileQueryWriter; use Doctrine\Migrations\Finder\MigrationDeepFinder; use Doctrine\Migrations\Finder\MigrationFinder; use Doctrine\Migrations\Finder\RecursiveRegexFinder; -use Doctrine\Migrations\MigrationException; use Doctrine\Migrations\OutputWriter; use Doctrine\Migrations\QueryWriter; use Doctrine\Migrations\Version; @@ -119,11 +125,11 @@ public function areMigrationsOrganizedByYearAndMonth() : bool public function validate() : void { if ($this->migrationsNamespace === null) { - throw MigrationException::migrationsNamespaceRequired(); + throw MigrationsNamespaceRequired::new(); } if ($this->migrationsDirectory === null) { - throw MigrationException::migrationsDirectoryRequired(); + throw MigrationsDirectoryRequired::new(); } } @@ -224,7 +230,7 @@ public function setMigrationsFinder(MigrationFinder $finder) : void { if (($this->migrationsAreOrganizedByYear || $this->migrationsAreOrganizedByYearAndMonth) && ! ($finder instanceof MigrationDeepFinder)) { - throw MigrationException::configurationIncompatibleWithFinder( + throw ParameterIncompatibleWithFinder::new( 'organize-migrations', $finder ); @@ -247,7 +253,7 @@ public function registerMigration(string $version, string $class) : Version $this->ensureMigrationClassExists($class); if (isset($this->migrations[$version])) { - throw MigrationException::duplicateMigrationVersion( + throw DuplicateMigrationVersion::new( $version, get_class($this->migrations[$version]) ); @@ -291,7 +297,7 @@ public function getVersion(string $version) : Version $this->loadMigrationsFromDirectory(); if (! isset($this->migrations[$version])) { - throw MigrationException::unknownMigrationVersion($version); + throw UnknownMigrationVersion::new($version); } return $this->migrations[$version]; @@ -644,7 +650,7 @@ protected function connect() : bool private function ensureOrganizeMigrationsIsCompatibleWithFinder() : void { if (! ($this->migrationFinder instanceof MigrationDeepFinder)) { - throw MigrationException::configurationIncompatibleWithFinder( + throw ParameterIncompatibleWithFinder::new( 'organize-migrations', $this->migrationFinder ); @@ -683,7 +689,7 @@ private function shouldExecuteMigration( private function ensureMigrationClassExists(string $class) : void { if (! class_exists($class)) { - throw MigrationException::migrationClassNotFound( + throw MigrationClassNotFound::new( $class, $this->getMigrationsNamespace() ); diff --git a/lib/Doctrine/Migrations/Configuration/Exception/ConfigurationException.php b/lib/Doctrine/Migrations/Configuration/Exception/ConfigurationException.php new file mode 100644 index 0000000000..8db929c641 --- /dev/null +++ b/lib/Doctrine/Migrations/Configuration/Exception/ConfigurationException.php @@ -0,0 +1,11 @@ +schemaValidate($xsdPath)) { libxml_clear_errors(); - throw MigrationException::configurationNotValid('XML configuration did not pass the validation test.'); + throw XmlNotValid::new(); } $xml = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA); diff --git a/lib/Doctrine/Migrations/Configuration/YamlConfiguration.php b/lib/Doctrine/Migrations/Configuration/YamlConfiguration.php index 4e46023410..f3c6033b8d 100644 --- a/lib/Doctrine/Migrations/Configuration/YamlConfiguration.php +++ b/lib/Doctrine/Migrations/Configuration/YamlConfiguration.php @@ -4,7 +4,8 @@ namespace Doctrine\Migrations\Configuration; -use Doctrine\Migrations\MigrationException; +use Doctrine\Migrations\Configuration\Exception\YamlNotAvailable; +use Doctrine\Migrations\Configuration\Exception\YamlNotValid; use Symfony\Component\Yaml\Yaml; use function class_exists; use function file_get_contents; @@ -18,13 +19,13 @@ class YamlConfiguration extends AbstractFileConfiguration protected function doLoad(string $file) : void { if (! class_exists(Yaml::class)) { - throw MigrationException::yamlConfigurationNotAvailable(); + throw YamlNotAvailable::new(); } $config = Yaml::parse(file_get_contents($file)); if (! is_array($config)) { - throw MigrationException::configurationNotValid('Configuration is not valid YAML.'); + throw YamlNotValid::new(); } if (isset($config['migrations_directory'])) { diff --git a/lib/Doctrine/Migrations/Exception/AbortMigration.php b/lib/Doctrine/Migrations/Exception/AbortMigration.php new file mode 100644 index 0000000000..27607e75b8 --- /dev/null +++ b/lib/Doctrine/Migrations/Exception/AbortMigration.php @@ -0,0 +1,11 @@ +configuration->getMigrations(); if (! isset($migrations[$to]) && $to > 0) { - throw MigrationException::unknownMigrationVersion($to); + throw UnknownMigrationVersion::new($to); } $direction = $from > $to @@ -126,7 +129,7 @@ public function migrate( * If there are no migrations to execute throw an exception. */ if (empty($migrationsToExecute) && ! $this->noMigrationException) { - throw MigrationException::noMigrationsToExecute(); + throw NoMigrationsToExecute::new(); } elseif (empty($migrationsToExecute)) { return $this->noMigrations(); } diff --git a/lib/Doctrine/Migrations/MigrationException.php b/lib/Doctrine/Migrations/MigrationException.php deleted file mode 100644 index 4f5d4775d2..0000000000 --- a/lib/Doctrine/Migrations/MigrationException.php +++ /dev/null @@ -1,135 +0,0 @@ -configuration->hasVersion($version)) { - throw MigrationException::unknownMigrationVersion($version); + throw UnknownMigrationVersion::new($version); } $version = $this->configuration->getVersion($version); diff --git a/lib/Doctrine/Migrations/Version.php b/lib/Doctrine/Migrations/Version.php index 0f509d2a51..2d196e0302 100644 --- a/lib/Doctrine/Migrations/Version.php +++ b/lib/Doctrine/Migrations/Version.php @@ -8,6 +8,8 @@ use Doctrine\DBAL\Types\Type; use Doctrine\Migrations\Configuration\Configuration; use Doctrine\Migrations\Event\MigrationsVersionEventArgs; +use Doctrine\Migrations\Exception\MigrationNotConvertibleToSql; +use Doctrine\Migrations\Exception\SkipMigration; use Doctrine\Migrations\Provider\LazySchemaDiffProvider; use Doctrine\Migrations\Provider\SchemaDiffProvider; use Doctrine\Migrations\Provider\SchemaDiffProviderInterface; @@ -161,7 +163,7 @@ public function writeSqlFile( $queries = $this->execute($direction, true); if (! empty($this->params)) { - throw MigrationException::migrationNotConvertibleToSql($this->class); + throw MigrationNotConvertibleToSql::new($this->class); } $this->outputWriter->write("\n-- Version " . $this->version . "\n"); @@ -250,7 +252,7 @@ public function execute( $this->dispatchEvent(Events::onMigrationsVersionExecuted, $direction, $dryRun); return $this->sql; - } catch (SkipMigrationException $e) { + } catch (SkipMigration $e) { if ($transaction) { //only rollback transaction if in transactional mode $this->connection->rollBack(); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index e7d65577ff..80a87569f9 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -48,14 +48,6 @@ tests/Doctrine/Migrations/Tests/Tools/Console/Command/AbstractCommandTest.php - - lib/Doctrine/Migrations/AbortMigrationException.php - lib/Doctrine/Migrations/MigrationException.php - lib/Doctrine/Migrations/IrreversibleMigrationException.php - lib/Doctrine/Migrations/SkipMigrationException.php - tests/Doctrine/Migrations/Tests/Stub/VersionDummyException.php - - lib/Doctrine/Migrations/Configuration/Connection/ConnectionLoaderInterface.php lib/Doctrine/Migrations/Provider/SchemaDiffProviderInterface.php diff --git a/tests/Doctrine/Migrations/Tests/AbstractMigrationTest.php b/tests/Doctrine/Migrations/Tests/AbstractMigrationTest.php index 27d126c6c2..701ca437f4 100644 --- a/tests/Doctrine/Migrations/Tests/AbstractMigrationTest.php +++ b/tests/Doctrine/Migrations/Tests/AbstractMigrationTest.php @@ -4,11 +4,11 @@ namespace Doctrine\Migrations\Tests; -use Doctrine\Migrations\AbortMigrationException; use Doctrine\Migrations\Configuration\Configuration; -use Doctrine\Migrations\IrreversibleMigrationException; +use Doctrine\Migrations\Exception\AbortMigration; +use Doctrine\Migrations\Exception\IrreversibleMigration; +use Doctrine\Migrations\Exception\SkipMigration; use Doctrine\Migrations\OutputWriter; -use Doctrine\Migrations\SkipMigrationException; use Doctrine\Migrations\Tests\Stub\AbstractMigrationStub; use Doctrine\Migrations\Tests\Stub\VersionDummy; use Doctrine\Migrations\Version; @@ -71,7 +71,7 @@ public function testWriteInvokesOutputWriter() : void public function testAbortIfThrowsException() : void { - $this->expectException(AbortMigrationException::class); + $this->expectException(AbortMigration::class); $this->expectExceptionMessage('Something failed'); $this->migration->abortIf(true, 'Something failed'); @@ -85,7 +85,7 @@ public function testAbortIfDontThrowsException() : void public function testAbortIfThrowsExceptionEvenWithoutMessage() : void { - $this->expectException(AbortMigrationException::class); + $this->expectException(AbortMigration::class); $this->expectExceptionMessage('Unknown Reason'); $this->migration->abortIf(true); @@ -93,7 +93,7 @@ public function testAbortIfThrowsExceptionEvenWithoutMessage() : void public function testSkipIfThrowsException() : void { - $this->expectException(SkipMigrationException::class); + $this->expectException(SkipMigration::class); $this->expectExceptionMessage('Something skipped'); $this->migration->skipIf(true, 'Something skipped'); @@ -107,7 +107,7 @@ public function testSkipIfDontThrowsException() : void public function testThrowIrreversibleMigrationException() : void { - $this->expectException(IrreversibleMigrationException::class); + $this->expectException(IrreversibleMigration::class); $this->expectExceptionMessage('Irreversible migration'); $this->migration->exposedThrowIrreversibleMigrationException('Irreversible migration'); @@ -115,7 +115,7 @@ public function testThrowIrreversibleMigrationException() : void public function testThrowIrreversibleMigrationExceptionWithoutMessage() : void { - $this->expectException(IrreversibleMigrationException::class); + $this->expectException(IrreversibleMigration::class); $this->expectExceptionMessage('This migration is irreversible and cannot be reverted.'); $this->migration->exposedThrowIrreversibleMigrationException(); diff --git a/tests/Doctrine/Migrations/Tests/Configuration/AbstractConfigurationTest.php b/tests/Doctrine/Migrations/Tests/Configuration/AbstractConfigurationTest.php index 97420ef860..d4511e1ae3 100644 --- a/tests/Doctrine/Migrations/Tests/Configuration/AbstractConfigurationTest.php +++ b/tests/Doctrine/Migrations/Tests/Configuration/AbstractConfigurationTest.php @@ -6,9 +6,9 @@ use Doctrine\Migrations\Configuration\AbstractFileConfiguration; use Doctrine\Migrations\Configuration\Configuration; +use Doctrine\Migrations\Exception\MigrationException; use Doctrine\Migrations\Finder\GlobFinder; use Doctrine\Migrations\Finder\MigrationFinder; -use Doctrine\Migrations\MigrationException; use Doctrine\Migrations\OutputWriter; use Doctrine\Migrations\Tests\MigrationTestCase; use InvalidArgumentException; diff --git a/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationTest.php b/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationTest.php index 547cd3416c..4564c8de3f 100644 --- a/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationTest.php +++ b/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationTest.php @@ -13,8 +13,8 @@ use Doctrine\DBAL\Platforms\Keywords\KeywordList; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\Migrations\Configuration\Configuration; +use Doctrine\Migrations\Exception\MigrationException; use Doctrine\Migrations\Migration; -use Doctrine\Migrations\MigrationException; use Doctrine\Migrations\OutputWriter; use Doctrine\Migrations\QueryWriter; use Doctrine\Migrations\Tests\MigrationTestCase; diff --git a/tests/Doctrine/Migrations/Tests/ConfigurationTest.php b/tests/Doctrine/Migrations/Tests/ConfigurationTest.php index d6499f141e..d5aa34e066 100644 --- a/tests/Doctrine/Migrations/Tests/ConfigurationTest.php +++ b/tests/Doctrine/Migrations/Tests/ConfigurationTest.php @@ -7,7 +7,7 @@ use Doctrine\Migrations\Configuration\Configuration; use Doctrine\Migrations\Event\MigrationsEventArgs; use Doctrine\Migrations\Events; -use Doctrine\Migrations\MigrationException; +use Doctrine\Migrations\Exception\MigrationException; use Doctrine\Migrations\Tests\Stub\EventVerificationListener; use Doctrine\Migrations\Tests\Stub\Version1Test; use Doctrine\Migrations\Tests\Stub\Version2Test; diff --git a/tests/Doctrine/Migrations/Tests/MigrationTest.php b/tests/Doctrine/Migrations/Tests/MigrationTest.php index 1c48fc6f43..30fe7920e0 100644 --- a/tests/Doctrine/Migrations/Tests/MigrationTest.php +++ b/tests/Doctrine/Migrations/Tests/MigrationTest.php @@ -6,8 +6,8 @@ use Doctrine\DBAL\Driver\Connection; use Doctrine\Migrations\Configuration\Configuration; +use Doctrine\Migrations\Exception\MigrationException; use Doctrine\Migrations\Migration; -use Doctrine\Migrations\MigrationException; use Doctrine\Migrations\OutputWriter; use Doctrine\Migrations\QueryWriter; use Doctrine\Migrations\Tests\Stub\Functional\MigrateNotTouchingTheSchema; diff --git a/tests/Doctrine/Migrations/Tests/VersionTest.php b/tests/Doctrine/Migrations/Tests/VersionTest.php index ff64f19ca6..f77e0927b5 100644 --- a/tests/Doctrine/Migrations/Tests/VersionTest.php +++ b/tests/Doctrine/Migrations/Tests/VersionTest.php @@ -7,7 +7,7 @@ use DateTime; use Doctrine\DBAL\Connection; use Doctrine\Migrations\Configuration\Configuration; -use Doctrine\Migrations\MigrationException; +use Doctrine\Migrations\Exception\MigrationException; use Doctrine\Migrations\OutputWriter; use Doctrine\Migrations\QueryWriter; use Doctrine\Migrations\Tests\Stub\ExceptionVersionDummy;