Unverified Commit 367b731d authored by Marco Pivetta's avatar Marco Pivetta

Merge branch 'fix/#2821-verify-that-platform-params-are-passed-to-the-query-cache-2.6' into 2.6

Backport #2821
parents 324f3ac2 45c022d9
...@@ -211,6 +211,15 @@ class Connection implements DriverConnection ...@@ -211,6 +211,15 @@ class Connection implements DriverConnection
unset($this->_params['pdo']); unset($this->_params['pdo']);
} }
if (isset($params["platform"])) {
if ( ! $params['platform'] instanceof Platforms\AbstractPlatform) {
throw DBALException::invalidPlatformType($params['platform']);
}
$this->platform = $params["platform"];
unset($this->_params["platform"]);
}
// Create default config and event manager if none given // Create default config and event manager if none given
if ( ! $config) { if ( ! $config) {
$config = new Configuration(); $config = new Configuration();
...@@ -384,7 +393,6 @@ class Connection implements DriverConnection ...@@ -384,7 +393,6 @@ class Connection implements DriverConnection
*/ */
private function detectDatabasePlatform() private function detectDatabasePlatform()
{ {
if ( ! isset($this->_params['platform'])) {
$version = $this->getDatabasePlatformVersion(); $version = $this->getDatabasePlatformVersion();
if (null !== $version) { if (null !== $version) {
...@@ -392,11 +400,6 @@ class Connection implements DriverConnection ...@@ -392,11 +400,6 @@ class Connection implements DriverConnection
} else { } else {
$this->platform = $this->_driver->getDatabasePlatform(); $this->platform = $this->_driver->getDatabasePlatform();
} }
} elseif ($this->_params['platform'] instanceof Platforms\AbstractPlatform) {
$this->platform = $this->_params['platform'];
} else {
throw DBALException::invalidPlatformSpecified();
}
$this->platform->setEventManager($this->_eventManager); $this->platform->setEventManager($this->_eventManager);
} }
......
...@@ -22,6 +22,7 @@ namespace Doctrine\DBAL; ...@@ -22,6 +22,7 @@ namespace Doctrine\DBAL;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\ExceptionConverterDriver; use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class DBALException extends \Exception class DBALException extends \Exception
{ {
...@@ -35,16 +36,37 @@ class DBALException extends \Exception ...@@ -35,16 +36,37 @@ class DBALException extends \Exception
return new self("Operation '$method' is not supported by platform."); return new self("Operation '$method' is not supported by platform.");
} }
/** public static function invalidPlatformSpecified() : self
* @return \Doctrine\DBAL\DBALException
*/
public static function invalidPlatformSpecified()
{ {
return new self( return new self(
"Invalid 'platform' option specified, need to give an instance of ". "Invalid 'platform' option specified, need to give an instance of ".
"\Doctrine\DBAL\Platforms\AbstractPlatform."); "\Doctrine\DBAL\Platforms\AbstractPlatform.");
} }
/**
* @param mixed $invalidPlatform
*/
public static function invalidPlatformType($invalidPlatform) : self
{
if (\is_object($invalidPlatform)) {
return new self(
sprintf(
"Option 'platform' must be a subtype of '%s', instance of '%s' given",
AbstractPlatform::class,
\get_class($invalidPlatform)
)
);
}
return new self(
sprintf(
"Option 'platform' must be an object and subtype of '%s'. Got '%s'",
AbstractPlatform::class,
\gettype($invalidPlatform)
)
);
}
/** /**
* Returns a new instance for an invalid specified platform version. * Returns a new instance for an invalid specified platform version.
* *
......
...@@ -7,8 +7,10 @@ use Doctrine\Common\EventManager; ...@@ -7,8 +7,10 @@ use Doctrine\Common\EventManager;
use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Events; use Doctrine\DBAL\Events;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Tests\Mocks\DriverConnectionMock; use Doctrine\Tests\Mocks\DriverConnectionMock;
use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\DriverMock;
use Doctrine\DBAL\Cache\ArrayStatement; use Doctrine\DBAL\Cache\ArrayStatement;
...@@ -777,6 +779,61 @@ class ConnectionTest extends \Doctrine\Tests\DbalTestCase ...@@ -777,6 +779,61 @@ class ConnectionTest extends \Doctrine\Tests\DbalTestCase
); );
} }
/**
* @group DBAL-2821
*/
public function testShouldNotPassPlatformInParamsToTheQueryCacheProfileInExecuteCacheQuery(): void
{
$resultCacheDriverMock = $this->createMock(Cache::class);
$resultCacheDriverMock
->expects($this->atLeastOnce())
->method('fetch')
->with('cacheKey')
->will($this->returnValue(['realKey' => []]));
/* @var $queryCacheProfileMock QueryCacheProfile|\PHPUnit_Framework_MockObject_MockObject */
$queryCacheProfileMock = $this->createMock(QueryCacheProfile::class);
$queryCacheProfileMock
->expects($this->any())
->method('getResultCacheDriver')
->will($this->returnValue($resultCacheDriverMock));
$query = 'SELECT 1';
$connectionParams = $this->params;
$queryCacheProfileMock
->expects($this->once())
->method('generateCacheKeys')
->with($query, [], [], $connectionParams)
->will($this->returnValue(['cacheKey', 'realKey']));
$connectionParams['platform'] = $this->createMock(AbstractPlatform::class);
/* @var $driver Driver */
$driver = $this->createMock(Driver::class);
(new Connection($connectionParams, $driver))->executeCacheQuery($query, [], [], $queryCacheProfileMock);
}
/**
* @group DBAL-2821
*/
public function testThrowsExceptionWhenInValidPlatformSpecified(): void
{
$connectionParams = $this->params;
$connectionParams['platform'] = new \stdClass();
/* @var $driver Driver */
$driver = $this->createMock(Driver::class);
$this->expectException(DBALException::class);
new Connection($connectionParams, $driver);
}
/** /**
* @group DBAL-990 * @group DBAL-990
*/ */
......
...@@ -58,4 +58,30 @@ class DBALExceptionTest extends DbalTestCase ...@@ -58,4 +58,30 @@ class DBALExceptionTest extends DbalTestCase
$exception->getMessage() $exception->getMessage()
); );
} }
/**
* @group DBAL-2821
*/
public function testInvalidPlatformTypeObject(): void
{
$exception = DBALException::invalidPlatformType(new \stdClass());
self::assertSame(
"Option 'platform' must be a subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform', instance of 'stdClass' given",
$exception->getMessage()
);
}
/**
* @group DBAL-2821
*/
public function testInvalidPlatformTypeScalar(): void
{
$exception = DBALException::invalidPlatformType('some string');
self::assertSame(
"Option 'platform' must be an object and subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform'. Got 'string'",
$exception->getMessage()
);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment