From ec907484bb38d6c6c74509601427b9c1372ba510 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 15 Nov 2009 10:46:47 -0500 Subject: [PATCH] finish code coverage for db interface --- classes/ComicPressDBInterface.inc | 38 +++++++++-------- test/ComicPressDBInterfaceTest.php | 66 ++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 17 deletions(-) diff --git a/classes/ComicPressDBInterface.inc b/classes/ComicPressDBInterface.inc index b86de6b..3fb4789 100644 --- a/classes/ComicPressDBInterface.inc +++ b/classes/ComicPressDBInterface.inc @@ -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 } ?> diff --git a/test/ComicPressDBInterfaceTest.php b/test/ComicPressDBInterfaceTest.php index ff84e40..77d010d 100644 --- a/test/ComicPressDBInterfaceTest.php +++ b/test/ComicPressDBInterfaceTest.php @@ -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); + } } ?>