Skip to content

Commit

Permalink
Add tests for count-cache
Browse files Browse the repository at this point in the history
And account for the query being dirty too
  • Loading branch information
AD7six committed Oct 21, 2015
1 parent 45d3932 commit 28978c2
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -2061,6 +2061,68 @@ public function testCountWithContainCallingAll()
$this->assertEquals(3, $query->count());
}

/**
* Verify that only one count query is issued
* A subsequent request for the count will take the previously
* returned value
*
* @return void
*/
public function testCountCache()
{
$query = $this->getMockBuilder('Cake\ORM\Query')
->disableOriginalConstructor()
->setMethods(['_performCount'])
->getMock();

$query->expects($this->once())
->method('_performCount')
->will($this->returnValue(1));

$result = $query->count();
$this->assertSame(1, $result, 'The result of the sql query should be returned');

$resultAgain = $query->count();
$this->assertSame(1, $resultAgain, 'No query should be issued and the cached value returned');
}

/**
* If the query is dirty the cached value should be ignored
* and a new count query issued
*
* @return void
*/
public function testCountCacheDirty()
{
$query = $this->getMockBuilder('Cake\ORM\Query')
->disableOriginalConstructor()
->setMethods(['_performCount'])
->getMock();

$query->expects($this->at(0))
->method('_performCount')
->will($this->returnValue(1));

$query->expects($this->at(1))
->method('_performCount')
->will($this->returnValue(2));

$query->expects($this->at(2))
->method('_performCount')
->will($this->returnValue(3));

$result = $query->count();
$this->assertSame(1, $result, 'The result of the sql query should be returned');

$query->where(['dirty' => 'cache']);

$secondResult = $query->count();
$this->assertSame(2, $secondResult, 'The query is dirty, the cache should be ignored');

$thirdResult = $query->count();
$this->assertSame(3, $thirdResult, 'The query is still dirty, the cache should be ignored');
}

/**
* Tests that it is possible to apply formatters inside the query builder
* for belongsTo associations
Expand Down

0 comments on commit 28978c2

Please sign in to comment.