Skip to content

Commit

Permalink
Make ResultCacheTest tests slightly more logical
Browse files Browse the repository at this point in the history
  • Loading branch information
someniatko authored and lcobucci committed Nov 16, 2019
1 parent 7bcbad0 commit e8f265d
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\Tests\Models\CMS\CmsArticle;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Tests\OrmFunctionalTestCase;
use function count;

/**
* ResultCacheTest
Expand Down Expand Up @@ -112,7 +113,7 @@ public function testUseResultCacheFalse()
$query->setResultCacheDriver($cache);
$query->setResultCacheId('testing_result_cache_id');
$query->useResultCache(false);
$users = $query->getResult();
$query->getResult();

$this->assertFalse($cache->contains('testing_result_cache_id'));

Expand All @@ -129,35 +130,43 @@ public function testUseResultCacheParams()
$sqlCount = count($this->_sqlLoggerStack->queries);
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux WHERE ux.id = ?1');

$query->setParameter(1, 1);
$query->setResultCacheDriver($cache);
$query->useResultCache(true);
$query->getResult();

// these queries should result in cache miss:
$query->setParameter(1, 1);
$query->getResult();
$query->setParameter(1, 2);
$query->getResult();

$this->assertEquals($sqlCount + 2, count($this->_sqlLoggerStack->queries), "Two non-cached queries.");
$this->assertCount(
$sqlCount + 2,
$this->_sqlLoggerStack->queries,
'Two non-cached queries.'
);

// these two queries should actually be cached, as they repeat previous ones:
$query->setParameter(1, 1);
$query->useResultCache(true);
$query->getResult();

$query->setParameter(1, 2);
$query->getResult();

$this->assertEquals($sqlCount + 2, count($this->_sqlLoggerStack->queries), "The next two sql should have been cached, but were not.");
$this->assertCount(
$sqlCount + 2,
$this->_sqlLoggerStack->queries,
'The next two sql queries should have been cached, but were not.'
);
}

public function testEnableResultCache()
public function testEnableResultCache() : void
{
$cache = new ArrayCache();
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');

$query->enableResultCache();
$query->setResultCacheDriver($cache);
$query->setResultCacheId('testing_result_cache_id');
$users = $query->getResult();
$query->getResult();

$this->assertTrue($cache->contains('testing_result_cache_id'));

Expand All @@ -167,41 +176,49 @@ public function testEnableResultCache()
/**
* @group DDC-1026
*/
public function testEnableResultCacheParams()
public function testEnableResultCacheParams() : void
{
$cache = new ArrayCache();
$sqlCount = count($this->_sqlLoggerStack->queries);
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux WHERE ux.id = ?1');

$query->setParameter(1, 1);
$query->setResultCacheDriver($cache);
$query->enableResultCache();
$query->getResult();

// these queries should result in cache miss:
$query->setParameter(1, 1);
$query->getResult();
$query->setParameter(1, 2);
$query->getResult();

$this->assertCount($sqlCount + 2, $this->_sqlLoggerStack->queries, "Two non-cached queries.");
$this->assertCount(
$sqlCount + 2,
$this->_sqlLoggerStack->queries,
'Two non-cached queries.'
);

// these two queries should actually be cached, as they repeat previous ones:
$query->setParameter(1, 1);
$query->enableResultCache();
$query->getResult();

$query->setParameter(1, 2);
$query->getResult();

$this->assertCount($sqlCount + 2, $this->_sqlLoggerStack->queries, "The next two sql should have been cached, but were not.");
$this->assertCount(
$sqlCount + 2,
$this->_sqlLoggerStack->queries,
'The next two sql queries should have been cached, but were not.'
);
}

public function testDisableResultCache()
public function testDisableResultCache() : void
{
$cache = new ArrayCache();
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');

$query->setResultCacheDriver($cache);
$query->setResultCacheId('testing_result_cache_id');
$query->disableResultCache();
$users = $query->getResult();
$query->getResult();

$this->assertFalse($cache->contains('testing_result_cache_id'));

Expand Down

0 comments on commit e8f265d

Please sign in to comment.