-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fix/#713-prevent-result-cache-key-collisions'
Close #713 DBAL-1030
- Loading branch information
Showing
7 changed files
with
205 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\DBAL\Cache; | ||
|
||
use Doctrine\DBAL\Cache\QueryCacheProfile; | ||
use Doctrine\Tests\DbalTestCase; | ||
use PDO; | ||
|
||
class QueryCacheProfileTest extends DbalTestCase | ||
{ | ||
const LIFETIME = 3600; | ||
const CACHE_KEY = 'user_specified_cache_key'; | ||
|
||
/** @var QueryCacheProfile */ | ||
private $queryCacheProfile; | ||
|
||
protected function setUp() | ||
{ | ||
$this->queryCacheProfile = new QueryCacheProfile(self::LIFETIME, self::CACHE_KEY); | ||
} | ||
|
||
public function testShouldUseTheGivenCacheKeyIfPresent() | ||
{ | ||
$query = 'SELECT * FROM foo WHERE bar = ?'; | ||
$params = [666]; | ||
$types = [PDO::PARAM_INT]; | ||
|
||
$connectionParams = array( | ||
'dbname' => 'database_name', | ||
'user' => 'database_user', | ||
'password' => 'database_password', | ||
'host' => 'database_host', | ||
'driver' => 'database_driver' | ||
); | ||
|
||
list($cacheKey) = $this->queryCacheProfile->generateCacheKeys( | ||
$query, | ||
$params, | ||
$types, | ||
$connectionParams | ||
); | ||
|
||
$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 = [666]; | ||
$types = [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); | ||
|
||
list($cacheKey) = $this->queryCacheProfile->generateCacheKeys( | ||
$query, | ||
$params, | ||
$types, | ||
$connectionParams | ||
); | ||
|
||
$this->assertNotEquals( | ||
self::CACHE_KEY, | ||
$cacheKey, | ||
'The returned cache key should be generated automatically' | ||
); | ||
|
||
$this->assertNotEmpty($cacheKey, 'The generated cache key should not be empty'); | ||
} | ||
|
||
public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections() | ||
{ | ||
$query = 'SELECT * FROM foo WHERE bar = ?'; | ||
$params = [666]; | ||
$types = [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); | ||
|
||
list($firstCacheKey) = $this->queryCacheProfile->generateCacheKeys( | ||
$query, | ||
$params, | ||
$types, | ||
$connectionParams | ||
); | ||
|
||
$connectionParams['host'] = 'a_different_host'; | ||
|
||
list($secondCacheKey) = $this->queryCacheProfile->generateCacheKeys( | ||
$query, | ||
$params, | ||
$types, | ||
$connectionParams | ||
); | ||
|
||
$this->assertNotEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be different'); | ||
} | ||
|
||
public function testShouldGenerateSameKeysIfNoneOfTheParamsChanges() | ||
{ | ||
$query = 'SELECT * FROM foo WHERE bar = ?'; | ||
$params = [666]; | ||
$types = [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); | ||
|
||
list($firstCacheKey) = $this->queryCacheProfile->generateCacheKeys( | ||
$query, | ||
$params, | ||
$types, | ||
$connectionParams | ||
); | ||
|
||
list($secondCacheKey) = $this->queryCacheProfile->generateCacheKeys( | ||
$query, | ||
$params, | ||
$types, | ||
$connectionParams | ||
); | ||
|
||
$this->assertEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be the same'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,4 @@ | |
<group>locking_functional</group> | ||
</exclude> | ||
</groups> | ||
</phpunit> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,4 @@ | |
<group>locking_functional</group> | ||
</exclude> | ||
</groups> | ||
</phpunit> | ||
</phpunit> |