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

[Cache] Support decorated Dbal drivers in PdoAdapter #42011

Merged
merged 1 commit into from Jul 14, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/patch-types.php
Expand Up @@ -25,6 +25,7 @@
case false !== strpos($file = realpath($file), '/vendor/'):
case false !== strpos($file, '/src/Symfony/Bridge/PhpUnit/'):
case false !== strpos($file, '/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Article.php'):
case false !== strpos($file, '/src/Symfony/Component/Cache/Tests/Fixtures/DriverWrapper.php'):
derrabus marked this conversation as resolved.
Show resolved Hide resolved
case false !== strpos($file, '/src/Symfony/Component/Config/Tests/Fixtures/BadFileName.php'):
case false !== strpos($file, '/src/Symfony/Component/Config/Tests/Fixtures/BadParent.php'):
case false !== strpos($file, '/src/Symfony/Component/Config/Tests/Fixtures/ParseError.php'):
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/PdoDbalAdapterTest.php
Expand Up @@ -11,10 +11,13 @@

namespace Symfony\Component\Cache\Tests\Adapter;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Driver\Middleware;
use Doctrine\DBAL\DriverManager;
use PHPUnit\Framework\SkippedTestSuiteError;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\PdoAdapter;
use Symfony\Component\Cache\Tests\Fixtures\DriverWrapper;

/**
* @group time-sensitive
Expand Down Expand Up @@ -43,4 +46,29 @@ public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterfac
{
return new PdoAdapter(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]), '', $defaultLifetime);
}

public function testConfigureSchemaDecoratedDbalDriver()
{
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]);
if (!interface_exists(Middleware::class)) {
$this->markTestSkipped('doctrine/dbal v2 does not support custom drivers using middleware');
}

$middleware = $this->createMock(Middleware::class);
$middleware
->method('wrap')
->willReturn(new DriverWrapper($connection->getDriver()));

$config = new Configuration();
$config->setMiddlewares([$middleware]);

$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $config);

$adapter = new PdoAdapter($connection);
$adapter->createTable();

$item = $adapter->getItem('key');
$item->set('value');
$this->assertTrue($adapter->save($item));
}
}
48 changes: 48 additions & 0 deletions src/Symfony/Component/Cache/Tests/Fixtures/DriverWrapper.php
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Fixtures;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;

class DriverWrapper implements Driver
{
/** @var Driver */
private $driver;

public function __construct(Driver $driver)
{
$this->driver = $driver;
}

public function connect(array $params, $username = null, $password = null, array $driverOptions = []): Driver\Connection
{
return $this->driver->connect($params, $username, $password, $driverOptions);
}

public function getDatabasePlatform(): AbstractPlatform
{
return $this->driver->getDatabasePlatform();
}

public function getSchemaManager(Connection $conn, AbstractPlatform $platform): AbstractSchemaManager
{
return $this->driver->getSchemaManager($conn, $platform);
}

public function getExceptionConverter(): Driver\API\ExceptionConverter
{
return $this->driver->getExceptionConverter();
}
}
9 changes: 9 additions & 0 deletions src/Symfony/Component/Cache/Traits/PdoTrait.php
Expand Up @@ -448,6 +448,15 @@ private function getConnection()
case $driver instanceof \Doctrine\DBAL\Driver\PDO\SQLSrv\Driver:
$this->driver = 'sqlsrv';
break;
case $driver instanceof \Doctrine\DBAL\Driver:
$this->driver = [
'mssql' => 'sqlsrv',
'oracle' => 'oci',
'postgresql' => 'pgsql',
'sqlite' => 'sqlite',
'mysql' => 'mysql',
][$driver->getDatabasePlatform()->getName()] ?? \get_class($driver);
break;
default:
$this->driver = \get_class($driver);
break;
Expand Down