From 8c9d8c02d668baaf03795d3576c18fb10e027795 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 11 Nov 2009 21:07:35 -0500 Subject: [PATCH] complete code coverage tests --- classes/ComicPressNavigation.inc | 3 +- classes/ComicPressStoryline.inc | 49 +++++++++++----- test/ComicPressNavigationTest.php | 12 +++- test/ComicPressStorylineTest.php | 97 +++++++++++++++++++++++++++---- 4 files changed, 131 insertions(+), 30 deletions(-) diff --git a/classes/ComicPressNavigation.inc b/classes/ComicPressNavigation.inc index a025259..bcc4a46 100644 --- a/classes/ComicPressNavigation.inc +++ b/classes/ComicPressNavigation.inc @@ -19,8 +19,9 @@ class ComicPressNavigation { if (($result = wp_cache_get($cache_key, 'comicpress')) !== false) { foreach ($result as $key => $post_id) { - $nev[$key] = get_post($post_id); + $nav[$key] = get_post($post_id); } + return $nav; } // global previous/next diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index bda1eee..5f9ac0d 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -46,28 +46,37 @@ class ComicPressStoryline { } } + function _create_structure_key($input) { + $key = 'storyline-structure-'; + if (is_string($input)) { return $key . $input; } + if (is_array($input)) { + $fixed_parts = array(); + foreach ($input as $i) { if (is_string($i)) { $fixed_parts[] = $i; } } + if (!empty($fixed_parts)) { return $key . implode(',', $fixed_parts); } + } + return false; + } + /** * Create a searchable structure from a node list. * @param array $structure The structure to process. * @return boolean True if the structure was valid. */ function create_structure($structure) { - $key = null; - if (is_string($structure)) { - $key = $structure; - $structure = explode(',', $structure); - } else { - if (is_array($structure)) { - $fixed_structure = array(); - foreach ($structure as $s) { - if (!is_array($s)) { $fixed_structure[] = $s; } - } - $key = implode(',', $fixed_structure); - } - } + $key = $this->_create_structure_key($structure); - if (!is_null($key)) { - $key = "storyline-structure-${key}"; + if ($key !== false) { + if (is_string($structure)) { + $structure = explode(',', $structure); + } else { + if (is_array($structure)) { + $fixed_structure = array(); + foreach ($structure as $s) { + if (!is_array($s)) { $fixed_structure[] = $s; } + } + $structure = $fixed_structure; + } + } if (($result = wp_cache_get($key, 'comicpress')) !== false) { $this->_structure = $result; @@ -142,9 +151,12 @@ class ComicPressStoryline { return false; } + // @codeCoverageIgnoreStart function parent($id) { return $this->_get_field('parent', $id); } function previous($id) { return $this->_get_field('previous', $id); } function next($id) { return $this->_get_field('next', $id); } + // @codeCoverageIgnoreEnd + function valid($id) { if (isset($this->_structure[$id])) { return array_keys($this->_structure[$id]); @@ -168,7 +180,9 @@ class ComicPressStoryline { } } } + // @codeCoverageIgnoreStart } while ($has_adjacent); + // @codeCoverageIgnoreEnd return $all_adjacent; } return false; @@ -243,9 +257,11 @@ class ComicPressStoryline { /** * Get a flattened category node list. */ + // @codeCoverageIgnoreStart function get_category_flattened($parent = null) { return $this->flatten_simple_storyline($this->get_category_simple_structure($parent)); } + // @codeCoverageIgnoreEnd /** * Merge a flat simple storyline into a tree. @@ -285,7 +301,6 @@ class ComicPressStoryline { * Integrates a bunch of other things. */ function normalize($flattened_storyline = null, $set = true) { - $comicpress = ComicPress::get_instance(); if (is_null($flattened_storyline)) { $flattened_storyline = $this->get_flattened_storyline(); } @@ -418,7 +433,9 @@ class ComicPressStoryline { } } } + // @codeCoverageIgnoreStart } while ($found_children); + // @codeCoverageIgnoreEnd return $children; } diff --git a/test/ComicPressNavigationTest.php b/test/ComicPressNavigationTest.php index 24da147..d55b111 100644 --- a/test/ComicPressNavigationTest.php +++ b/test/ComicPressNavigationTest.php @@ -17,8 +17,18 @@ class ComicPressNavigationTest extends PHPUnit_Framework_TestCase { $this->assertTrue($this->nav->get_post_nav(false) === false); } + function testGetPostNavPostHasNoID() { + $this->assertTrue($this->nav->get_post_nav((object)array()) === false); + } + function testGetPostNavFromCache() { - $this->markTestIncomplete(); + wp_cache_set('navigation-1', array('test' => 2), 'comicpress'); + + $post_2 = (object)array('ID' => 2); + + wp_insert_post((array)$post_2); + + $this->assertEquals(array('test' => $post_2), $this->nav->get_post_nav((object)array('ID' => 1))); } function testGetPostNav() { diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index c780841..cbe0b21 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -14,23 +14,19 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { function providerTestCreateStorylineStructure() { return array( array( - false, false, false ), array( array('0'), - false, false ), array( array('1'), false, - false ), array( array(array(0,1)), - false, false ), array( @@ -41,6 +37,10 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { array('0/1', '0/1/2'), array('1' => array('next' => 2, 'level' => 1), '2' => array('parent' => 1, 'previous' => 1, 'level' => 2)) ), + array( + array('0/1', false), + false + ), array( array('0/1', '0/1/2', '0/1/3'), array( @@ -97,12 +97,28 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { * @group cache */ function testCreateStorylineStructureFromCache($input, $expected_structure) { - if (is_string($input)) { - wp_cache_set("storyline-structure-${input}", $expected_structure, 'comicpress'); - } + $key = $this->css->_create_structure_key($input); + if ($key !== false) { + wp_cache_set($key, $expected_structure, 'comicpress'); - $this->assertEquals(is_array($expected_structure), $this->css->create_structure($input)); - $this->assertEquals($expected_structure, $this->css->_structure); + $this->assertEquals(is_array($expected_structure), $this->css->create_structure($input)); + $this->assertEquals($expected_structure, $this->css->_structure); + } + } + + function providerTestCreateStructureKey() { + return array( + array(false, false), + array('blah', 'storyline-structure-blah'), + array(array('test', 'test2'), 'storyline-structure-test,test2') + ); + } + + /** + * @dataProvider providerTestCreateStructureKey + */ + function testCreateStructureKey($input, $expected_key) { + $this->assertTrue($expected_key === $this->css->_create_structure_key($input)); } function providerTestGetFields() { @@ -511,11 +527,11 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { return array( array( null, - array(1,2,3,4,5,6,7) + array(1,2,3,4,5,6,8,7) ), array( array(), - array(1,2,3,4,5,6,7) + array(1,2,3,4,5,6,8,7) ), array( array('child_of' => 1), @@ -529,6 +545,14 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { array('child_of' => 1, 'only' => 7), array(1,2,3,7) ), + array( + array('child_of' => 1, '!only' => 2), + array(1, 3) + ), + array( + array('child_of' => 4), + array(4,5,6,8) + ), array( array('level' => 1), array(1,4,7) @@ -554,7 +578,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { function testBuildFromRestrictions($restrictions, $expected_categories) { global $post; - $this->css->set_flattened_storyline('0/1,0/1/2,0/1/3,0/4,0/4/5,0/4/6,0/7'); + $this->css->set_flattened_storyline('0/1,0/1/2,0/1/3,0/4,0/4/5,0/4/6,0/4/6/8,0/7'); wp_set_post_categories(1, array(3)); $post = (object)array('ID' => 1); @@ -562,6 +586,10 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_categories, $this->css->build_from_restrictions($restrictions)); } + function testFindChildrenEmpty() { + $this->assertTrue(false === $this->css->_find_children(null)); + } + function providerTestAllAdjacent() { return array( array(3, 'previous', array(2, 1)), @@ -584,6 +612,51 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_result, $this->css->all_adjacent($start, $direction)); } + + function providerTestNormalize() { + return array( + array(null, false), + array(true, false), + array(null, true), + array(true, true), + ); + } + + /** + * @dataProvider providerTestNormalize + */ + function testNormalize($flattened_storyline, $do_set) { + $css = $this->getMock('ComicPressStoryline', array('get_flattened_storyline', 'get_category_flattened', 'normalize_flattened_storyline', 'set_flattened_storyline')); + $css->expects(is_null($flattened_storyline) ? $this->once() : $this->never())->method('get_flattened_storyline'); + $css->expects($do_set ? $this->once() : $this->never())->method('set_flattened_storyline'); + + $css->normalize($flattened_storyline, $do_set); + } + + function testExclude() { + $css = $this->getMock('ComicPressStoryline', array('_find_blah')); + $css->expects($this->once())->method('_find_blah')->with(1)->will($this->returnValue(array(1,2,3))); + $css->_category_search = array(1,2,3,4,5,6); + $css->_exclude('_find_blah', 1); + $this->assertEquals(array(4,5,6), $css->_category_search); + } + + function providerTestEnsurePostID() { + return array( + array(null, null), + array((object)array('test' => 'blah'), null), + array((object)array('ID' => 1), 1), + array("1", 1), + array("1a", null), + ); + } + + /** + * @dataProvider providerTestEnsurePostID + */ + function testEnsurePostID($thing, $expected_result) { + $this->assertEquals($expected_result, $this->css->_ensure_post_id($thing)); + } } ?> \ No newline at end of file