Skip to content

Commit

Permalink
[Cache] Support decorated Dbal drivers in PdoAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroeny authored and Tobion committed Jul 14, 2021
1 parent ba7e97d commit 58d74e3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
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'):
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

0 comments on commit 58d74e3

Please sign in to comment.