From 58d74e30be4946bdefaf4b1e1eb39df37d201755 Mon Sep 17 00:00:00 2001 From: Jeroeny Date: Wed, 7 Jul 2021 14:24:36 +0200 Subject: [PATCH] [Cache] Support decorated Dbal drivers in PdoAdapter --- .github/patch-types.php | 1 + .../Tests/Adapter/PdoDbalAdapterTest.php | 28 +++++++++++ .../Cache/Tests/Fixtures/DriverWrapper.php | 48 +++++++++++++++++++ .../Component/Cache/Traits/PdoTrait.php | 9 ++++ 4 files changed, 86 insertions(+) create mode 100644 src/Symfony/Component/Cache/Tests/Fixtures/DriverWrapper.php diff --git a/.github/patch-types.php b/.github/patch-types.php index 95e56b5984..be1c9f2746 100644 --- a/.github/patch-types.php +++ b/.github/patch-types.php @@ -25,6 +25,7 @@ foreach ($loader->getClassMap() as $class => $file) { 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'): 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'): diff --git a/src/Symfony/Component/Cache/Tests/Adapter/PdoDbalAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/PdoDbalAdapterTest.php index c31f96dda5..3bfa3070b5 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/PdoDbalAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/PdoDbalAdapterTest.php @@ -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 @@ -43,4 +46,29 @@ class PdoDbalAdapterTest extends AdapterTestCase { 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)); + } } diff --git a/src/Symfony/Component/Cache/Tests/Fixtures/DriverWrapper.php b/src/Symfony/Component/Cache/Tests/Fixtures/DriverWrapper.php new file mode 100644 index 0000000000..bb73d8d0cf --- /dev/null +++ b/src/Symfony/Component/Cache/Tests/Fixtures/DriverWrapper.php @@ -0,0 +1,48 @@ + + * + * 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(); + } +} diff --git a/src/Symfony/Component/Cache/Traits/PdoTrait.php b/src/Symfony/Component/Cache/Traits/PdoTrait.php index e115acfb89..a2129c459b 100644 --- a/src/Symfony/Component/Cache/Traits/PdoTrait.php +++ b/src/Symfony/Component/Cache/Traits/PdoTrait.php @@ -448,6 +448,15 @@ trait PdoTrait 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;