From 28572a81c20ffe36a8eb6754f0ad0f13670ebae3 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sun, 29 Dec 2019 11:13:14 +0100 Subject: [PATCH 1/2] drop helperset usage in favor of the DependencyFactory --- bin/doctrine-migrations.php | 23 +-- .../ConfigurationFormatLoader.php | 53 ++++++ .../Configuration/ConfigurationLoader.php | 76 +++++---- .../Connection/ConfiguredConnectionLoader.php | 30 ++++ .../Connection/ConnectionLoader.php | 2 +- .../Loader/ConnectionHelperLoader.php | 51 ------ .../Exception/ConnectionNotSpecified.php | 4 +- .../Connection/Loader/NoConnectionLoader.php | 2 +- lib/Doctrine/Migrations/DependencyFactory.php | 5 + .../Tools/Console/Command/DoctrineCommand.php | 61 ++++--- .../Tools/Console/ConnectionLoader.php | 34 ---- .../Tools/Console/ConsoleRunner.php | 35 ++-- .../Helper/MigrationsConfigurationHelper.php | 90 ---------- .../ConfigurationFormatLoaderTest.php | 57 +++++++ .../Configuration/ConfigurationLoaderTest.php | 133 +++++++++++---- .../Connection/ConnectionLoaderTest.php | 58 +++++++ .../Loader/ConnectionLoaderTest.php | 29 +--- .../Connection}/_files/migrations-db.php | 0 .../Connection}/_files/sqlite-connection.php | 0 .../_files_loader}/config.php | 0 .../_files_loader}/migrations.php | 0 .../Tools/Console/ConnectionLoaderTest.php | 87 ---------- .../Tests/Tools/Console/ConsoleRunnerTest.php | 50 +++--- .../Helper/ConfigurationHelperTest.php | 159 ------------------ .../Tests/doctrine-migrations-phpstan-app.php | 8 +- 25 files changed, 440 insertions(+), 607 deletions(-) create mode 100644 lib/Doctrine/Migrations/Configuration/ConfigurationFormatLoader.php create mode 100644 lib/Doctrine/Migrations/Configuration/Connection/ConfiguredConnectionLoader.php delete mode 100644 lib/Doctrine/Migrations/Configuration/Connection/Loader/ConnectionHelperLoader.php rename lib/Doctrine/Migrations/{Tools/Console => Configuration/Connection/Loader}/Exception/ConnectionNotSpecified.php (76%) delete mode 100644 lib/Doctrine/Migrations/Tools/Console/ConnectionLoader.php delete mode 100644 lib/Doctrine/Migrations/Tools/Console/Helper/MigrationsConfigurationHelper.php create mode 100644 tests/Doctrine/Migrations/Tests/Configuration/ConfigurationFormatLoaderTest.php create mode 100644 tests/Doctrine/Migrations/Tests/Configuration/Connection/ConnectionLoaderTest.php rename tests/Doctrine/Migrations/Tests/{Tools/Console => Configuration/Connection}/_files/migrations-db.php (100%) rename tests/Doctrine/Migrations/Tests/{Tools/Console => Configuration/Connection}/_files/sqlite-connection.php (100%) rename tests/Doctrine/Migrations/Tests/{Tools/Console/Helper/files => Configuration/_files_loader}/config.php (100%) rename tests/Doctrine/Migrations/Tests/{Tools/Console/Helper/files => Configuration/_files_loader}/migrations.php (100%) delete mode 100644 tests/Doctrine/Migrations/Tests/Tools/Console/ConnectionLoaderTest.php delete mode 100644 tests/Doctrine/Migrations/Tests/Tools/Console/Helper/ConfigurationHelperTest.php diff --git a/bin/doctrine-migrations.php b/bin/doctrine-migrations.php index e106154cea..f25834987a 100644 --- a/bin/doctrine-migrations.php +++ b/bin/doctrine-migrations.php @@ -6,8 +6,6 @@ use Doctrine\Migrations\Tools\Console\ConsoleRunner; use Phar; -use Symfony\Component\Console\Helper\HelperSet; -use Symfony\Component\Console\Helper\QuestionHelper; use const DIRECTORY_SEPARATOR; use const E_USER_ERROR; use const PHP_EOL; @@ -62,31 +60,14 @@ break; } - $helperSet = null; if ($configurationFile !== null) { if (! is_readable($configurationFile)) { trigger_error('Configuration file [' . $configurationFile . '] does not have read permission.', E_USER_ERROR); exit(1); } - $helperSet = require $configurationFile; - - if (! $helperSet instanceof HelperSet) { - foreach ($GLOBALS as $helperSetCandidate) { - if (! $helperSetCandidate instanceof HelperSet) { - continue; - } - - $helperSet = $helperSetCandidate; - break; - } - } + require $configurationFile; } - $helperSet = $helperSet ?? new HelperSet(); - $helperSet->set(new QuestionHelper(), 'question'); - - $commands = []; - - ConsoleRunner::run($helperSet, $commands); + ConsoleRunner::run(); })(); diff --git a/lib/Doctrine/Migrations/Configuration/ConfigurationFormatLoader.php b/lib/Doctrine/Migrations/Configuration/ConfigurationFormatLoader.php new file mode 100644 index 0000000000..332c5a7d71 --- /dev/null +++ b/lib/Doctrine/Migrations/Configuration/ConfigurationFormatLoader.php @@ -0,0 +1,53 @@ +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/ConfigurationLoader.php b/lib/Doctrine/Migrations/Configuration/ConfigurationLoader.php index 0e15127df4..4fe5628481 100644 --- a/lib/Doctrine/Migrations/Configuration/ConfigurationLoader.php +++ b/lib/Doctrine/Migrations/Configuration/ConfigurationLoader.php @@ -5,49 +5,67 @@ namespace Doctrine\Migrations\Configuration; use Doctrine\Migrations\Configuration\Exception\UnknownLoader; -use Doctrine\Migrations\Configuration\Loader\ArrayLoader; -use Doctrine\Migrations\Configuration\Loader\JsonFileLoader; -use Doctrine\Migrations\Configuration\Loader\Loader; -use Doctrine\Migrations\Configuration\Loader\PhpFileLoader; -use Doctrine\Migrations\Configuration\Loader\XmlFileLoader; -use Doctrine\Migrations\Configuration\Loader\YamlFileLoader; -use function count; +use Doctrine\Migrations\Tools\Console\Exception\FileTypeNotSupported; +use const PATHINFO_EXTENSION; +use function file_exists; +use function pathinfo; /** - * @internal + * The ConfigurationLoader class is responsible for getting the Configuration instance from one of the supported methods + * for defining the configuration for your migrations. */ -class ConfigurationLoader +final class ConfigurationLoader { - /** @var Loader[] */ - private $loaders = []; + /** @var ConfigurationFormatLoader */ + private $loader; - public function addLoader(string $type, Loader $loader) : void + public function __construct(?ConfigurationFormatLoader $loader = null) { - $this->loaders[$type] = $loader; + $this->loader = $loader ?: new ConfigurationFormatLoader(); } - private function setDefaultLoaders() : void + public function getConfiguration(?string $configurationFile) : Configuration { - $this->loaders = [ - 'array' => new ArrayLoader(), - 'json' => new JsonFileLoader(), - 'php' => new PhpFileLoader(), - 'xml' => new XmlFileLoader(), - 'yaml' => new YamlFileLoader(), - 'yml' => new YamlFileLoader(), + if ($configurationFile !== null) { + 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([]); } - public function getLoader(string $type) : Loader + private function configExists(string $config) : bool { - if (count($this->loaders) === 0) { - $this->setDefaultLoaders(); - } + return file_exists($config); + } - if (! isset($this->loaders[$type])) { - throw UnknownLoader::new($type); - } + /** + * @throws FileTypeNotSupported + */ + private function loadConfig(string $configFile) : Configuration + { + $extension = pathinfo($configFile, PATHINFO_EXTENSION); - return $this->loaders[$type]; + try { + return $this->loader->getLoader($extension)->load($configFile); + } catch (UnknownLoader $e) { + throw FileTypeNotSupported::new(); + } } } diff --git a/lib/Doctrine/Migrations/Configuration/Connection/ConfiguredConnectionLoader.php b/lib/Doctrine/Migrations/Configuration/Connection/ConfiguredConnectionLoader.php new file mode 100644 index 0000000000..4e09f3bfde --- /dev/null +++ b/lib/Doctrine/Migrations/Configuration/Connection/ConfiguredConnectionLoader.php @@ -0,0 +1,30 @@ +dbConfig = $dbConfig; + } + + public function getConnection() : Connection + { + $loader = new ArrayConnectionConfigurationLoader( + $this->dbConfig ?: 'migrations-db.php', + new NoConnectionLoader() + ); + + return $loader->getConnection(); + } +} diff --git a/lib/Doctrine/Migrations/Configuration/Connection/ConnectionLoader.php b/lib/Doctrine/Migrations/Configuration/Connection/ConnectionLoader.php index e1699a0ea7..33974838ff 100644 --- a/lib/Doctrine/Migrations/Configuration/Connection/ConnectionLoader.php +++ b/lib/Doctrine/Migrations/Configuration/Connection/ConnectionLoader.php @@ -5,7 +5,7 @@ namespace Doctrine\Migrations\Configuration\Connection; use Doctrine\DBAL\Connection; -use Doctrine\Migrations\Tools\Console\Exception\ConnectionNotSpecified; +use Doctrine\Migrations\Configuration\Connection\Loader\Exception\ConnectionNotSpecified; /** * The ConnectionLoader defines the interface used to load the Doctrine\DBAL\Connection instance to use diff --git a/lib/Doctrine/Migrations/Configuration/Connection/Loader/ConnectionHelperLoader.php b/lib/Doctrine/Migrations/Configuration/Connection/Loader/ConnectionHelperLoader.php deleted file mode 100644 index 82073afca5..0000000000 --- a/lib/Doctrine/Migrations/Configuration/Connection/Loader/ConnectionHelperLoader.php +++ /dev/null @@ -1,51 +0,0 @@ -helperSet = $helperSet ?: new HelperSet(); - $this->helperName = $helperName; - $this->fallback = $fallback; - } - - /** - * Read the input and return a Configuration, returns null if the config - * is not supported. - */ - public function getConnection() : Connection - { - if ($this->helperSet->has($this->helperName)) { - $connectionHelper = $this->helperSet->get($this->helperName); - - if ($connectionHelper instanceof ConnectionHelper) { - return $connectionHelper->getConnection(); - } - } - - return $this->fallback->getConnection(); - } -} diff --git a/lib/Doctrine/Migrations/Tools/Console/Exception/ConnectionNotSpecified.php b/lib/Doctrine/Migrations/Configuration/Connection/Loader/Exception/ConnectionNotSpecified.php similarity index 76% rename from lib/Doctrine/Migrations/Tools/Console/Exception/ConnectionNotSpecified.php rename to lib/Doctrine/Migrations/Configuration/Connection/Loader/Exception/ConnectionNotSpecified.php index 098c841efc..ea8ce04d7d 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Exception/ConnectionNotSpecified.php +++ b/lib/Doctrine/Migrations/Configuration/Connection/Loader/Exception/ConnectionNotSpecified.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Doctrine\Migrations\Tools\Console\Exception; +namespace Doctrine\Migrations\Configuration\Connection\Loader\Exception; use InvalidArgumentException; -final class ConnectionNotSpecified extends InvalidArgumentException implements ConsoleException +final class ConnectionNotSpecified extends InvalidArgumentException implements LoaderException { public static function new() : self { diff --git a/lib/Doctrine/Migrations/Configuration/Connection/Loader/NoConnectionLoader.php b/lib/Doctrine/Migrations/Configuration/Connection/Loader/NoConnectionLoader.php index bcc57f9299..08c9974f16 100644 --- a/lib/Doctrine/Migrations/Configuration/Connection/Loader/NoConnectionLoader.php +++ b/lib/Doctrine/Migrations/Configuration/Connection/Loader/NoConnectionLoader.php @@ -6,7 +6,7 @@ use Doctrine\DBAL\Connection; use Doctrine\Migrations\Configuration\Connection\ConnectionLoader; -use Doctrine\Migrations\Tools\Console\Exception\ConnectionNotSpecified; +use Doctrine\Migrations\Configuration\Connection\Loader\Exception\ConnectionNotSpecified; /** * @internal diff --git a/lib/Doctrine/Migrations/DependencyFactory.php b/lib/Doctrine/Migrations/DependencyFactory.php index cb0a01b9f7..f48a877712 100644 --- a/lib/Doctrine/Migrations/DependencyFactory.php +++ b/lib/Doctrine/Migrations/DependencyFactory.php @@ -77,6 +77,11 @@ public function __construct(Configuration $configuration, Connection $connection $this->em = $em; } + public function isFrozen() : bool + { + return $this->frozen; + } + public function freeze() : void { $this->frozen = true; diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php index 5406addbe8..671bc5a06e 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php @@ -4,15 +4,16 @@ namespace Doctrine\Migrations\Tools\Console\Command; +use Doctrine\DBAL\Connection; +use Doctrine\Migrations\Configuration\Configuration; +use Doctrine\Migrations\Configuration\ConfigurationLoader; +use Doctrine\Migrations\Configuration\Connection\Loader\ArrayConnectionConfigurationLoader; +use Doctrine\Migrations\Configuration\Connection\Loader\NoConnectionLoader; use Doctrine\Migrations\DependencyFactory; -use Doctrine\Migrations\Tools\Console\ConnectionLoader; use Doctrine\Migrations\Tools\Console\ConsoleLogger; use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied; -use Doctrine\Migrations\Tools\Console\Helper\ConfigurationHelper; -use Doctrine\Migrations\Tools\Console\Helper\MigrationsConfigurationHelper; -use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; +use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -68,35 +69,22 @@ protected function outputHeader( protected function initialize(InputInterface $input, OutputInterface $output) : void { - if ($this->dependencyFactory !== null) { - $this->dependencyFactory->freeze(); - - return; - } + if ($this->dependencyFactory === null) { + $config = is_string($input->getOption('configuration')) ? $input->getOption('configuration'): null; + $configuration = self::getConfiguration($config); - $helperSet = $this->getHelperSet() ?: new HelperSet(); + $dbConfig = is_string($input->getOption('db-configuration')) ? $input->getOption('db-configuration'): null; + $connection = self::getConnection($dbConfig); - if ($helperSet->has('configuration') && $helperSet->get('configuration') instanceof ConfigurationHelper) { - /** @var MigrationsConfigurationHelper $configHelper */ - $configHelper = $helperSet->get('configuration'); - } else { - $configHelper = new MigrationsConfigurationHelper(); + $this->dependencyFactory = new DependencyFactory($configuration, $connection); } - $configuration = $configHelper->getConfiguration($input); - - $dbConfig = is_string($input->getOption('db-configuration')) ? $input->getOption('db-configuration'): null; - - $connection = (new ConnectionLoader()) - ->getConnection($dbConfig, $helperSet); - - $em = null; - if ($helperSet->has('em') && $helperSet->get('em') instanceof EntityManagerHelper) { - $em = $helperSet->get('em')->getEntityManager(); + if ($this->dependencyFactory->isFrozen()) { + return; } - $logger = new ConsoleLogger($output); - $this->dependencyFactory = new DependencyFactory($configuration, $connection, $em, $logger); + $logger = new ConsoleLogger($output); + $this->dependencyFactory->setService(LoggerInterface::class, $logger); $this->dependencyFactory->freeze(); } @@ -133,4 +121,21 @@ protected function procOpen(string $editorCommand, string $path) : void { proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes); } + + private static function getConnection(?string $dbConfig) : Connection + { + $loader = new ArrayConnectionConfigurationLoader( + $dbConfig ?: 'migrations-db.php', + new NoConnectionLoader() + ); + + return $loader->getConnection(); + } + + private static function getConfiguration(?string $config) : Configuration + { + $configurationLoader = new ConfigurationLoader(); + + return $configurationLoader->getConfiguration($config); + } } diff --git a/lib/Doctrine/Migrations/Tools/Console/ConnectionLoader.php b/lib/Doctrine/Migrations/Tools/Console/ConnectionLoader.php deleted file mode 100644 index b2652aed61..0000000000 --- a/lib/Doctrine/Migrations/Tools/Console/ConnectionLoader.php +++ /dev/null @@ -1,34 +0,0 @@ -getConnection(); - } -} diff --git a/lib/Doctrine/Migrations/Tools/Console/ConsoleRunner.php b/lib/Doctrine/Migrations/Tools/Console/ConsoleRunner.php index 05e5bc80da..57d6365453 100644 --- a/lib/Doctrine/Migrations/Tools/Console/ConsoleRunner.php +++ b/lib/Doctrine/Migrations/Tools/Console/ConsoleRunner.php @@ -4,6 +4,7 @@ namespace Doctrine\Migrations\Tools\Console; +use Doctrine\Migrations\DependencyFactory; use Doctrine\Migrations\Tools\Console\Command\DiffCommand; use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand; use Doctrine\Migrations\Tools\Console\Command\DumpSchemaCommand; @@ -17,7 +18,6 @@ use Doctrine\Migrations\Tools\Console\Command\VersionCommand; use PackageVersions\Versions; use Symfony\Component\Console\Application; -use Symfony\Component\Console\Helper\HelperSet; /** * The ConsoleRunner class is used to create the Symfony Console application for the Doctrine Migrations console. @@ -29,42 +29,41 @@ class ConsoleRunner { /** @param DoctrineCommand[] $commands */ - public static function run(HelperSet $helperSet, array $commands = []) : void + public static function run(array $commands = [], ?DependencyFactory $dependencyFactory = null) : void { - $cli = static::createApplication($helperSet, $commands); + $cli = static::createApplication($commands, $dependencyFactory); $cli->run(); } /** @param DoctrineCommand[] $commands */ - public static function createApplication(HelperSet $helperSet, array $commands = []) : Application + public static function createApplication(array $commands = [], ?DependencyFactory $dependencyFactory = null) : Application { $cli = new Application('Doctrine Migrations', Versions::getVersion('doctrine/migrations')); $cli->setCatchExceptions(true); - $cli->setHelperSet($helperSet); - self::addCommands($cli); + self::addCommands($cli, $dependencyFactory); $cli->addCommands($commands); return $cli; } - public static function addCommands(Application $cli) : void + public static function addCommands(Application $cli, ?DependencyFactory $dependencyFactory = null) : void { $cli->addCommands([ - new DumpSchemaCommand(), - new ExecuteCommand(), - new GenerateCommand(), - new LatestCommand(), - new MigrateCommand(), - new RollupCommand(), - new StatusCommand(), - new VersionCommand(), - new UpToDateCommand(), + new DumpSchemaCommand(null, $dependencyFactory), + new ExecuteCommand(null, $dependencyFactory), + new GenerateCommand(null, $dependencyFactory), + new LatestCommand(null, $dependencyFactory), + new MigrateCommand(null, $dependencyFactory), + new RollupCommand(null, $dependencyFactory), + new StatusCommand(null, $dependencyFactory), + new VersionCommand(null, $dependencyFactory), + new UpToDateCommand(null, $dependencyFactory), ]); - if (! $cli->getHelperSet()->has('em')) { + if ($dependencyFactory === null || $dependencyFactory->getEntityManager() === null) { return; } - $cli->add(new DiffCommand()); + $cli->add(new DiffCommand(null, $dependencyFactory)); } } 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/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationFormatLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationFormatLoaderTest.php new file mode 100644 index 0000000000..e8eaa8f3d6 --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationFormatLoaderTest.php @@ -0,0 +1,57 @@ +loader = new ConfigurationFormatLoader(); + } + + 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/ConfigurationLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationLoaderTest.php index 05b21af59a..99a01b3e05 100644 --- a/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationLoaderTest.php +++ b/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationLoaderTest.php @@ -4,54 +4,127 @@ namespace Doctrine\Migrations\Tests\Configuration; +use Doctrine\Migrations\Configuration\Configuration; +use Doctrine\Migrations\Configuration\ConfigurationFormatLoader; use Doctrine\Migrations\Configuration\ConfigurationLoader; use Doctrine\Migrations\Configuration\Exception\UnknownLoader; -use Doctrine\Migrations\Configuration\Loader\ArrayLoader; -use Doctrine\Migrations\Configuration\Loader\JsonFileLoader; use Doctrine\Migrations\Configuration\Loader\Loader; -use Doctrine\Migrations\Configuration\Loader\PhpFileLoader; -use Doctrine\Migrations\Configuration\Loader\XmlFileLoader; -use Doctrine\Migrations\Configuration\Loader\YamlFileLoader; -use PHPUnit\Framework\TestCase; +use Doctrine\Migrations\Tests\MigrationTestCase; +use InvalidArgumentException; +use PHPUnit\Framework\MockObject\MockObject; +use function chdir; +use function getcwd; -class ConfigurationLoaderTest extends TestCase +class ConfigurationLoaderTest extends MigrationTestCase { - /** @var ConfigurationLoader */ + /** @var MockObject */ private $loader; - public function setUp() : void + /** @var ConfigurationLoader */ + private $configurationLoader; + + protected function setUp() : void + { + $this->loader = $this->createMock(ConfigurationFormatLoader::class); + $this->configurationLoader = new ConfigurationLoader($this->loader); + } + + /** + * Test that unsupported file type throws exception + */ + public function testNoAvailableConfigGivesBackEmptyConfig() : void { - $this->loader = new ConfigurationLoader(); + $confExpected = new Configuration(); + + $configLoader = $this->createMock(Loader::class); + + $configLoader + ->expects(self::once()) + ->method('load') + ->with([]) + ->willReturn($confExpected); + + $this->loader + ->expects(self::once()) + ->method('getLoader') + ->with('array') + ->willReturn($configLoader); + + $dir = getcwd()?: '.'; + try { + chdir(__DIR__); + $this->configurationLoader->getConfiguration(null); + } finally { + chdir($dir); + } } - public function testAdd() : void + public function testConfigurationLoaderFailsToLoadOtherFormat() : void { - $loader = $this->createMock(Loader::class); - $this->loader->addLoader('foo', $loader); + $this->loader + ->expects(self::once()) + ->method('getLoader') + ->with('wrong') + ->willThrowException(UnknownLoader::new('dummy')); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Given config file type is not supported'); - self::assertSame($loader, $this->loader->getLoader('foo')); + $dir = getcwd()?: '.'; + try { + chdir(__DIR__); + $this->configurationLoader->getConfiguration('testconfig.wrong'); + } finally { + chdir($dir); + } } - public function testUnknownLoader() : void + public function testLoadsFile() : void { - $this->expectException(UnknownLoader::class); - $this->expectExceptionMessage('Unknown configuration loader "foo".'); - $this->loader->getLoader('foo'); + $confExpected = new Configuration(); + $configLoader = $this->createMock(Loader::class); + + $configLoader + ->expects(self::once()) + ->method('load') + ->with('config.php') + ->willReturn($confExpected); + + $this->loader + ->expects(self::once()) + ->method('getLoader') + ->with('php') + ->willReturn($configLoader); + + $config = $this->configurationLoader->getConfiguration('config.php'); + + self::assertSame($config, $confExpected); } - public function testDefaults() : void + public function testLoadsDefaultFile() : 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)); + $confExpected = new Configuration(); + $configLoader = $this->createMock(Loader::class); + + $configLoader + ->expects(self::once()) + ->method('load') + ->with('migrations.php') + ->willReturn($confExpected); + + $this->loader + ->expects(self::once()) + ->method('getLoader') + ->with('php') + ->willReturn($configLoader); + + $dir = getcwd()?: '.'; + try { + chdir(__DIR__ . '/_files_loader'); + $config = $this->configurationLoader->getConfiguration(null); + } finally { + chdir($dir); } + self::assertSame($config, $confExpected); } } diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Connection/ConnectionLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Connection/ConnectionLoaderTest.php new file mode 100644 index 0000000000..45f99ad487 --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Configuration/Connection/ConnectionLoaderTest.php @@ -0,0 +1,58 @@ +connectionLoader = new ConfiguredConnectionLoader('_files/sqlite-connection.php'); + try { + $conn = $this->connectionLoader->getConnection(); + self::assertInstanceOf(SqlitePlatform::class, $conn->getDatabasePlatform()); + } finally { + chdir($dir); + } + } + + public function testGetConnectionFromArrayNotFound() : void + { + $this->expectException(ConnectionNotSpecified::class); + + $dir = getcwd()?: '.'; + chdir(__DIR__); + $this->connectionLoader = new ConfiguredConnectionLoader(__DIR__ . '/_files/wrong.php'); + try { + $this->connectionLoader->getConnection(); + } finally { + chdir($dir); + } + } + + public function testGetConnectionFromArrayFromFallbackFile() : void + { + $dir = getcwd()?: '.'; + chdir(__DIR__ . '/_files'); + $this->connectionLoader = new ConfiguredConnectionLoader(null); + try { + $conn = $this->connectionLoader->getConnection(); + self::assertInstanceOf(SqlitePlatform::class, $conn->getDatabasePlatform()); + } finally { + chdir($dir); + } + } +} diff --git a/tests/Doctrine/Migrations/Tests/Configuration/Connection/Loader/ConnectionLoaderTest.php b/tests/Doctrine/Migrations/Tests/Configuration/Connection/Loader/ConnectionLoaderTest.php index 18060a60b6..d6fc10ed07 100644 --- a/tests/Doctrine/Migrations/Tests/Configuration/Connection/Loader/ConnectionLoaderTest.php +++ b/tests/Doctrine/Migrations/Tests/Configuration/Connection/Loader/ConnectionLoaderTest.php @@ -4,16 +4,12 @@ namespace Doctrine\Migrations\Tests\Configuration\Connection\Loader; -use Doctrine\DBAL\Connection; use Doctrine\DBAL\Platforms\SqlitePlatform; -use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; use Doctrine\Migrations\Configuration\Connection\Loader\ArrayConnectionConfigurationLoader; -use Doctrine\Migrations\Configuration\Connection\Loader\ConnectionHelperLoader; +use Doctrine\Migrations\Configuration\Connection\Loader\Exception\ConnectionNotSpecified; use Doctrine\Migrations\Configuration\Connection\Loader\Exception\InvalidConfiguration; use Doctrine\Migrations\Configuration\Connection\Loader\NoConnectionLoader; -use Doctrine\Migrations\Tools\Console\Exception\ConnectionNotSpecified; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Helper\HelperSet; final class ConnectionLoaderTest extends TestCase { @@ -52,27 +48,4 @@ public function testArrayConnectionConfigurationLoaderNoFile() : void $loader = new ArrayConnectionConfigurationLoader(null, new NoConnectionLoader()); $loader->getConnection(); } - - public function testConnectionHelperLoader() : void - { - $connection = $this->createMock(Connection::class); - - $helper = $this->createMock(ConnectionHelper::class); - $helper->expects(self::once())->method('getConnection')->willReturn($connection); - - $helperSet = new HelperSet(); - $helperSet->set($helper, 'connection'); - $loader = new ConnectionHelperLoader('connection', new NoConnectionLoader(), $helperSet); - $conn = $loader->getConnection(); - - self::assertSame($connection, $conn); - } - - public function testConnectionHelperLoaderNoHelper() : void - { - $this->expectException(ConnectionNotSpecified::class); - $helperSet = new HelperSet(); - $loader = new ConnectionHelperLoader('connection', new NoConnectionLoader(), $helperSet); - $loader->getConnection(); - } } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/_files/migrations-db.php b/tests/Doctrine/Migrations/Tests/Configuration/Connection/_files/migrations-db.php similarity index 100% rename from tests/Doctrine/Migrations/Tests/Tools/Console/_files/migrations-db.php rename to tests/Doctrine/Migrations/Tests/Configuration/Connection/_files/migrations-db.php diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/_files/sqlite-connection.php b/tests/Doctrine/Migrations/Tests/Configuration/Connection/_files/sqlite-connection.php similarity index 100% rename from tests/Doctrine/Migrations/Tests/Tools/Console/_files/sqlite-connection.php rename to tests/Doctrine/Migrations/Tests/Configuration/Connection/_files/sqlite-connection.php diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Helper/files/config.php b/tests/Doctrine/Migrations/Tests/Configuration/_files_loader/config.php similarity index 100% rename from tests/Doctrine/Migrations/Tests/Tools/Console/Helper/files/config.php rename to tests/Doctrine/Migrations/Tests/Configuration/_files_loader/config.php diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Helper/files/migrations.php b/tests/Doctrine/Migrations/Tests/Configuration/_files_loader/migrations.php similarity index 100% rename from tests/Doctrine/Migrations/Tests/Tools/Console/Helper/files/migrations.php rename to tests/Doctrine/Migrations/Tests/Configuration/_files_loader/migrations.php diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/ConnectionLoaderTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/ConnectionLoaderTest.php deleted file mode 100644 index a7e30c723e..0000000000 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/ConnectionLoaderTest.php +++ /dev/null @@ -1,87 +0,0 @@ -createMock(HelperSet::class); - - $dir = getcwd() ?: '.'; - chdir(__DIR__); - try { - $conn = $this->connectionLoader->getConnection('_files/sqlite-connection.php', $helperSet); - self::assertInstanceOf(SqlitePlatform::class, $conn->getDatabasePlatform()); - } finally { - chdir($dir); - } - } - - public function testGetConnectionFromArrayNotFound() : void - { - $this->expectException(ConnectionNotSpecified::class); - $helperSet = $this->createMock(HelperSet::class); - - $dir = getcwd()?: '.'; - chdir(__DIR__); - try { - $this->connectionLoader->getConnection(__DIR__ . '/_files/wrong.php', $helperSet); - } finally { - chdir($dir); - } - } - - public function testGetConnectionFromArrayFromFallbackFile() : void - { - $helperSet = $this->createMock(HelperSet::class); - - $dir = getcwd()?: '.'; - chdir(__DIR__ . '/_files'); - try { - $conn = $this->connectionLoader->getConnection(null, $helperSet); - self::assertInstanceOf(SqlitePlatform::class, $conn->getDatabasePlatform()); - } finally { - chdir($dir); - } - } - - public function testGetConnectionFromHelper() : void - { - $connection = $this->createMock(Connection::class); - - $helper = $this->createMock(ConnectionHelper::class); - $helper->expects(self::once())->method('getConnection')->willReturn($connection); - - $helperSet = new HelperSet(); - $helperSet->set($helper, 'connection'); - - $dir = getcwd()?: '.'; - chdir(__DIR__); - try { - self::assertSame($connection, $this->connectionLoader->getConnection(null, $helperSet)); - } finally { - chdir($dir); - } - } - - protected function setUp() : void - { - $this->connectionLoader = new ConnectionLoader(); - } -} diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/ConsoleRunnerTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/ConsoleRunnerTest.php index c79f4bc648..9dd1391d80 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/ConsoleRunnerTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/ConsoleRunnerTest.php @@ -4,9 +4,9 @@ namespace Doctrine\Migrations\Tests\Tools\Console; +use Doctrine\Migrations\DependencyFactory; use Doctrine\Migrations\Tools\Console\ConsoleRunner; -use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; -use PHPUnit\Framework\MockObject\MockObject; +use Doctrine\ORM\EntityManager; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; @@ -17,16 +17,11 @@ */ class ConsoleRunnerTest extends TestCase { - /** @var MockObject|EntityManagerHelper */ - private $entityManagerHelper; - /** @var Application */ private $application; public function testRun() : void { - $helperSet = new HelperSet([]); - $application = $this->createMock(Application::class); ConsoleRunnerStub::$application = $application; @@ -34,7 +29,7 @@ public function testRun() : void $application->expects(self::once()) ->method('run'); - ConsoleRunnerStub::run($helperSet, []); + ConsoleRunnerStub::run([]); } public function testHasExecuteCommand() : void @@ -88,11 +83,15 @@ public function testHasUpToDateCommand() : void public function testHasDiffCommand() : void { - $this->application->setHelperSet(new HelperSet([ - 'em' => $this->entityManagerHelper, - ])); + $em = $this->createMock(EntityManager::class); - ConsoleRunner::addCommands($this->application); + $dependencyFactory = $this->createMock(DependencyFactory::class); + $dependencyFactory + ->expects(self::atLeastOnce()) + ->method('getEntityManager') + ->willReturn($em); + + ConsoleRunner::addCommands($this->application, $dependencyFactory); self::assertTrue($this->application->has('migrations:diff')); } @@ -108,21 +107,30 @@ public function testNotHasDiffCommand() : void public function testCreateApplication() : void { - $helperSet = new HelperSet(); + $application = ConsoleRunner::createApplication(); + $commands = $application->all('migrations'); + self::assertCount(9, $commands); + } + + public function testCreateApplicationWithEntityManager() : void + { + $em = $this->createMock(EntityManager::class); - $application = ConsoleRunner::createApplication($helperSet); + $dependencyFactory = $this->createMock(DependencyFactory::class); + $dependencyFactory + ->expects(self::atLeastOnce()) + ->method('getEntityManager') + ->willReturn($em); - self::assertSame($helperSet, $application->getHelperSet()); + $application = ConsoleRunner::createApplication([], $dependencyFactory); + $commands = $application->all('migrations'); + self::assertCount(10, $commands); } protected function setUp() : void { parent::setUp(); - - $this->application = new Application(); - $this->entityManagerHelper = $this->getMockBuilder(EntityManagerHelper::class) - ->disableOriginalConstructor() - ->getMock(); + $this->application = new Application(); } } @@ -134,7 +142,7 @@ class ConsoleRunnerStub extends ConsoleRunner /** * @param Command[] $commands */ - public static function createApplication(HelperSet $helperSet, array $commands = []) : Application + public static function createApplication(array $commands = [], ?DependencyFactory $dependencyFactory = null) : Application { return static::$application; } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Helper/ConfigurationHelperTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Helper/ConfigurationHelperTest.php deleted file mode 100644 index 1019b6c94c..0000000000 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Helper/ConfigurationHelperTest.php +++ /dev/null @@ -1,159 +0,0 @@ -input = $this->getMockBuilder(ArrayInput::class) - ->setConstructorArgs([[]]) - ->onlyMethods(['getOption']) - ->getMock(); - - $this->loader = $this->createMock(ConfigurationLoader::class); - $this->configurationHelper = new MigrationsConfigurationHelper($this->loader); - } - - /** - * Test that unsupported file type throws exception - */ - public function testNoAvailableConfigGivesBackEmptyConfig() : void - { - $this->input->method('getOption') - ->with('configuration') - ->willReturn(null); - - $confExpected = new Configuration(); - - $configLoader = $this->createMock(Loader::class); - - $configLoader - ->expects(self::once()) - ->method('load') - ->with([]) - ->willReturn($confExpected); - - $this->loader - ->expects(self::once()) - ->method('getLoader') - ->with('array') - ->willReturn($configLoader); - - $dir = getcwd()?: '.'; - try { - chdir(__DIR__); - $this->configurationHelper->getConfiguration($this->input); - } finally { - chdir($dir); - } - } - - public function testConfigurationHelperFailsToLoadOtherFormat() : void - { - $this->input->method('getOption') - ->with('configuration') - ->will(self::returnValue('testconfig.wrong')); - $this->loader - ->expects(self::once()) - ->method('getLoader') - ->with('wrong') - ->willThrowException(UnknownLoader::new('dummy')); - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Given config file type is not supported'); - - $dir = getcwd()?: '.'; - try { - chdir(__DIR__); - $this->configurationHelper->getConfiguration($this->input); - } finally { - chdir($dir); - } - } - - public function testLoadsFile() : void - { - $confExpected = new Configuration(); - $configLoader = $this->createMock(Loader::class); - - $configLoader - ->expects(self::once()) - ->method('load') - ->with('config.php') - ->willReturn($confExpected); - - $this->loader - ->expects(self::once()) - ->method('getLoader') - ->with('php') - ->willReturn($configLoader); - - $this->input->method('getOption') - ->with('configuration') - ->will(self::returnValue('config.php')); - - $config = $this->configurationHelper->getConfiguration($this->input); - - self::assertSame($config, $confExpected); - } - - public function testLoadsDefaultFile() : void - { - $confExpected = new Configuration(); - $configLoader = $this->createMock(Loader::class); - - $configLoader - ->expects(self::once()) - ->method('load') - ->with('migrations.php') - ->willReturn($confExpected); - - $this->loader - ->expects(self::once()) - ->method('getLoader') - ->with('php') - ->willReturn($configLoader); - - $this->input->method('getOption') - ->with('configuration') - ->willReturn(null); - - $dir = getcwd()?: '.'; - try { - chdir(__DIR__ . '/files'); - $config = $this->configurationHelper->getConfiguration($this->input); - } finally { - chdir($dir); - } - self::assertSame($config, $confExpected); - } -} diff --git a/tests/Doctrine/Migrations/Tests/doctrine-migrations-phpstan-app.php b/tests/Doctrine/Migrations/Tests/doctrine-migrations-phpstan-app.php index a779732c09..c5e0d3b72e 100644 --- a/tests/Doctrine/Migrations/Tests/doctrine-migrations-phpstan-app.php +++ b/tests/Doctrine/Migrations/Tests/doctrine-migrations-phpstan-app.php @@ -6,13 +6,7 @@ use Doctrine\Migrations\Tools\Console\Command\DiffCommand; use Doctrine\Migrations\Tools\Console\ConsoleRunner; -use Symfony\Component\Console\Helper\HelperSet; -use Symfony\Component\Console\Helper\QuestionHelper; require_once __DIR__ . '/../../../../vendor/autoload.php'; - -$helperSet = $helperSet ?? new HelperSet(); -$helperSet->set(new QuestionHelper(), 'question'); - -return ConsoleRunner::createApplication($helperSet, [new DiffCommand()]); +return ConsoleRunner::createApplication([new DiffCommand()]); From 40532cbb7d3c52801444d9f17237543ddb570b9f Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sun, 29 Dec 2019 16:15:23 +0100 Subject: [PATCH 2/2] use named constructors for the Dependency Factory --- lib/Doctrine/Migrations/DependencyFactory.php | 41 ++++++++++----- .../Tools/Console/Command/DoctrineCommand.php | 2 +- .../Tests/DependencyFactoryTest.php | 52 +++++++++++++------ .../Console/Command/LatestCommandTest.php | 2 +- .../Console/Command/MigrateCommandTest.php | 40 ++++---------- .../Console/Command/MigrationVersionTest.php | 2 +- .../Console/Command/StatusCommandTest.php | 2 +- .../Console/Command/UpToDateCommandTest.php | 2 +- 8 files changed, 78 insertions(+), 65 deletions(-) diff --git a/lib/Doctrine/Migrations/DependencyFactory.php b/lib/Doctrine/Migrations/DependencyFactory.php index f48a877712..cd1ec917ee 100644 --- a/lib/Doctrine/Migrations/DependencyFactory.php +++ b/lib/Doctrine/Migrations/DependencyFactory.php @@ -69,12 +69,27 @@ class DependencyFactory /** @var bool */ private $frozen = false; - public function __construct(Configuration $configuration, Connection $connection, ?EntityManagerInterface $em = null, ?LoggerInterface $logger = null) + public static function fromConnection(Configuration $configuration, Connection $connection, ?LoggerInterface $logger = null) : self { - $this->configuration = $configuration; - $this->logger = $logger ?: new NullLogger(); - $this->connection = $connection; - $this->em = $em; + $dependencyFactory = new self(); + + $dependencyFactory->configuration = $configuration; + $dependencyFactory->connection = $connection; + $dependencyFactory->logger = $logger ?: new NullLogger(); + + return $dependencyFactory; + } + + public static function fromEntityManager(Configuration $configuration, EntityManagerInterface $em, ?LoggerInterface $logger = null) : self + { + $dependencyFactory = new self(); + + $dependencyFactory->configuration = $configuration; + $dependencyFactory->em = $em; + $dependencyFactory->connection = $em->getConnection(); + $dependencyFactory->logger = $logger ?: new NullLogger(); + + return $dependencyFactory; } public function isFrozen() : bool @@ -179,8 +194,8 @@ public function getSchemaDiffProvider() : SchemaDiffProvider return $this->getDependency(SchemaDiffProvider::class, function () : LazySchemaDiffProvider { return LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration( new DBALSchemaDiffProvider( - $this->connection->getSchemaManager(), - $this->connection->getDatabasePlatform() + $this->getConnection()->getSchemaManager(), + $this->getConnection()->getDatabasePlatform() ) ); }); @@ -196,7 +211,7 @@ public function getFileBuilder() : FileBuilder public function getParameterFormatter() : ParameterFormatter { return $this->getDependency(ParameterFormatter::class, function () : ParameterFormatter { - return new InlineParameterFormatter($this->connection); + return new InlineParameterFormatter($this->getConnection()); }); } @@ -243,7 +258,7 @@ public function getMetadataStorage() : MetadataStorage { return $this->getDependency(MetadataStorage::class, function () : MetadataStorage { return new TableMetadataStorage( - $this->connection, + $this->getConnection(), $this->getMetadataStorageConfiguration() ); }); @@ -265,7 +280,7 @@ public function getVersionExecutor() : Executor return new DbalExecutor( $this->getMetadataStorage(), $this->getEventDispatcher(), - $this->connection, + $this->getConnection(), $this->getSchemaDiffProvider(), $this->getLogger(), $this->getParameterFormatter(), @@ -317,7 +332,7 @@ public function getMigrationSqlGenerator() : SqlGenerator return $this->getDependency(SqlGenerator::class, function () : SqlGenerator { return new SqlGenerator( $this->getConfiguration(), - $this->connection->getDatabasePlatform() + $this->getConnection()->getDatabasePlatform() ); }); } @@ -336,7 +351,7 @@ public function getMigrationStatusInfosHelper() : MigrationStatusInfosHelper return $this->getDependency(MigrationStatusInfosHelper::class, function () : MigrationStatusInfosHelper { return new MigrationStatusInfosHelper( $this->getConfiguration(), - $this->connection, + $this->getConnection(), $this->getVersionAliasResolver() ); }); @@ -346,7 +361,7 @@ public function getMigrator() : Migrator { return $this->getDependency(Migrator::class, function () : Migrator { return new DbalMigrator( - $this->connection, + $this->getConnection(), $this->getEventDispatcher(), $this->getVersionExecutor(), $this->logger, diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php index 671bc5a06e..f5549f2885 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php @@ -76,7 +76,7 @@ protected function initialize(InputInterface $input, OutputInterface $output) : $dbConfig = is_string($input->getOption('db-configuration')) ? $input->getOption('db-configuration'): null; $connection = self::getConnection($dbConfig); - $this->dependencyFactory = new DependencyFactory($configuration, $connection); + $this->dependencyFactory = DependencyFactory::fromConnection($configuration, $connection); } if ($this->dependencyFactory->isFrozen()) { diff --git a/tests/Doctrine/Migrations/Tests/DependencyFactoryTest.php b/tests/Doctrine/Migrations/Tests/DependencyFactoryTest.php index 27b6665093..d0df757ba6 100644 --- a/tests/Doctrine/Migrations/Tests/DependencyFactoryTest.php +++ b/tests/Doctrine/Migrations/Tests/DependencyFactoryTest.php @@ -10,6 +10,7 @@ use Doctrine\Migrations\Exception\FrozenDependencies; use Doctrine\Migrations\Finder\GlobFinder; use Doctrine\Migrations\Finder\RecursiveRegexFinder; +use Doctrine\ORM\EntityManager; use PHPUnit\Framework\MockObject\MockObject; use stdClass; @@ -18,17 +19,29 @@ final class DependencyFactoryTest extends MigrationTestCase /** @var MockObject|Connection */ private $connection; + /** @var Configuration */ + private $configuration; + + /** @var EntityManager|MockObject */ + private $entityManager; + public function setUp() : void { - $this->connection = $this->createMock(Connection::class); + $this->connection = $this->createMock(Connection::class); + $this->entityManager = $this->createMock(EntityManager::class); + $this->entityManager + ->expects(self::any()) + ->method('getConnection') + ->willReturn($this->connection); + + $this->configuration = new Configuration(); } public function testFreeze() : void { - $conf = new Configuration(); - $conf->addMigrationsDirectory('foo', 'bar'); + $this->configuration->addMigrationsDirectory('foo', 'bar'); - $di = new DependencyFactory($conf, $this->connection); + $di = DependencyFactory::fromConnection($this->configuration, $this->connection); $di->freeze(); $this->expectException(FrozenDependencies::class); @@ -38,10 +51,9 @@ public function testFreeze() : void public function testFinderForYearMonthStructure() : void { - $conf = new Configuration(); - $conf->setMigrationsAreOrganizedByYearAndMonth(true); + $this->configuration->setMigrationsAreOrganizedByYearAndMonth(true); - $di = new DependencyFactory($conf, $this->connection); + $di = DependencyFactory::fromConnection($this->configuration, $this->connection); $finder = $di->getMigrationsFinder(); self::assertInstanceOf(RecursiveRegexFinder::class, $finder); @@ -49,10 +61,9 @@ public function testFinderForYearMonthStructure() : void public function testFinderForYearStructure() : void { - $conf = new Configuration(); - $conf->setMigrationsAreOrganizedByYear(true); + $this->configuration->setMigrationsAreOrganizedByYear(true); - $di = new DependencyFactory($conf, $this->connection); + $di = DependencyFactory::fromConnection($this->configuration, $this->connection); $finder = $di->getMigrationsFinder(); self::assertInstanceOf(RecursiveRegexFinder::class, $finder); @@ -60,19 +71,26 @@ public function testFinderForYearStructure() : void public function testFinder() : void { - $conf = new Configuration(); - $di = new DependencyFactory($conf, $this->connection); - $finder = $di->getMigrationsFinder(); + $this->configuration = new Configuration(); + $di = DependencyFactory::fromConnection($this->configuration, $this->connection); + $finder = $di->getMigrationsFinder(); self::assertInstanceOf(GlobFinder::class, $finder); } public function testConnection() : void { - $conf = new Configuration(); - $di = new DependencyFactory($conf, $this->connection); - $conn = $di->getConnection(); + $di = DependencyFactory::fromConnection($this->configuration, $this->connection); + + self::assertSame($this->connection, $di->getConnection()); + self::assertNull($di->getEntityManager()); + } + + public function testEntityManager() : void + { + $di = DependencyFactory::fromEntityManager($this->configuration, $this->entityManager); - self::assertSame($this->connection, $conn); + self::assertSame($this->entityManager, $di->getEntityManager()); + self::assertSame($this->connection, $di->getConnection()); } } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/LatestCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/LatestCommandTest.php index 778ce81442..da179cb138 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/LatestCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/LatestCommandTest.php @@ -42,7 +42,7 @@ protected function setUp() : void $conn = $this->getSqliteConnection(); - $dependencyFactory = new DependencyFactory($configuration, $conn); + $dependencyFactory = DependencyFactory::fromConnection($configuration, $conn); $this->migrationRepository = $dependencyFactory->getMigrationRepository(); $this->metadataStorage = $dependencyFactory->getMetadataStorage(); diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php index 4bf7160ab9..e3c3e8dc6f 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php @@ -10,6 +10,7 @@ use Doctrine\Migrations\DependencyFactory; use Doctrine\Migrations\Metadata\MigrationPlanList; use Doctrine\Migrations\Metadata\Storage\MetadataStorage; +use Doctrine\Migrations\Migrator; use Doctrine\Migrations\MigratorConfiguration; use Doctrine\Migrations\QueryWriter; use Doctrine\Migrations\Tests\MigrationTestCase; @@ -24,7 +25,7 @@ class MigrateCommandTest extends MigrationTestCase { - /** @var DependencyFactory|MockObject */ + /** @var DependencyFactory */ private $dependencyFactory; /** @var Configuration */ @@ -121,10 +122,7 @@ public function testExecutedUnavailableMigrationsCancel() : void $this->storage->complete($result); $migrator = $this->createMock(DbalMigrator::class); - - $this->dependencyFactory->expects(self::any()) - ->method('getMigrator') - ->willReturn($migrator); + $this->dependencyFactory->setService(Migrator::class, $migrator); $migrator->expects(self::never()) ->method('migrate'); @@ -148,9 +146,7 @@ public function testExecuteWriteSql($arg, string $path) : void { $migrator = $this->createMock(DbalMigrator::class); - $this->dependencyFactory->expects(self::any()) - ->method('getMigrator') - ->willReturn($migrator); + $this->dependencyFactory->setService(Migrator::class, $migrator); $migrator->expects(self::once()) ->method('migrate') @@ -183,10 +179,7 @@ public function getWriteSqlValues() : array public function testExecuteMigrate() : void { $migrator = $this->createMock(DbalMigrator::class); - - $this->dependencyFactory->expects(self::any()) - ->method('getMigrator') - ->willReturn($migrator); + $this->dependencyFactory->setService(Migrator::class, $migrator); $this->migrateCommand->expects(self::once()) ->method('canExecute') @@ -209,10 +202,7 @@ public function testExecuteMigrate() : void public function testExecuteMigrateAllOrNothing() : void { $migrator = $this->createMock(DbalMigrator::class); - - $this->dependencyFactory->expects(self::any()) - ->method('getMigrator') - ->willReturn($migrator); + $this->dependencyFactory->setService(Migrator::class, $migrator); $migrator->expects(self::once()) ->method('migrate') @@ -242,9 +232,7 @@ public function testExecuteMigrateCancelExecutedUnavailableMigrations() : void $migrator = $this->createMock(DbalMigrator::class); - $this->dependencyFactory->expects(self::once()) - ->method('getMigrator') - ->willReturn($migrator); + $this->dependencyFactory->setService(Migrator::class, $migrator); $migrator->expects(self::never()) ->method('migrate'); @@ -267,10 +255,7 @@ public function testExecuteMigrateCancelExecutedUnavailableMigrations() : void public function testExecuteMigrateCancel() : void { $migrator = $this->createMock(DbalMigrator::class); - - $this->dependencyFactory->expects(self::once()) - ->method('getMigrator') - ->willReturn($migrator); + $this->dependencyFactory->setService(Migrator::class, $migrator); $migrator->expects(self::never()) ->method('migrate'); @@ -292,15 +277,10 @@ protected function setUp() : void $connection = $this->getSqliteConnection(); - $this->dependencyFactory = $this->getMockBuilder(DependencyFactory::class) - ->setConstructorArgs([$this->configuration, $connection]) - ->onlyMethods(['getMigrator', 'getQueryWriter']) - ->getMock(); + $this->dependencyFactory = DependencyFactory::fromConnection($this->configuration, $connection); $this->queryWriter = $this->createMock(QueryWriter::class); - $this->dependencyFactory->expects(self::any()) - ->method('getQueryWriter') - ->willReturn($this->queryWriter); + $this->dependencyFactory->setService(QueryWriter::class, $this->queryWriter); $migration = $this->createMock(AbstractMigration::class); diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrationVersionTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrationVersionTest.php index 4ed7e12d8a..6f5eccdf5a 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrationVersionTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrationVersionTest.php @@ -43,7 +43,7 @@ protected function setUp() : void $conn = $this->getSqliteConnection(); $logger = new TestLogger(); - $dependencyFactory = new DependencyFactory($configuration, $conn, null, $logger); + $dependencyFactory = DependencyFactory::fromConnection($configuration, $conn, $logger); $this->migrationRepository = $dependencyFactory->getMigrationRepository(); $this->metadataStorage = $dependencyFactory->getMetadataStorage(); diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/StatusCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/StatusCommandTest.php index 3c0af11991..025a0270da 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/StatusCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/StatusCommandTest.php @@ -47,7 +47,7 @@ protected function setUp() : void $conn = $this->getSqliteConnection(); - $dependencyFactory = new DependencyFactory($configuration, $conn); + $dependencyFactory = DependencyFactory::fromConnection($configuration, $conn); $this->migrationRepository = $dependencyFactory->getMigrationRepository(); $this->metadataStorage = $dependencyFactory->getMetadataStorage(); diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/UpToDateCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/UpToDateCommandTest.php index fbaf60f40a..02710d6131 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/UpToDateCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/UpToDateCommandTest.php @@ -41,7 +41,7 @@ protected function setUp() : void $conn = $this->getSqliteConnection(); - $dependencyFactory = new DependencyFactory($configuration, $conn); + $dependencyFactory = DependencyFactory::fromConnection($configuration, $conn); $this->migrationRepository = $dependencyFactory->getMigrationRepository(); $this->metadataStorage = $dependencyFactory->getMetadataStorage();