Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow legacy cli-config.php configurations #989

Merged
merged 1 commit into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/Doctrine/Migrations/Tools/Console/ConsoleRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Doctrine\Migrations\Tools\Console;

use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
use Doctrine\Migrations\Configuration\Migration\ConfigurationFileWithFallback;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Tools\Console\Command\CurrentCommand;
use Doctrine\Migrations\Tools\Console\Command\DiffCommand;
Expand All @@ -19,9 +23,11 @@
use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand;
use Doctrine\Migrations\Tools\Console\Command\UpToDateCommand;
use Doctrine\Migrations\Tools\Console\Command\VersionCommand;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use PackageVersions\Versions;
use RuntimeException;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use function file_exists;
use function getcwd;
use function is_readable;
Expand Down Expand Up @@ -67,6 +73,7 @@ public static function findDependencyFactory() : ?DependencyFactory
}

$dependencyFactory = require $configurationFile;
$dependencyFactory = self::checkLegacyConfiguration($dependencyFactory, $configurationFile);
}

if ($dependencyFactory !== null && ! ($dependencyFactory instanceof DependencyFactory)) {
Expand Down Expand Up @@ -121,4 +128,36 @@ public static function addCommands(Application $cli, ?DependencyFactory $depende

$cli->add(new DiffCommand($dependencyFactory));
}

/**
* @param mixed|HelperSet $dependencyFactory
*
* @return mixed|DependencyFactory
*/
private static function checkLegacyConfiguration($dependencyFactory, string $configurationFile)
{
if (! ($dependencyFactory instanceof HelperSet)) {
return $dependencyFactory;
}

$configurations = new ConfigurationFileWithFallback();
if ($dependencyFactory->has('em') && $dependencyFactory->get('em') instanceof EntityManagerHelper) {
return DependencyFactory::fromEntityManager(
$configurations,
new ExistingEntityManager($dependencyFactory->get('em')->getEntityManager())
);
}

if ($dependencyFactory->has('db') && $dependencyFactory->get('db') instanceof ConnectionHelper) {
return DependencyFactory::fromConnection(
$configurations,
new ExistingConnection($dependencyFactory->get('db')->getConnection())
);
}

throw new RuntimeException(sprintf(
'Configuration HelperSet returned by "%s" does not have a valid "em" or the "db" helper.',
$configurationFile
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Tools\Console\ConsoleRunner;
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Console\Application;
Expand All @@ -24,6 +25,66 @@ class ConsoleRunnerTest extends TestCase
/** @var Application */
private $application;

public function testCreateDependencyFactoryFromLegacyDbalHelper() : void
{
$dir = getcwd();
if ($dir === false) {
$dir = '.';
}

chdir(__DIR__ . '/legacy-config-dbal');

try {
$dependencyFactory = ConsoleRunnerStub::findDependencyFactory();
self::assertInstanceOf(DependencyFactory::class, $dependencyFactory);
self::assertSame('sqlite', $dependencyFactory->getConnection()->getDatabasePlatform()->getName());
} finally {
chdir($dir);
}
}

public function testCreateDependencyFactoryFromLegacyOrmHelper() : void
{
$dir = getcwd();
if ($dir === false) {
$dir = '.';
}

chdir(__DIR__ . '/legacy-config-orm');

try {
$dependencyFactory = ConsoleRunnerStub::findDependencyFactory();
self::assertInstanceOf(DependencyFactory::class, $dependencyFactory);
self::assertSame('sqlite', $dependencyFactory->getConnection()->getDatabasePlatform()->getName());
self::assertInstanceOf(EntityManager::class, $dependencyFactory->getEntityManager());
} finally {
chdir($dir);
}
}

public function testCreateDependencyFactoryFromWrongLegacyHelper() : void
{
$this->expectException(RuntimeException::class);

$dir = getcwd();
if ($dir === false) {
$dir = '.';
}

chdir(__DIR__ . '/legacy-config-wrong');

$this->expectExceptionMessage(sprintf(
'Configuration HelperSet returned by "%s" does not have a valid "em" or the "db" helper.',
realpath(__DIR__ . '/legacy-config-wrong/cli-config.php')
));

try {
$dependencyFactory = ConsoleRunnerStub::findDependencyFactory();
} finally {
chdir($dir);
}
}

/**
* @dataProvider getDependencyFactoryTestDirectories
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Symfony\Component\Console\Helper\HelperSet;

$conn = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);

return new HelperSet([
'db' => new ConnectionHelper($conn),
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

return [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\Persistence\Mapping\Driver\PHPDriver;
use Symfony\Component\Console\Helper\HelperSet;

$conf = new Configuration();
$conf->setProxyDir(__DIR__);
$conf->setProxyNamespace('Foo');
$conf->setMetadataDriverImpl(new PHPDriver(''));

$conn = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);

$em = EntityManager::create($conn, $conf);

return new HelperSet([
'em' => new EntityManagerHelper($em),
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

return [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Symfony\Component\Console\Helper\HelperSet;

$conn = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);

return new HelperSet([
'abc' => new ConnectionHelper($conn),
]);