diff --git a/lib/Doctrine/Migrations/Configuration/Loader/ArrayLoader.php b/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationArray.php similarity index 87% rename from lib/Doctrine/Migrations/Configuration/Loader/ArrayLoader.php rename to lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationArray.php index c7970fcec6..441e8cc3f2 100644 --- a/lib/Doctrine/Migrations/Configuration/Loader/ArrayLoader.php +++ b/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationArray.php @@ -2,12 +2,11 @@ declare(strict_types=1); -namespace Doctrine\Migrations\Configuration\Loader; +namespace Doctrine\Migrations\Configuration\Configuration; use Closure; use Doctrine\Migrations\Configuration\Configuration; -use Doctrine\Migrations\Configuration\Exception\InvalidConfigurationKey; -use Doctrine\Migrations\Configuration\Exception\UnableToLoadResource; +use Doctrine\Migrations\Configuration\Configuration\Exception\InvalidConfigurationKey; use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration; use Doctrine\Migrations\Tools\BooleanStringFormatter; use function assert; @@ -16,20 +15,21 @@ use function is_bool; use function is_callable; -/** - * @internal - */ -final class ArrayLoader implements Loader +final class ConfigurationArray implements ConfigurationLoader { + /** @var array */ + private $configurations; + /** - * @param mixed $array + * @param array $configurations */ - public function load($array) : Configuration + public function __construct(array $configurations) { - if (! is_array($array)) { - throw UnableToLoadResource::with(static::class); - } + $this->configurations = $configurations; + } + public function getConfiguration() : Configuration + { $configMap = [ 'migrations_paths' => static function ($paths, Configuration $configuration) : void { foreach ($paths as $namespace => $path) { @@ -63,7 +63,7 @@ public function load($array) : Configuration ]; $object = new Configuration(); - self::applyConfigs($configMap, $object, $array); + self::applyConfigs($configMap, $object, $this->configurations); if ($object->getMetadataStorageConfiguration() === null) { $object->setMetadataStorageConfiguration(new TableMetadataStorageConfiguration()); diff --git a/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationFile.php b/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationFile.php new file mode 100644 index 0000000000..feeb9518a0 --- /dev/null +++ b/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationFile.php @@ -0,0 +1,35 @@ +file = $file; + } + + /** + * @param array $directories + * + * @return array + */ + final protected function getDirectoriesRelativeToFile(array $directories, string $file) : array + { + foreach ($directories as $ns => $dir) { + $path = realpath(dirname($file) . '/' . $dir); + + $directories[$ns] = $path !== false ? $path : $dir; + } + + return $directories; + } +} diff --git a/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationFileWithFallback.php b/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationFileWithFallback.php new file mode 100644 index 0000000000..9b8daec4e8 --- /dev/null +++ b/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationFileWithFallback.php @@ -0,0 +1,66 @@ +file = $file; + } + + public function getConfiguration() : Configuration + { + if ($this->file !== null) { + return $this->loadConfiguration($this->file); + } + + /** + * If no config has been provided, look for default config file in the path. + */ + $defaultFiles = [ + 'migrations.xml', + 'migrations.yml', + 'migrations.yaml', + 'migrations.json', + 'migrations.php', + ]; + + foreach ($defaultFiles as $file) { + if ($this->configurationFileExists($file)) { + return $this->loadConfiguration($file); + } + } + + throw MissingConfigurationFile::new(); + } + + private function configurationFileExists(string $config) : bool + { + return file_exists($config); + } + + /** + * @throws FileTypeNotSupported + */ + private function loadConfiguration(string $file) : Configuration + { + return (new FormattedFile($file))->getConfiguration(); + } +} diff --git a/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationLoader.php b/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationLoader.php new file mode 100644 index 0000000000..680e603465 --- /dev/null +++ b/lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationLoader.php @@ -0,0 +1,12 @@ +configurations = $configurations; + } + + public function getConfiguration() : Configuration + { + return $this->configurations; + } +} diff --git a/lib/Doctrine/Migrations/Configuration/Configuration/FormattedFile.php b/lib/Doctrine/Migrations/Configuration/Configuration/FormattedFile.php new file mode 100644 index 0000000000..8c5696a7aa --- /dev/null +++ b/lib/Doctrine/Migrations/Configuration/Configuration/FormattedFile.php @@ -0,0 +1,54 @@ +loaders = [ + 'json' => static function ($file) : ConfigurationLoader { + return new JsonFile($file); + }, + 'php' => static function ($file) : ConfigurationLoader { + return new PhpFile($file); + }, + 'xml' => static function ($file) : ConfigurationLoader { + return new XmlFile($file); + }, + 'yaml' => static function ($file) : ConfigurationLoader { + return new YamlFile($file); + }, + 'yml' => static function ($file) : ConfigurationLoader { + return new YamlFile($file); + }, + ]; + } + + public function getConfiguration() : Configuration + { + if (count($this->loaders) === 0) { + $this->setDefaultLoaders(); + } + + $extension = pathinfo($this->file, PATHINFO_EXTENSION); + if (! isset($this->loaders[$extension])) { + throw Configuration\Exception\InvalidConfigurationFormat::new(); + } + + return $this->loaders[$extension]($this->file)->getConfiguration(); + } +} diff --git a/lib/Doctrine/Migrations/Configuration/Configuration/JsonFile.php b/lib/Doctrine/Migrations/Configuration/Configuration/JsonFile.php new file mode 100644 index 0000000000..49dcb749a8 --- /dev/null +++ b/lib/Doctrine/Migrations/Configuration/Configuration/JsonFile.php @@ -0,0 +1,44 @@ +file)) { + throw FileNotFound::new(); + } + + $contents = file_get_contents($this->file); + + assert($contents !== false); + + $config = json_decode($contents, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + throw JsonNotValid::new(); + } + + if (isset($config['migrations_paths'])) { + $config['migrations_paths'] = $this->getDirectoriesRelativeToFile( + $config['migrations_paths'], + $this->file + ); + } + + return (new ConfigurationArray($config))->getConfiguration(); + } +} diff --git a/lib/Doctrine/Migrations/Configuration/Configuration/PhpFile.php b/lib/Doctrine/Migrations/Configuration/Configuration/PhpFile.php new file mode 100644 index 0000000000..1c379233e3 --- /dev/null +++ b/lib/Doctrine/Migrations/Configuration/Configuration/PhpFile.php @@ -0,0 +1,35 @@ +file)) { + throw FileNotFound::new(); + } + $config = require $this->file; + if ($config instanceof Configuration) { + return $config; + } + + assert(is_array($config)); + if (isset($config['migrations_paths'])) { + $config['migrations_paths'] = $this->getDirectoriesRelativeToFile( + $config['migrations_paths'], + $this->file + ); + } + + return (new ConfigurationArray($config))->getConfiguration(); + } +} diff --git a/lib/Doctrine/Migrations/Configuration/Loader/XML/configuration.xsd b/lib/Doctrine/Migrations/Configuration/Configuration/XML/configuration.xsd similarity index 100% rename from lib/Doctrine/Migrations/Configuration/Loader/XML/configuration.xsd rename to lib/Doctrine/Migrations/Configuration/Configuration/XML/configuration.xsd diff --git a/lib/Doctrine/Migrations/Configuration/Loader/XmlFileLoader.php b/lib/Doctrine/Migrations/Configuration/Configuration/XmlFile.php similarity index 80% rename from lib/Doctrine/Migrations/Configuration/Loader/XmlFileLoader.php rename to lib/Doctrine/Migrations/Configuration/Configuration/XmlFile.php index dabcf03f60..ba87744d7e 100644 --- a/lib/Doctrine/Migrations/Configuration/Loader/XmlFileLoader.php +++ b/lib/Doctrine/Migrations/Configuration/Configuration/XmlFile.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Doctrine\Migrations\Configuration\Loader; +namespace Doctrine\Migrations\Configuration\Configuration; use Doctrine\Migrations\Configuration\Configuration; +use Doctrine\Migrations\Configuration\Configuration\Exception\XmlNotValid; use Doctrine\Migrations\Configuration\Exception\FileNotFound; -use Doctrine\Migrations\Configuration\Exception\XmlNotValid; use Doctrine\Migrations\Tools\BooleanStringFormatter; use DOMDocument; use SimpleXMLElement; @@ -20,31 +20,17 @@ use function simplexml_load_string; use function strtr; -/** - * @internal - */ -final class XmlFileLoader extends AbstractFileLoader +final class XmlFile extends ConfigurationFile { - /** @var ArrayLoader */ - private $arrayLoader; - - public function __construct() - { - $this->arrayLoader = new ArrayLoader(); - } - - /** - * @param mixed|string $file - */ - public function load($file) : Configuration + public function getConfiguration() : Configuration { - if (! file_exists($file)) { + if (! file_exists($this->file)) { throw FileNotFound::new(); } - $this->validateXml($file); + $this->validateXml($this->file); - $rawXML = file_get_contents($file); + $rawXML = file_get_contents($this->file); assert($rawXML !== false); $root = simplexml_load_string($rawXML, SimpleXMLElement::class, LIBXML_NOCDATA); @@ -59,13 +45,13 @@ public function load($file) : Configuration ); } if (isset($config['migrations_paths'])) { - $config['migrations_paths'] = $this->getDirectoryRelativeToFile( - $file, - $config['migrations_paths'] + $config['migrations_paths'] = $this->getDirectoriesRelativeToFile( + $config['migrations_paths'], + $this->file ); } - return $this->arrayLoader->load($config); + return (new ConfigurationArray($config))->getConfiguration(); } /** diff --git a/lib/Doctrine/Migrations/Configuration/Loader/YamlFileLoader.php b/lib/Doctrine/Migrations/Configuration/Configuration/YamlFile.php similarity index 53% rename from lib/Doctrine/Migrations/Configuration/Loader/YamlFileLoader.php rename to lib/Doctrine/Migrations/Configuration/Configuration/YamlFile.php index b75e64c340..5d756cc647 100644 --- a/lib/Doctrine/Migrations/Configuration/Loader/YamlFileLoader.php +++ b/lib/Doctrine/Migrations/Configuration/Configuration/YamlFile.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Doctrine\Migrations\Configuration\Loader; +namespace Doctrine\Migrations\Configuration\Configuration; use Doctrine\Migrations\Configuration\Configuration; +use Doctrine\Migrations\Configuration\Configuration\Exception\YamlNotAvailable; +use Doctrine\Migrations\Configuration\Configuration\Exception\YamlNotValid; use Doctrine\Migrations\Configuration\Exception\FileNotFound; -use Doctrine\Migrations\Configuration\Exception\YamlNotAvailable; -use Doctrine\Migrations\Configuration\Exception\YamlNotValid; use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Yaml; use function assert; @@ -16,33 +16,19 @@ use function file_get_contents; use function is_array; -/** - * @internal - */ -final class YamlFileLoader extends AbstractFileLoader +final class YamlFile extends ConfigurationFile { - /** @var ArrayLoader */ - private $arrayLoader; - - public function __construct() - { - $this->arrayLoader = new ArrayLoader(); - } - - /** - * @param mixed $file - */ - public function load($file) : Configuration + public function getConfiguration() : Configuration { if (! class_exists(Yaml::class)) { throw YamlNotAvailable::new(); } - if (! file_exists($file)) { + if (! file_exists($this->file)) { throw FileNotFound::new(); } - $content = file_get_contents($file); + $content = file_get_contents($this->file); assert($content !== false); @@ -57,12 +43,12 @@ public function load($file) : Configuration } if (isset($config['migrations_paths'])) { - $config['migrations_paths'] = $this->getDirectoryRelativeToFile( - $file, - $config['migrations_paths'] + $config['migrations_paths'] = $this->getDirectoriesRelativeToFile( + $config['migrations_paths'], + $this->file ); } - return $this->arrayLoader->load($config); + return (new ConfigurationArray($config))->getConfiguration(); } } diff --git a/lib/Doctrine/Migrations/Configuration/ConfigurationLoader.php b/lib/Doctrine/Migrations/Configuration/ConfigurationLoader.php deleted file mode 100644 index 0e15127df4..0000000000 --- a/lib/Doctrine/Migrations/Configuration/ConfigurationLoader.php +++ /dev/null @@ -1,53 +0,0 @@ -loaders[$type] = $loader; - } - - private function setDefaultLoaders() : void - { - $this->loaders = [ - 'array' => new ArrayLoader(), - 'json' => new JsonFileLoader(), - 'php' => new PhpFileLoader(), - 'xml' => new XmlFileLoader(), - 'yaml' => new YamlFileLoader(), - 'yml' => new YamlFileLoader(), - ]; - } - - public function getLoader(string $type) : Loader - { - if (count($this->loaders) === 0) { - $this->setDefaultLoaders(); - } - - if (! isset($this->loaders[$type])) { - throw UnknownLoader::new($type); - } - - return $this->loaders[$type]; - } -} diff --git a/lib/Doctrine/Migrations/Configuration/Exception/FileNotFound.php b/lib/Doctrine/Migrations/Configuration/Exception/FileNotFound.php index 1577f1051c..d7e5a9f52d 100644 --- a/lib/Doctrine/Migrations/Configuration/Exception/FileNotFound.php +++ b/lib/Doctrine/Migrations/Configuration/Exception/FileNotFound.php @@ -10,6 +10,6 @@ final class FileNotFound extends InvalidArgumentException implements Configurati { public static function new() : self { - return new self('Given config file does not exist'); + return new self('Given configuration file does not exist'); } } diff --git a/lib/Doctrine/Migrations/Configuration/Exception/UnableToLoadResource.php b/lib/Doctrine/Migrations/Configuration/Exception/UnableToLoadResource.php deleted file mode 100644 index af36504e2d..0000000000 --- a/lib/Doctrine/Migrations/Configuration/Exception/UnableToLoadResource.php +++ /dev/null @@ -1,16 +0,0 @@ - $input - * - * @return array - */ - final protected function getDirectoryRelativeToFile(string $file, array $input) : array - { - foreach ($input as $ns => $dir) { - $path = realpath(dirname($file) . '/' . $dir); - - $input[$ns] = $path !== false ? $path : $dir; - } - - return $input; - } -} diff --git a/lib/Doctrine/Migrations/Configuration/Loader/JsonFileLoader.php b/lib/Doctrine/Migrations/Configuration/Loader/JsonFileLoader.php deleted file mode 100644 index 33946685cb..0000000000 --- a/lib/Doctrine/Migrations/Configuration/Loader/JsonFileLoader.php +++ /dev/null @@ -1,58 +0,0 @@ -arrayLoader = new ArrayLoader(); - } - - /** - * @param mixed $file - */ - public function load($file) : Configuration - { - if (! file_exists($file)) { - throw FileNotFound::new(); - } - - $contents = file_get_contents($file); - - assert($contents !== false); - - $config = json_decode($contents, true); - - if (json_last_error() !== JSON_ERROR_NONE) { - throw JsonNotValid::new(); - } - - if (isset($config['migrations_paths'])) { - $config['migrations_paths'] = $this->getDirectoryRelativeToFile( - $file, - $config['migrations_paths'] - ); - } - - return $this->arrayLoader->load($config); - } -} diff --git a/lib/Doctrine/Migrations/Configuration/Loader/Loader.php b/lib/Doctrine/Migrations/Configuration/Loader/Loader.php deleted file mode 100644 index 80e6190ce8..0000000000 --- a/lib/Doctrine/Migrations/Configuration/Loader/Loader.php +++ /dev/null @@ -1,15 +0,0 @@ -arrayLoader = new ArrayLoader(); - } - - /** - * @param mixed $file - */ - public function load($file) : Configuration - { - if (! file_exists($file)) { - throw FileNotFound::new(); - } - $config = require $file; - if ($config instanceof Configuration) { - return $config; - } - - assert(is_array($config)); - if (isset($config['migrations_paths'])) { - $config['migrations_paths'] = $this->getDirectoryRelativeToFile( - $file, - $config['migrations_paths'] - ); - } - - return $this->arrayLoader->load($config); - } -} diff --git a/lib/Doctrine/Migrations/Tools/Console/Helper/MigrationsConfigurationHelper.php b/lib/Doctrine/Migrations/Tools/Console/Helper/MigrationsConfigurationHelper.php deleted file mode 100644 index 0c317a8503..0000000000 --- a/lib/Doctrine/Migrations/Tools/Console/Helper/MigrationsConfigurationHelper.php +++ /dev/null @@ -1,90 +0,0 @@ -loader = $loader ?: new ConfigurationLoader(); - } - - public function getConfiguration(InputInterface $input) : Configuration - { - /** - * If a configuration option is passed to the command line, use that configuration - * instead of any other one. - */ - $configurationFile = $input->getOption('configuration'); - - if ($configurationFile !== null && is_string($configurationFile)) { - return $this->loadConfig($configurationFile); - } - - /** - * If no any other config has been found, look for default config file in the path. - */ - $defaultConfig = [ - 'migrations.xml', - 'migrations.yml', - 'migrations.yaml', - 'migrations.json', - 'migrations.php', - ]; - - foreach ($defaultConfig as $config) { - if ($this->configExists($config)) { - return $this->loadConfig($config); - } - } - - return $this->loader->getLoader('array')->load([]); - } - - private function configExists(string $config) : bool - { - return file_exists($config); - } - - /** - * @throws FileTypeNotSupported - */ - private function loadConfig(string $configFile) : Configuration - { - $extension = pathinfo($configFile, PATHINFO_EXTENSION); - - try { - return $this->loader->getLoader($extension)->load($configFile); - } catch (UnknownLoader $e) { - throw FileTypeNotSupported::new(); - } - } - - /** - * {@inheritdoc} - */ - public function getName() : string - { - return 'configuration'; - } -} diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 40660c1334..066e88f29b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -14,7 +14,7 @@ parameters: - '~Variable method call on Doctrine\\Migrations\\AbstractMigration~' - message: '~^Variable property access on mixed\.$~' - path: %currentWorkingDirectory%/lib/Doctrine/Migrations/Configuration/Loader/XmlFileLoader.php + path: %currentWorkingDirectory%/lib/Doctrine/Migrations/Configuration/Configuration/XmlFile.php - message: '~^Call to function is_bool\(\) with bool will always evaluate to true\.$~' path: %currentWorkingDirectory%/lib/Doctrine/Migrations/InlineParameterFormatter.php diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Configuration/ArrayLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/ArrayLoaderTest.php new file mode 100644 index 0000000000..e8471a045c --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/ArrayLoaderTest.php @@ -0,0 +1,20 @@ +expectException(InvalidConfigurationKey::class); + $this->expectExceptionMessage('Migrations configuration key "foo" does not exist'); + $loader = new ConfigurationArray(['foo' => 'aaa']); + $loader->getConfiguration(); + } +} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Configuration/ConfigurationFileWithFallbackTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/ConfigurationFileWithFallbackTest.php new file mode 100644 index 0000000000..09971c40c2 --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/ConfigurationFileWithFallbackTest.php @@ -0,0 +1,51 @@ +getConfiguration(); + + self::assertSame('Doctrine Sandbox Migrations', $configuration->getName()); + } finally { + chdir($dir); + } + } + + public function testFileLoaderFallback() : void + { + $dir = getcwd() ?: '.'; + chdir(__DIR__ . '/../_files_loader'); + + try { + $loader = new ConfigurationFileWithFallback(); + $configuration = $loader->getConfiguration(); + + self::assertSame('Doctrine Sandbox Migrations FilesLoader', $configuration->getName()); + } finally { + chdir($dir); + } + } + + public function testMissingConfig() : void + { + $this->expectException(MissingConfigurationFile::class); + $loader = new ConfigurationFileWithFallback(); + $configuration = $loader->getConfiguration(); + } +} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Configuration/FormattedFileTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/FormattedFileTest.php new file mode 100644 index 0000000000..138e7e8fc8 --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/FormattedFileTest.php @@ -0,0 +1,20 @@ +expectException(InvalidConfigurationFormat::class); + + $loader = new FormattedFile('migrations.abc'); + $loader->getConfiguration(); + } +} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Configuration/JsonLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/JsonLoaderTest.php new file mode 100644 index 0000000000..91b1e0489d --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/JsonLoaderTest.php @@ -0,0 +1,24 @@ +getConfiguration(); + } + + public function testMalformed() : void + { + $this->expectException(Configuration\Exception\JsonNotValid::class); + $this->load('malformed'); + } +} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Loader/AbstractLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/LoaderTest.php similarity index 95% rename from tests/Doctrine/Migrations/Tests/Configuration/Loader/AbstractLoaderTest.php rename to tests/Doctrine/Migrations/Tests/Configuration/Configuration/LoaderTest.php index 65ce44184b..e9bf96b91d 100644 --- a/tests/Doctrine/Migrations/Tests/Configuration/Loader/AbstractLoaderTest.php +++ b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/LoaderTest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Doctrine\Migrations\Tests\Configuration\Loader; +namespace Doctrine\Migrations\Tests\Configuration\Configuration; use Doctrine\Migrations\Configuration\Configuration; -use Doctrine\Migrations\Configuration\Exception\InvalidConfigurationKey; +use Doctrine\Migrations\Configuration\Configuration\Exception\InvalidConfigurationKey; use Doctrine\Migrations\Exception\MigrationException; use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration; use InvalidArgumentException; @@ -13,7 +13,7 @@ use const DIRECTORY_SEPARATOR; use function dirname; -abstract class AbstractLoaderTest extends TestCase +abstract class LoaderTest extends TestCase { abstract public function load(string $prefix = '') : Configuration; diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Loader/PhpLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/PhpLoaderTest.php similarity index 50% rename from tests/Doctrine/Migrations/Tests/Configuration/Loader/PhpLoaderTest.php rename to tests/Doctrine/Migrations/Tests/Configuration/Configuration/PhpLoaderTest.php index f76cc622c8..9a00286dac 100644 --- a/tests/Doctrine/Migrations/Tests/Configuration/Loader/PhpLoaderTest.php +++ b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/PhpLoaderTest.php @@ -2,18 +2,18 @@ declare(strict_types=1); -namespace Doctrine\Migrations\Tests\Configuration\Loader; +namespace Doctrine\Migrations\Tests\Configuration\Configuration; use Doctrine\Migrations\Configuration\Configuration; -use Doctrine\Migrations\Configuration\Loader\PhpFileLoader; +use Doctrine\Migrations\Configuration\Configuration\PhpFile; -class PhpLoaderTest extends AbstractLoaderTest +class PhpLoaderTest extends LoaderTest { public function load(string $prefix = '') : Configuration { - $loader = new PhpFileLoader(); + $loader = new PhpFile(__DIR__ . '/../_files/config' . ($prefix!==''? ('_' . $prefix) : '') . '.php'); - return $loader->load(__DIR__ . '/../_files/config' . ($prefix!==''? ('_' . $prefix) : '') . '.php'); + return $loader->getConfiguration(); } public function testLoadInline() : void diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Configuration/XmlLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/XmlLoaderTest.php new file mode 100644 index 0000000000..e0dfbd1b90 --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/XmlLoaderTest.php @@ -0,0 +1,32 @@ +getConfiguration(); + } + + public function testConfigurationWithInvalidOption() : void + { + $this->expectException(Configuration\Exception\XmlNotValid::class); + + $this->load('invalid'); + } + + public function testMalformed() : void + { + $this->expectException(Configuration\Exception\XmlNotValid::class); + + $this->load('malformed'); + } +} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Configuration/YamlLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/YamlLoaderTest.php new file mode 100644 index 0000000000..6ba6f8746b --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Configuration/Configuration/YamlLoaderTest.php @@ -0,0 +1,25 @@ +getConfiguration(); + } + + public function testMalformed() : void + { + $this->expectException(Configuration\Exception\YamlNotValid::class); + + $this->load('malformed'); + } +} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationLoaderTest.php deleted file mode 100644 index 05b21af59a..0000000000 --- a/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationLoaderTest.php +++ /dev/null @@ -1,57 +0,0 @@ -loader = new ConfigurationLoader(); - } - - public function testAdd() : void - { - $loader = $this->createMock(Loader::class); - $this->loader->addLoader('foo', $loader); - - self::assertSame($loader, $this->loader->getLoader('foo')); - } - - public function testUnknownLoader() : void - { - $this->expectException(UnknownLoader::class); - $this->expectExceptionMessage('Unknown configuration loader "foo".'); - $this->loader->getLoader('foo'); - } - - public function testDefaults() : void - { - $defaults = [ - 'array' => ArrayLoader::class, - 'xml' => XmlFileLoader::class, - 'yaml' => YamlFileLoader::class, - 'yml' => YamlFileLoader::class, - 'php' => PhpFileLoader::class, - 'json' => JsonFileLoader::class, - ]; - - foreach ($defaults as $name => $class) { - self::assertInstanceOf($class, $this->loader->getLoader($name)); - } - } -} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Loader/ArrayLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Loader/ArrayLoaderTest.php deleted file mode 100644 index 047c2fca02..0000000000 --- a/tests/Doctrine/Migrations/Tests/Configuration/Loader/ArrayLoaderTest.php +++ /dev/null @@ -1,37 +0,0 @@ -expectException(UnableToLoadResource::class); - $this->expectExceptionMessage('The provided resource can not be loaded by the loader "Doctrine\Migrations\Configuration\Loader\ArrayLoader".'); - $loader = new ArrayLoader(); - $loader->load(null); - } - - public function testInvalidKey() : void - { - $this->expectException(InvalidConfigurationKey::class); - $this->expectExceptionMessage('Migrations configuration key "foo" does not exist'); - $loader = new ArrayLoader(); - $loader->load(['foo' => 'aaa']); - } - - public function testInvalidKeyInteger() : void - { - $this->expectException(InvalidConfigurationKey::class); - $this->expectExceptionMessage('Migrations configuration key "0" does not exist.'); - $loader = new ArrayLoader(); - $loader->load(['aaa']); - } -} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Loader/JsonLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Loader/JsonLoaderTest.php deleted file mode 100644 index 323fe49ef0..0000000000 --- a/tests/Doctrine/Migrations/Tests/Configuration/Loader/JsonLoaderTest.php +++ /dev/null @@ -1,25 +0,0 @@ -load(__DIR__ . '/../_files/config' . ($prefix!==''? ('_' . $prefix) : '') . '.json'); - } - - public function testMalformed() : void - { - $this->expectException(JsonNotValid::class); - $this->load('malformed'); - } -} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Loader/XmlLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Loader/XmlLoaderTest.php deleted file mode 100644 index 4be6592830..0000000000 --- a/tests/Doctrine/Migrations/Tests/Configuration/Loader/XmlLoaderTest.php +++ /dev/null @@ -1,33 +0,0 @@ -load(__DIR__ . '/../_files/config' . ($prefix!==''? ('_' . $prefix) : '') . '.xml'); - } - - public function testConfigurationWithInvalidOption() : void - { - $this->expectException(XmlNotValid::class); - - $this->load('invalid'); - } - - public function testMalformed() : void - { - $this->expectException(XmlNotValid::class); - - $this->load('malformed'); - } -} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Loader/YamlLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Loader/YamlLoaderTest.php deleted file mode 100644 index 4c8548a100..0000000000 --- a/tests/Doctrine/Migrations/Tests/Configuration/Loader/YamlLoaderTest.php +++ /dev/null @@ -1,26 +0,0 @@ -load(__DIR__ . '/../_files/config' . ($prefix!==''? ('_' . $prefix) : '') . '.yml'); - } - - public function testMalformed() : void - { - $this->expectException(YamlNotValid::class); - - $this->load('malformed'); - } -} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/_files_loader/migrations.php b/tests/Doctrine/Migrations/Tests/Configuration/_files_loader/migrations.php new file mode 100644 index 0000000000..b3558dbd1f --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Configuration/_files_loader/migrations.php @@ -0,0 +1,9 @@ + 'Doctrine Sandbox Migrations FilesLoader', + 'migrations_paths' => ['DoctrineMigrationsTest' => '.'], + +];