Skip to content

Commit

Permalink
Improved Tests for Sitemap Behavior findSitemapRecords
Browse files Browse the repository at this point in the history
Tests are fully isolated and unit style as opposed to integration style. Elimates one source of reliance on Fixtures and Test Database.

Signed-off-by: Justin Yost <justin@loadsys.com>
  • Loading branch information
justinyost committed Nov 10, 2016
1 parent 1a06624 commit 0a85185
Showing 1 changed file with 141 additions and 41 deletions.
182 changes: 141 additions & 41 deletions tests/TestCase/Model/Behavior/SitemapBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,62 +140,162 @@ public function testReturnUrlForEntity() {
}

/**
* Test findSitemapRecords method
* Test ::findSitemapRecords when the Fields config is set.
*
* @param array $sitemapConfig The Configs for the Sitemap Behavior.
* @param int $expectedCount The expected number of returning results from the find.
* @return void
* @dataProvider providerFindSitemapRecords
*/
public function testFindSitemapRecords($sitemapConfig, $expectedCount) {
$this->Pages->removeBehavior('Sitemap');
$this->Pages->addBehavior('Sitemap.Sitemap', $sitemapConfig);
public function testFindSitemapRecordsFields() {
$configs = [
'conditions' => [
'field1' => 'value1',
],
'cacheConfigKey' => 'cache-key-asdf',
'order' => [
'field2' => 'value2',
],
'fields' => [
'field3' => 'value3',
],
];
$options = [
'foo' => 'baz',
];

$TableMock = $this
->getMockBuilder('\Cake\ORM\Table')
->disableOriginalConstructor()
->getMock();
$SitemapBehavior = $this
->getMockBuilder('\Sitemap\Model\Behavior\SitemapBehavior')
->setMethods(['mapResults'])
->setConstructorArgs([$TableMock, $configs])
->getMock();

$QueryMock = $this->getMockBuilder('\Cake\ORM\Query')
->setMethods([
'where',
'cache',
'order',
'formatResults',
'select',
'repository',
'alias',
])
->disableOriginalConstructor()
->getMock();

$query = $this->Pages->find('forSitemap');
$count = $query->count();
$QueryMock->expects($this->once())
->method('where')
->with($configs['conditions'])
->will($this->returnSelf());
$QueryMock->expects($this->once())
->method('repository')
->with()
->will($this->returnSelf());
$QueryMock->expects($this->once())
->method('alias')
->with()
->will($this->returnValue('alias-canary'));
$QueryMock->expects($this->once())
->method('cache')
->with('sitemap_alias-canary', $configs['cacheConfigKey'])
->will($this->returnSelf());
$QueryMock->expects($this->once())
->method('order')
->with($configs['order'])
->will($this->returnSelf());
$QueryMock->expects($this->once())
->method('formatResults')
->with($this->isInstanceOf('closure'))
->will($this->returnSelf());
$QueryMock->expects($this->once())
->method('select')
->with($configs['fields'])
->will($this->returnValue('canary'));

$output = $SitemapBehavior->findSitemapRecords($QueryMock, $options);
$this->assertEquals(
$expectedCount,
$count,
"The count of the pages should be equal to {$expectedCount}"
'canary',
$output,
'The output from ::findSitemapRecords should be our mocked response from the Query Object.'
);
}

/**
* DataProvider for testFindSitemapRecords.
* Test ::findSitemapRecords when the Fields config is not set.
*
* @return array Data inputs for testFindSitemapRecords.
* @return void
*/
public function providerFindSitemapRecords() {
return [
'Empty Config Options' => [
[],
5,
public function testFindSitemapRecordsNoFields() {
$configs = [
'conditions' => [
'field1' => 'value1',
],
'Non Conditional Config Options' => [
[
'lastmod' => 'modified_date',
],
5,
'cacheConfigKey' => 'cache-key-asdf',
'order' => [
'field2' => 'value2',
],
];
$options = [
'foo' => 'baz',
];

'Is Indexed TRUE Config Options' => [
[
'conditions' => [
'is_indexed' => true,
],
],
4,
],
$TableMock = $this
->getMockBuilder('\Cake\ORM\Table')
->disableOriginalConstructor()
->getMock();
$SitemapBehavior = $this
->getMockBuilder('\Sitemap\Model\Behavior\SitemapBehavior')
->setMethods(['mapResults'])
->setConstructorArgs([$TableMock, $configs])
->getMock();

'Is Indexed FALSE Config Options' => [
[
'conditions' => [
'is_indexed' => false,
],
],
1,
],
];
$QueryMock = $this->getMockBuilder('\Cake\ORM\Query')
->setMethods([
'where',
'cache',
'order',
'formatResults',
'select',
'repository',
'alias',
])
->disableOriginalConstructor()
->getMock();

$QueryMock->expects($this->once())
->method('where')
->with($configs['conditions'])
->will($this->returnSelf());
$QueryMock->expects($this->once())
->method('repository')
->with()
->will($this->returnSelf());
$QueryMock->expects($this->once())
->method('alias')
->with()
->will($this->returnValue('alias-canary'));
$QueryMock->expects($this->once())
->method('cache')
->with('sitemap_alias-canary', $configs['cacheConfigKey'])
->will($this->returnSelf());
$QueryMock->expects($this->once())
->method('order')
->with($configs['order'])
->will($this->returnSelf());
$QueryMock->expects($this->once())
->method('formatResults')
->with($this->isInstanceOf('closure'))
->will($this->returnValue('canary'));
$QueryMock->expects($this->never())
->method('select');

$output = $SitemapBehavior->findSitemapRecords($QueryMock, $options);
$this->assertEquals(
'canary',
$output,
'The output from ::findSitemapRecords should be our mocked response from the Query Object.'
);
}

/**
Expand Down

0 comments on commit 0a85185

Please sign in to comment.