Skip to content

Commit

Permalink
Revert "Use a DI factory to unit test the plugin"
Browse files Browse the repository at this point in the history
This reverts commit 114678a.
  • Loading branch information
leofeyer committed Dec 3, 2019
1 parent 114678a commit d2cb958
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 46 deletions.
53 changes: 24 additions & 29 deletions manager-bundle/src/ContaoManager/Plugin.php
Expand Up @@ -63,11 +63,6 @@ class Plugin implements BundlePluginInterface, ConfigPluginInterface, RoutingPlu
*/
private static $autoloadModules;

/**
* @var callable
*/
private $dbalConnectionFactory;

/**
* Sets the path to enable autoloading of legacy Contao modules.
*/
Expand All @@ -76,11 +71,6 @@ public static function autoloadModules(string $modulePath): void
static::$autoloadModules = $modulePath;
}

public function __construct(callable $dbalConnectionFactory = null)
{
$this->dbalConnectionFactory = $dbalConnectionFactory ?: [DriverManager::class, 'getConnection'];
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -322,6 +312,29 @@ private function handlePrependLocale(array $extensionConfigs, ContainerBuilder $
* @return array<string,array<string,array<string,array<string,mixed>>>>
*/
private function addDefaultServerVersion(array $extensionConfigs, ContainerBuilder $container): array
{
$params = $this->getConnectionParams($extensionConfigs, $container);

try {
$connection = DriverManager::getConnection($params);
$connection->connect();
$connection->close();
} catch (DriverException $e) {
$extensionConfigs[] = [
'dbal' => [
'connections' => [
'default' => [
'server_version' => '5.5',
],
],
],
];
}

return $extensionConfigs;
}

private function getConnectionParams(array $extensionConfigs, ContainerBuilder $container): array
{
$params = [];

Expand All @@ -341,24 +354,6 @@ private function addDefaultServerVersion(array $extensionConfigs, ContainerBuild
$params[$key] = $parameterBag->unescapeValue($container->resolveEnvPlaceholders($value, true));
}

// If there are no DB credentials yet (install tool), we have to set
// the server version to prevent a DBAL exception (see #1422)
try {
$connection = call_user_func($this->dbalConnectionFactory, $params);
$connection->connect();
$connection->close();
} catch (DriverException $e) {
$extensionConfigs[] = [
'dbal' => [
'connections' => [
'default' => [
'server_version' => '5.5',
],
],
],
];
}

return $extensionConfigs;
return $params;
}
}
27 changes: 10 additions & 17 deletions manager-bundle/tests/ContaoManager/PluginTest.php
Expand Up @@ -24,7 +24,6 @@
use Contao\User;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle;
use Doctrine\DBAL\Connection;
use FOS\HttpCacheBundle\FOSHttpCacheBundle;
use Lexik\Bundle\MaintenanceBundle\LexikMaintenanceBundle;
use Nelmio\CorsBundle\NelmioCorsBundle;
Expand Down Expand Up @@ -399,26 +398,20 @@ public function testRetrievesTheConnectionParametersFromTheConfiguration(): void
],
];

$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('connect');
$connection->expects($this->once())->method('close');

$dbalConnectionFactory = function ($params) use ($connection) {
$this->assertSame(
[
'url' => 'mysql://root:foo%bar@localhost:3306/database',
'password' => 'foo%bar',
],
$params
);
return $connection;
};
$class = new \ReflectionClass(Plugin::class);
$method = $class->getMethod('getConnectionParams');
$method->setAccessible(true);

$url = $_ENV['DATABASE_URL'] ?? null;
$_ENV['DATABASE_URL'] = 'mysql://root:foo%bar@localhost:3306/database';

$plugin = new Plugin($dbalConnectionFactory);
$plugin->getExtensionConfig('doctrine', $extensionConfigs, $container);
$this->assertSame(
[
'url' => 'mysql://root:foo%bar@localhost:3306/database',
'password' => 'foo%bar',
],
$method->invokeArgs(new Plugin(), [$extensionConfigs, $container])
);

$_ENV['DATABASE_URL'] = $url;
}
Expand Down

0 comments on commit d2cb958

Please sign in to comment.