Commit d19567bc authored by Antonio Vilar's avatar Antonio Vilar Committed by Marco Pivetta

Added new tests for QueryCacheProfile and Connection

parent a60be246
...@@ -20,10 +20,7 @@ class QueryCacheProfileTest extends DbalTestCase ...@@ -20,10 +20,7 @@ class QueryCacheProfileTest extends DbalTestCase
$this->queryCacheProfile = new QueryCacheProfile(self::LIFETIME, self::CACHE_KEY); $this->queryCacheProfile = new QueryCacheProfile(self::LIFETIME, self::CACHE_KEY);
} }
/** public function testShouldUseTheGivenCacheKeyIfPresent()
* @test
*/
public function it_should_use_the_given_cache_key_if_present()
{ {
$query = 'SELECT * FROM foo WHERE bar = ?'; $query = 'SELECT * FROM foo WHERE bar = ?';
$params = array(666); $params = array(666);
...@@ -37,20 +34,43 @@ class QueryCacheProfileTest extends DbalTestCase ...@@ -37,20 +34,43 @@ class QueryCacheProfileTest extends DbalTestCase
'driver' => 'database_driver' 'driver' => 'database_driver'
); );
$expectedRealCacheKey = 'query=SELECT * FROM foo WHERE bar = ?&params=a:1:{i:0;i:666;}&types=a:1:{i:0;' $generatedKeys = $this->queryCacheProfile->generateCacheKeys(
. 'i:1;}a:5:{s:6:"dbname";s:13:"database_name";s:4:"user";s:13:"database_user";s:8:"password";s:17:"' $query,
. 'database_password";s:4:"host";s:13:"database_host";s:6:"driver";s:15:"database_driver";}'; $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->assertNotEmpty($generatedKeys[0], 'The generated cache key should not be empty');
$this->assertEquals($expectedRealCacheKey, $cacheKeysResult[1]);
} }
/** public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections()
* @test
*/
public function it_should_generate_an_automatic_key_if_no_key_has_been_specified()
{ {
$query = 'SELECT * FROM foo WHERE bar = ?'; $query = 'SELECT * FROM foo WHERE bar = ?';
$params = array(666); $params = array(666);
...@@ -64,15 +84,65 @@ class QueryCacheProfileTest extends DbalTestCase ...@@ -64,15 +84,65 @@ class QueryCacheProfileTest extends DbalTestCase
'driver' => 'database_driver' 'driver' => 'database_driver'
); );
$expectedRealCacheKey = 'query=SELECT * FROM foo WHERE bar = ?&params=a:1:{i:0;i:666;}&types=a:1:{i:0;' $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);
. '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
);
$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); $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($firstCacheKey, $secondCacheKey, 'Cache keys should be the same');
$this->assertEquals($expectedRealCacheKey, $cacheKeysResult[1]);
} }
} }
...@@ -670,4 +670,38 @@ class ConnectionTest extends \Doctrine\Tests\DbalTestCase ...@@ -670,4 +670,38 @@ class ConnectionTest extends \Doctrine\Tests\DbalTestCase
$this->assertSame($platformMock, $connection->getDatabasePlatform()); $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);
}
} }
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