From 9962901205fb41a6246e0cb96f3a222a6fd19134 Mon Sep 17 00:00:00 2001 From: Antonio Vilar Date: Sat, 1 Nov 2014 09:02:07 +0100 Subject: [PATCH 1/9] Used connection params in cache key generation --- lib/Doctrine/DBAL/Cache/QueryCacheProfile.php | 11 ++++++++--- lib/Doctrine/DBAL/Connection.php | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php b/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php index 330eec91f23..550ffad1e44 100644 --- a/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php +++ b/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php @@ -88,17 +88,22 @@ public function getCacheKey() } /** - * Generates the real cache key from query, params and types. + * Generates the real cache key from query, params, types and connection parameters. * * @param string $query * @param array $params * @param array $types + * @param array $connectionParams * * @return array */ - public function generateCacheKeys($query, $params, $types) + public function generateCacheKeys($query, $params, $types, $connectionParams = array()) { - $realCacheKey = $query . "-" . serialize($params) . "-" . serialize($types); + $realCacheKey = 'query=' . $query . + '¶ms=' . serialize($params) . + '&types=' . serialize($types) . + (!empty($connectionParams) ? serialize($connectionParams) : ''); + // should the key be automatically generated using the inputs or is the cache key set? if ($this->cacheKey === null) { $cacheKey = sha1($realCacheKey); diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 6f6b0b81d9d..61afb7b8e8e 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -879,7 +879,7 @@ public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qc throw CacheException::noResultDriverConfigured(); } - list($cacheKey, $realKey) = $qcp->generateCacheKeys($query, $params, $types); + list($cacheKey, $realKey) = $qcp->generateCacheKeys($query, $params, $types, $this->getParams()); // fetch the row pointers entry if ($data = $resultCache->fetch($cacheKey)) { From a60be24636d9ef8abe937539719c146eeeaff5a9 Mon Sep 17 00:00:00 2001 From: Antonio Vilar Date: Sat, 8 Nov 2014 09:54:42 +0100 Subject: [PATCH 2/9] Added tests for Cache\QueryCacheProfile --- .../DBAL/Cache/QueryCacheProfileTest.php | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php diff --git a/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php b/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php new file mode 100644 index 00000000000..a1f6260a058 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php @@ -0,0 +1,78 @@ +queryCacheProfile = new QueryCacheProfile(self::LIFETIME, self::CACHE_KEY); + } + + /** + * @test + */ + public function it_should_use_the_given_cache_key_if_present() + { + $query = 'SELECT * FROM foo WHERE bar = ?'; + $params = array(666); + $types = array(\PDO::PARAM_INT); + + $connectionParams = array( + 'dbname' => 'database_name', + 'user' => 'database_user', + 'password' => 'database_password', + 'host' => 'database_host', + 'driver' => 'database_driver' + ); + + $expectedRealCacheKey = 'query=SELECT * FROM foo WHERE bar = ?¶ms=a:1:{i:0;i:666;}&types=a:1:{i:0;' + . 'i:1;}a:5:{s:6:"dbname";s:13:"database_name";s:4:"user";s:13:"database_user";s:8:"password";s:17:"' + . 'database_password";s:4:"host";s:13:"database_host";s:6:"driver";s:15:"database_driver";}'; + + $cacheKeysResult = $this->queryCacheProfile->generateCacheKeys($query, $params, $types, $connectionParams); + + $this->assertEquals(self::CACHE_KEY, $cacheKeysResult[0], 'The returned cached key should match the given one'); + $this->assertEquals($expectedRealCacheKey, $cacheKeysResult[1]); + } + + /** + * @test + */ + public function it_should_generate_an_automatic_key_if_no_key_has_been_specified() + { + $query = 'SELECT * FROM foo WHERE bar = ?'; + $params = array(666); + $types = array(\PDO::PARAM_INT); + + $connectionParams = array( + 'dbname' => 'database_name', + 'user' => 'database_user', + 'password' => 'database_password', + 'host' => 'database_host', + 'driver' => 'database_driver' + ); + + $expectedRealCacheKey = 'query=SELECT * FROM foo WHERE bar = ?¶ms=a:1:{i:0;i:666;}&types=a:1:{i:0;' + . 'i:1;}a:5:{s:6:"dbname";s:13:"database_name";s:4:"user";s:13:"database_user";s:8:"password";s:17:"' + . 'database_password";s:4:"host";s:13:"database_host";s:6:"driver";s:15:"database_driver";}'; + + $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); + + $cacheKeysResult = $this->queryCacheProfile->generateCacheKeys($query, $params, $types, $connectionParams); + + $this->assertEquals('8f74136f51719d57f4da9b6d2ba955b42189bb81', $cacheKeysResult[0]); + $this->assertEquals($expectedRealCacheKey, $cacheKeysResult[1]); + } +} From d19567bc98935c3f11d569108cd9de27a886a868 Mon Sep 17 00:00:00 2001 From: Antonio Vilar Date: Sat, 8 Nov 2014 20:53:05 +0100 Subject: [PATCH 3/9] Added new tests for QueryCacheProfile and Connection --- .../DBAL/Cache/QueryCacheProfileTest.php | 110 ++++++++++++++---- tests/Doctrine/Tests/DBAL/ConnectionTest.php | 34 ++++++ 2 files changed, 124 insertions(+), 20 deletions(-) diff --git a/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php b/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php index a1f6260a058..76c0776c34e 100644 --- a/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php +++ b/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php @@ -20,10 +20,7 @@ protected function setUp() $this->queryCacheProfile = new QueryCacheProfile(self::LIFETIME, self::CACHE_KEY); } - /** - * @test - */ - public function it_should_use_the_given_cache_key_if_present() + public function testShouldUseTheGivenCacheKeyIfPresent() { $query = 'SELECT * FROM foo WHERE bar = ?'; $params = array(666); @@ -37,20 +34,43 @@ public function it_should_use_the_given_cache_key_if_present() 'driver' => 'database_driver' ); - $expectedRealCacheKey = 'query=SELECT * FROM foo WHERE bar = ?¶ms=a:1:{i:0;i:666;}&types=a:1:{i:0;' - . 'i:1;}a:5:{s:6:"dbname";s:13:"database_name";s:4:"user";s:13:"database_user";s:8:"password";s:17:"' - . 'database_password";s:4:"host";s:13:"database_host";s:6:"driver";s:15:"database_driver";}'; + $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + $query, + $params, + $types, + $connectionParams + ); + + $this->assertEquals(self::CACHE_KEY, $generatedKeys[0], 'The returned cached key should match the given one'); + } + + public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven() + { + $query = 'SELECT * FROM foo WHERE bar = ?'; + $params = array(666); + $types = array(\PDO::PARAM_INT); + + $connectionParams = array( + 'dbname' => 'database_name', + 'user' => 'database_user', + 'password' => 'database_password', + 'host' => 'database_host', + 'driver' => 'database_driver' + ); + + $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); - $cacheKeysResult = $this->queryCacheProfile->generateCacheKeys($query, $params, $types, $connectionParams); + $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + $query, + $params, + $types, + $connectionParams + ); - $this->assertEquals(self::CACHE_KEY, $cacheKeysResult[0], 'The returned cached key should match the given one'); - $this->assertEquals($expectedRealCacheKey, $cacheKeysResult[1]); + $this->assertNotEmpty($generatedKeys[0], 'The generated cache key should not be empty'); } - /** - * @test - */ - public function it_should_generate_an_automatic_key_if_no_key_has_been_specified() + public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections() { $query = 'SELECT * FROM foo WHERE bar = ?'; $params = array(666); @@ -64,15 +84,65 @@ public function it_should_generate_an_automatic_key_if_no_key_has_been_specified 'driver' => 'database_driver' ); - $expectedRealCacheKey = 'query=SELECT * FROM foo WHERE bar = ?¶ms=a:1:{i:0;i:666;}&types=a:1:{i:0;' - . 'i:1;}a:5:{s:6:"dbname";s:13:"database_name";s:4:"user";s:13:"database_user";s:8:"password";s:17:"' - . 'database_password";s:4:"host";s:13:"database_host";s:6:"driver";s:15:"database_driver";}'; + $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); + + $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + $query, + $params, + $types, + $connectionParams + ); + + $firstCacheKey = $generatedKeys[0]; + + $connectionParams['host'] = 'a_different_host'; + + $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + $query, + $params, + $types, + $connectionParams + ); + + $secondCacheKey = $generatedKeys[0]; + + $this->assertNotEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be different'); + } + + public function testShouldGenerateSameKeysIfNoneOfTheParamsChanges() + { + $query = 'SELECT * FROM foo WHERE bar = ?'; + $params = array(666); + $types = array(\PDO::PARAM_INT); + + $connectionParams = array( + 'dbname' => 'database_name', + 'user' => 'database_user', + 'password' => 'database_password', + 'host' => 'database_host', + 'driver' => 'database_driver' + ); $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); - $cacheKeysResult = $this->queryCacheProfile->generateCacheKeys($query, $params, $types, $connectionParams); + $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + $query, + $params, + $types, + $connectionParams + ); + + $firstCacheKey = $generatedKeys[0]; + + $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + $query, + $params, + $types, + $connectionParams + ); + + $secondCacheKey = $generatedKeys[0]; - $this->assertEquals('8f74136f51719d57f4da9b6d2ba955b42189bb81', $cacheKeysResult[0]); - $this->assertEquals($expectedRealCacheKey, $cacheKeysResult[1]); + $this->assertEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be the same'); } } diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index ff414a86ed2..ce1447558c6 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -670,4 +670,38 @@ public function testPlatformDetectionIsTriggerOnlyOnceOnRetrievingPlatform() $this->assertSame($platformMock, $connection->getDatabasePlatform()); } + + public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCacheQuery() + { + $resultCacheDriverMock = $this->getMock('Doctrine\Common\Cache\Cache'); + + $resultCacheDriverMock->expects($this->any()) + ->method('fetch') + ->will($this->returnValue(array('realKey' => array()))); + + $query = 'SELECT * FROM foo WHERE bar = ?'; + $params = array(666); + $types = array(\PDO::PARAM_INT); + + $queryCacheProfileMock = $this->getMock('Doctrine\DBAL\Cache\QueryCacheProfile'); + + $queryCacheProfileMock->expects($this->any()) + ->method('getResultCacheDriver') + ->will($this->returnValue($resultCacheDriverMock)); + + // This is our main expectation + $queryCacheProfileMock->expects($this->once()) + ->method('generateCacheKeys') + ->with($query, $params, $types, $this->params) + ->will($this->returnValue(array('cacheKey', 'realKey'))); + + $conn = new Connection( + $this->params, + $this->getMock('Doctrine\DBAL\Driver') + ); + + $result = $conn->executeCacheQuery($query, $params, $types, $queryCacheProfileMock); + + $this->assertInstanceOf('Doctrine\DBAL\Cache\ArrayStatement', $result); + } } From e374d9945244cc271a487aa6790caf590d7186bc Mon Sep 17 00:00:00 2001 From: Antonio Vilar Date: Mon, 10 Nov 2014 16:15:02 +0100 Subject: [PATCH 4/9] Some test tweaks --- lib/Doctrine/DBAL/Cache/QueryCacheProfile.php | 2 +- .../Tests/DBAL/Cache/QueryCacheProfileTest.php | 10 +++++++--- tests/Doctrine/Tests/DBAL/ConnectionTest.php | 9 +++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php b/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php index 550ffad1e44..7b6010b5b15 100644 --- a/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php +++ b/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php @@ -97,7 +97,7 @@ public function getCacheKey() * * @return array */ - public function generateCacheKeys($query, $params, $types, $connectionParams = array()) + public function generateCacheKeys($query, $params, $types, array $connectionParams = array()) { $realCacheKey = 'query=' . $query . '¶ms=' . serialize($params) . diff --git a/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php b/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php index 76c0776c34e..ea9f3a7487a 100644 --- a/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php +++ b/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php @@ -2,8 +2,6 @@ namespace Doctrine\Tests\DBAL\Cache; -require_once __DIR__ . '/../../TestInit.php'; - use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\Tests\DbalTestCase; @@ -41,7 +39,7 @@ public function testShouldUseTheGivenCacheKeyIfPresent() $connectionParams ); - $this->assertEquals(self::CACHE_KEY, $generatedKeys[0], 'The returned cached key should match the given one'); + $this->assertEquals(self::CACHE_KEY, $generatedKeys[0], 'The returned cache key should match the given one'); } public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven() @@ -67,6 +65,12 @@ public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven() $connectionParams ); + $this->assertNotEquals( + self::CACHE_KEY, + $generatedKeys[0], + 'The returned cache key should be generated automatically' + ); + $this->assertNotEmpty($generatedKeys[0], 'The generated cache key should not be empty'); } diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index ce1447558c6..18c0ff0010c 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -675,8 +675,9 @@ public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCach { $resultCacheDriverMock = $this->getMock('Doctrine\Common\Cache\Cache'); - $resultCacheDriverMock->expects($this->any()) + $resultCacheDriverMock->expects($this->atLeastOnce()) ->method('fetch') + ->with('cacheKey') ->will($this->returnValue(array('realKey' => array()))); $query = 'SELECT * FROM foo WHERE bar = ?'; @@ -700,8 +701,8 @@ public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCach $this->getMock('Doctrine\DBAL\Driver') ); - $result = $conn->executeCacheQuery($query, $params, $types, $queryCacheProfileMock); - - $this->assertInstanceOf('Doctrine\DBAL\Cache\ArrayStatement', $result); + $this->assertInstanceOf('Doctrine\DBAL\Cache\ArrayStatement', + $conn->executeCacheQuery($query, $params, $types, $queryCacheProfileMock) + ); } } From 2bd4b79cac8b8ecbbf1a228dda2e6302dc0be13f Mon Sep 17 00:00:00 2001 From: Antonio Vilar Date: Mon, 10 Nov 2014 16:59:51 +0100 Subject: [PATCH 5/9] Loaded bootstrap script in all phpunit travis configuration files --- tests/travis/pgsql.travis.xml | 2 +- tests/travis/sqlite.travis.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/travis/pgsql.travis.xml b/tests/travis/pgsql.travis.xml index 934a5a5333d..0298a5ce1cb 100644 --- a/tests/travis/pgsql.travis.xml +++ b/tests/travis/pgsql.travis.xml @@ -34,4 +34,4 @@ locking_functional - \ No newline at end of file + diff --git a/tests/travis/sqlite.travis.xml b/tests/travis/sqlite.travis.xml index 6c22315150c..1460f0b8eb5 100644 --- a/tests/travis/sqlite.travis.xml +++ b/tests/travis/sqlite.travis.xml @@ -21,4 +21,4 @@ locking_functional - \ No newline at end of file + From 00f13ca37c286c90d90681d7e628dd0d0af01c35 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Tue, 2 May 2017 14:41:17 +0200 Subject: [PATCH 6/9] Corrected PHPUnit mocks usages --- tests/Doctrine/Tests/DBAL/ConnectionTest.php | 16 ++++++++++------ .../Tests/DBAL/Schema/DB2SchemaManagerTest.php | 14 +++++++------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index 18c0ff0010c..a9b0c6c4230 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -2,9 +2,12 @@ namespace Doctrine\Tests\DBAL; +use Doctrine\Common\Cache\Cache; use Doctrine\Common\EventManager; +use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Driver; use Doctrine\DBAL\Events; use Doctrine\Tests\Mocks\DriverConnectionMock; use Doctrine\Tests\Mocks\DriverMock; @@ -673,7 +676,7 @@ public function testPlatformDetectionIsTriggerOnlyOnceOnRetrievingPlatform() public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCacheQuery() { - $resultCacheDriverMock = $this->getMock('Doctrine\Common\Cache\Cache'); + $resultCacheDriverMock = $this->createMock(Cache::class); $resultCacheDriverMock->expects($this->atLeastOnce()) ->method('fetch') @@ -684,7 +687,8 @@ public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCach $params = array(666); $types = array(\PDO::PARAM_INT); - $queryCacheProfileMock = $this->getMock('Doctrine\DBAL\Cache\QueryCacheProfile'); + /* @var $queryCacheProfileMock QueryCacheProfile|\PHPUnit_Framework_MockObject_MockObject */ + $queryCacheProfileMock = $this->createMock(QueryCacheProfile::class); $queryCacheProfileMock->expects($this->any()) ->method('getResultCacheDriver') @@ -696,10 +700,10 @@ public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCach ->with($query, $params, $types, $this->params) ->will($this->returnValue(array('cacheKey', 'realKey'))); - $conn = new Connection( - $this->params, - $this->getMock('Doctrine\DBAL\Driver') - ); + /* @var $driver Driver */ + $driver = $this->createMock(Driver::class); + + $conn = new Connection($this->params, $driver); $this->assertInstanceOf('Doctrine\DBAL\Cache\ArrayStatement', $conn->executeCacheQuery($query, $params, $types, $queryCacheProfileMock) diff --git a/tests/Doctrine/Tests/DBAL/Schema/DB2SchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Schema/DB2SchemaManagerTest.php index 69da08db4af..d524afe9c45 100644 --- a/tests/Doctrine/Tests/DBAL/Schema/DB2SchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Schema/DB2SchemaManagerTest.php @@ -27,13 +27,13 @@ final class DB2SchemaManagerTest extends \PHPUnit_Framework_TestCase protected function setUp() { $eventManager = new EventManager(); - $driverMock = $this->getMock(Driver::class); - $platform = $this->getMock(DB2Platform::class); - $this->conn = $this->getMock( - Connection::class, - ['fetchAll'], - [['platform' => $platform], $driverMock, new Configuration(), $eventManager] - ); + $driverMock = $this->createMock(Driver::class); + $platform = $this->createMock(DB2Platform::class); + $this->conn = $this + ->getMockBuilder(Connection::class) + ->setMethods(['fetchAll']) + ->setConstructorArgs([['platform' => $platform], $driverMock, new Configuration(), $eventManager]) + ->getMock(); $this->manager = new DB2SchemaManager($this->conn); } From a1b01060879805e7b234529d61101f95d291ab89 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 3 May 2017 13:21:56 +0200 Subject: [PATCH 7/9] CS (alignment, imports, short array syntax, list assignments) --- .../DBAL/Cache/QueryCacheProfileTest.php | 75 +++++++++---------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php b/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php index ea9f3a7487a..88219f215d5 100644 --- a/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php +++ b/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php @@ -4,6 +4,7 @@ use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\Tests\DbalTestCase; +use PDO; class QueryCacheProfileTest extends DbalTestCase { @@ -21,44 +22,44 @@ protected function setUp() public function testShouldUseTheGivenCacheKeyIfPresent() { $query = 'SELECT * FROM foo WHERE bar = ?'; - $params = array(666); - $types = array(\PDO::PARAM_INT); + $params = [666]; + $types = [PDO::PARAM_INT]; $connectionParams = array( - 'dbname' => 'database_name', - 'user' => 'database_user', + 'dbname' => 'database_name', + 'user' => 'database_user', 'password' => 'database_password', - 'host' => 'database_host', - 'driver' => 'database_driver' + 'host' => 'database_host', + 'driver' => 'database_driver' ); - $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + list($cacheKey) = $this->queryCacheProfile->generateCacheKeys( $query, $params, $types, $connectionParams ); - $this->assertEquals(self::CACHE_KEY, $generatedKeys[0], 'The returned cache key should match the given one'); + $this->assertEquals(self::CACHE_KEY, $cacheKey, 'The returned cache key should match the given one'); } public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven() { $query = 'SELECT * FROM foo WHERE bar = ?'; - $params = array(666); - $types = array(\PDO::PARAM_INT); + $params = [666]; + $types = [PDO::PARAM_INT]; $connectionParams = array( - 'dbname' => 'database_name', - 'user' => 'database_user', + 'dbname' => 'database_name', + 'user' => 'database_user', 'password' => 'database_password', - 'host' => 'database_host', - 'driver' => 'database_driver' + 'host' => 'database_host', + 'driver' => 'database_driver' ); $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); - $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + list($cacheKey) = $this->queryCacheProfile->generateCacheKeys( $query, $params, $types, @@ -67,86 +68,78 @@ public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven() $this->assertNotEquals( self::CACHE_KEY, - $generatedKeys[0], + $cacheKey, 'The returned cache key should be generated automatically' ); - $this->assertNotEmpty($generatedKeys[0], 'The generated cache key should not be empty'); + $this->assertNotEmpty($cacheKey, 'The generated cache key should not be empty'); } public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections() { $query = 'SELECT * FROM foo WHERE bar = ?'; - $params = array(666); - $types = array(\PDO::PARAM_INT); + $params = [666]; + $types = [PDO::PARAM_INT]; $connectionParams = array( - 'dbname' => 'database_name', - 'user' => 'database_user', + 'dbname' => 'database_name', + 'user' => 'database_user', 'password' => 'database_password', - 'host' => 'database_host', - 'driver' => 'database_driver' + 'host' => 'database_host', + 'driver' => 'database_driver' ); $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); - $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + list($firstCacheKey) = $this->queryCacheProfile->generateCacheKeys( $query, $params, $types, $connectionParams ); - $firstCacheKey = $generatedKeys[0]; - $connectionParams['host'] = 'a_different_host'; - $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + list($secondCacheKey) = $this->queryCacheProfile->generateCacheKeys( $query, $params, $types, $connectionParams ); - $secondCacheKey = $generatedKeys[0]; - $this->assertNotEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be different'); } public function testShouldGenerateSameKeysIfNoneOfTheParamsChanges() { $query = 'SELECT * FROM foo WHERE bar = ?'; - $params = array(666); - $types = array(\PDO::PARAM_INT); + $params = [666]; + $types = [PDO::PARAM_INT]; $connectionParams = array( - 'dbname' => 'database_name', - 'user' => 'database_user', + 'dbname' => 'database_name', + 'user' => 'database_user', 'password' => 'database_password', - 'host' => 'database_host', - 'driver' => 'database_driver' + 'host' => 'database_host', + 'driver' => 'database_driver' ); $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); - $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + list($firstCacheKey) = $this->queryCacheProfile->generateCacheKeys( $query, $params, $types, $connectionParams ); - $firstCacheKey = $generatedKeys[0]; - - $generatedKeys = $this->queryCacheProfile->generateCacheKeys( + list($secondCacheKey) = $this->queryCacheProfile->generateCacheKeys( $query, $params, $types, $connectionParams ); - $secondCacheKey = $generatedKeys[0]; - $this->assertEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be the same'); } } From db1395b7d2baaa3a027a3727fa34e17a5a85d54f Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 3 May 2017 13:22:18 +0200 Subject: [PATCH 8/9] Always serializing the `connectionParams` (easier) --- lib/Doctrine/DBAL/Cache/QueryCacheProfile.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php b/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php index 7b6010b5b15..19f7a6096f1 100644 --- a/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php +++ b/lib/Doctrine/DBAL/Cache/QueryCacheProfile.php @@ -97,12 +97,12 @@ public function getCacheKey() * * @return array */ - public function generateCacheKeys($query, $params, $types, array $connectionParams = array()) + public function generateCacheKeys($query, $params, $types, array $connectionParams = []) { $realCacheKey = 'query=' . $query . '¶ms=' . serialize($params) . '&types=' . serialize($types) . - (!empty($connectionParams) ? serialize($connectionParams) : ''); + '&connectionParams=' . serialize($connectionParams); // should the key be automatically generated using the inputs or is the cache key set? if ($this->cacheKey === null) { From c23d80a1af818252e3b54139ca44183729eee06b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 3 May 2017 13:23:57 +0200 Subject: [PATCH 9/9] CS (alignment, short array syntax), inlining useless variable --- tests/Doctrine/Tests/DBAL/ConnectionTest.php | 27 +++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index a9b0c6c4230..ccc37ade492 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -11,6 +11,7 @@ use Doctrine\DBAL\Events; use Doctrine\Tests\Mocks\DriverConnectionMock; use Doctrine\Tests\Mocks\DriverMock; +use Doctrine\DBAL\Cache\ArrayStatement; class ConnectionTest extends \Doctrine\Tests\DbalTestCase { @@ -678,35 +679,37 @@ public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCach { $resultCacheDriverMock = $this->createMock(Cache::class); - $resultCacheDriverMock->expects($this->atLeastOnce()) + $resultCacheDriverMock + ->expects($this->atLeastOnce()) ->method('fetch') ->with('cacheKey') - ->will($this->returnValue(array('realKey' => array()))); + ->will($this->returnValue(['realKey' => []])); - $query = 'SELECT * FROM foo WHERE bar = ?'; - $params = array(666); - $types = array(\PDO::PARAM_INT); + $query = 'SELECT * FROM foo WHERE bar = ?'; + $params = [666]; + $types = [\PDO::PARAM_INT]; /* @var $queryCacheProfileMock QueryCacheProfile|\PHPUnit_Framework_MockObject_MockObject */ $queryCacheProfileMock = $this->createMock(QueryCacheProfile::class); - $queryCacheProfileMock->expects($this->any()) + $queryCacheProfileMock + ->expects($this->any()) ->method('getResultCacheDriver') ->will($this->returnValue($resultCacheDriverMock)); // This is our main expectation - $queryCacheProfileMock->expects($this->once()) + $queryCacheProfileMock + ->expects($this->once()) ->method('generateCacheKeys') ->with($query, $params, $types, $this->params) - ->will($this->returnValue(array('cacheKey', 'realKey'))); + ->will($this->returnValue(['cacheKey', 'realKey'])); /* @var $driver Driver */ $driver = $this->createMock(Driver::class); - $conn = new Connection($this->params, $driver); - - $this->assertInstanceOf('Doctrine\DBAL\Cache\ArrayStatement', - $conn->executeCacheQuery($query, $params, $types, $queryCacheProfileMock) + $this->assertInstanceOf( + ArrayStatement::class, + (new Connection($this->params, $driver))->executeCacheQuery($query, $params, $types, $queryCacheProfileMock) ); } }