finish code coverage for db interface

This commit is contained in:
John Bintz 2009-11-15 10:46:47 -05:00
parent 96eb1e0695
commit ec907484bb
2 changed files with 87 additions and 17 deletions

View File

@ -12,12 +12,30 @@ class ComicPressDBInterface {
function _get_categories_to_exclude($categories = null) {
if (is_array($categories)) {
return array_diff(get_all_category_ids(), $categories);
return array_values(array_diff(get_all_category_ids(), $categories));
} else {
return array();
}
}
function _prepare_wp_query() {
global $wp_query;
$this->is_single = $wp_query->is_single;
$this->in_the_loop = $wp_query->in_the_loop;
$wp_query->is_single = $wp_query->in_the_loop = true;
}
function _reset_wp_query() {
global $wp_query;
$wp_query->is_single = $this->is_single;
$wp_query->in_the_loop = $this->in_the_loop;
}
// @codeCoverageIgnoreStart
/**
* Find the terminal post in a specific category.
*/
@ -73,22 +91,6 @@ class ComicPressDBInterface {
return empty($result) ? false : $result;
}
function _prepare_wp_query() {
global $wp_query;
$this->is_single = $wp_query->is_single;
$this->in_the_loop = $wp_query->in_the_loop;
$wp_query->is_single = $wp_query->in_the_loop = true;
}
function _reset_wp_query() {
global $wp_query;
$wp_query->is_single = $this->is_single;
$wp_query->in_the_loop = $this->in_the_loop;
}
/**
* Get the previous comic from the current one.
*/
@ -98,6 +100,8 @@ class ComicPressDBInterface {
* Get the next comic from the current one.
*/
function get_next_post($categories = null, $override_post = null) { return $this->get_adjacent_post($categories, true, $override_post); }
// @codeCoverageIgnoreEnd
}
?>

View File

@ -5,6 +5,14 @@ require_once('PHPUnit/Framework.php');
require_once('ComicPressDBInterface.inc');
class ComicPressDBInterfaceTest extends PHPUnit_Framework_TestCase {
function setUp() {
global $wp_query;
_reset_wp();
unset($wp_query);
}
function testSingleton() {
$a = ComicPressDBInterface::get_instance();
$this->assertTrue(!isset($a->test));
@ -14,6 +22,64 @@ class ComicPressDBInterfaceTest extends PHPUnit_Framework_TestCase {
$b = ComicPressDBInterface::get_instance();
$this->assertEquals("test", $b->test);
}
function providerTestGetCategoriesToExclude() {
return array(
array(null, array()),
array(array(), array(1,2,3)),
array(array(1), array(2,3)),
);
}
/**
* @dataProvider providerTestGetCategoriesToExclude
*/
function testGetCategoriesToExclude($input, $expected_output) {
add_category(1, (object)array('slug' => 'one'));
add_category(2, (object)array('slug' => 'one'));
add_category(3, (object)array('slug' => 'one'));
$dbi = new ComicPressDBInterface();
$this->assertEquals($expected_output, $dbi->_get_categories_to_exclude($input));
}
function testPrepareWPQuery() {
global $wp_query;
$dbi = new ComicPressDBInterface();
$wp_query = (object)array(
'is_single' => false,
'in_the_loop' => false
);
$dbi->_prepare_wp_query();
$this->assertTrue($wp_query->is_single);
$this->assertTrue($wp_query->in_the_loop);
$this->assertTrue(false === $dbi->is_single);
$this->assertTrue(false === $dbi->in_the_loop);
}
function testResetWPQuery() {
global $wp_query;
$dbi = new ComicPressDBInterface();
$wp_query = (object)array(
'is_single' => true,
'in_the_loop' => true
);
$dbi->is_single = false;
$dbi->in_the_loop = false;
$dbi->_Reset_wp_query();
$this->assertTrue(false === $wp_query->is_single);
$this->assertTrue(false === $wp_query->in_the_loop);
}
}
?>