From 99939dac4cc2b1bf68aa30d75726302d9834684e Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 9 Nov 2009 20:54:45 -0500 Subject: [PATCH] add all adjacent --- classes/ComicPressStoryline.inc | 14 ++++++++++++++ test/ComicPressStorylineTest.php | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index 17f6cf2..046ea1f 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -153,6 +153,20 @@ class ComicPressStoryline { return false; } + function all_adjacent($id, $direction) { + if (isset($this->_structure[$id])) { + $all_adjacent = array(); + do { + if ($has_adjacent = isset($this->_structure[$id][$direction])) { + $all_adjacent[] = $this->_structure[$id][$direction]; + $id = $this->_structure[$id][$direction]; + } + } while ($has_adjacent); + return $all_adjacent; + } + return false; + } + /** * Get the valid navigation directions for a particular post. */ diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index 805ffc6..c780841 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -561,6 +561,29 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_categories, $this->css->build_from_restrictions($restrictions)); } + + function providerTestAllAdjacent() { + return array( + array(3, 'previous', array(2, 1)), + array(2, 'next', array(3, 4)), + array(4, 'next', array()), + array(5, 'next', false) + ); + } + + /** + * @dataProvider providerTestAllAdjacent + */ + function testAllAdjacent($start, $direction, $expected_result) { + $this->css->_structure = array( + '1' => array('next' => 2), + '2' => array('previous' => 1, 'parent' => 1, 'next' => 3), + '3' => array('previous' => 2, 'parent' => 1, 'next' => 4), + '4' => array('previous' => 3, 'parent' => 1) + ); + + $this->assertEquals($expected_result, $this->css->all_adjacent($start, $direction)); + } } ?> \ No newline at end of file