diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index cadcbc1..ba7a956 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -294,3 +294,6 @@ class ComicPress { return false; } } + +class ComicPressException extends Exception {} + diff --git a/classes/ComicPressTagBuilder.inc b/classes/ComicPressTagBuilder.inc index 4f9328c..54606d8 100644 --- a/classes/ComicPressTagBuilder.inc +++ b/classes/ComicPressTagBuilder.inc @@ -35,16 +35,20 @@ class ComicPressTagBuilder { } public function __call($method, $arguments) { + $ok = false; + switch ($method) { case 'next': case 'previous': case 'first': case 'last': $this->post = call_user_func(array($this->dbi, "get_${method}_post"), $this->storyline->build_from_restrictions($this->restrictions), $this->parent_post); + $ok = true; break; case 'in': if (!isset($arguments[0])) { throw new Exception('Need to specify a category'); } $this->restrictions[] = array('child_of', $arguments[0]); + $ok = true; break; case 'id': return $this->post->ID; @@ -52,6 +56,11 @@ class ComicPressTagBuilder { return $this->post->post_title; case 'timestamp': return strtotime($this->post->post_date); + case 'date': + if (isset($arguments[0])) { + return date($arguments[0], strtotime($this->post->post_date)); + } + break; case 'permalink': return get_permalink($this->post->ID); case 'post': @@ -63,11 +72,16 @@ class ComicPressTagBuilder { $new_method = array_shift($method_info); call_user_func_array(array($this, $new_method), $method_info); } + $ok = true; } break; } - return $this; + if ($ok) { + return $this; + } else { + throw new ComicPressException("${method} isn't allowed at this point"); + } } public function parse_method($method_name) { diff --git a/test/ComicPressTagBuilderTest.php b/test/ComicPressTagBuilderTest.php index 57eb308..987bb3b 100644 --- a/test/ComicPressTagBuilderTest.php +++ b/test/ComicPressTagBuilderTest.php @@ -102,7 +102,8 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { 'post_title' => 'Post title', 'post_date' => '2010-01-01', 'guid' => 'the-slug', - )) + )), + array(array('date', 'Ymd'), '20100101'), ); } @@ -128,7 +129,52 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { $core = new ComicPressTagBuilderFactory($dbi); $core = $core->first_in_1(); - $this->assertEquals($expected_result, $core->{$info_method}()); + if (is_array($info_method)) { + $method = array_shift($info_method); + $this->assertEquals($expected_result, call_user_func_array(array($core, $method), $info_method)); + } else { + $this->assertEquals($expected_result, $core->{$info_method}()); + } + } + + function providerTestStorylineBuilderExceptions() { + return array( + array(array('bad')), + array(array(array('in', 1), 'bad')), + array(array(array('in', 1), 'date')), + ); + } + + /** + * @expectedException ComicPressException + * @dataProvider providerTestStorylineBuilderExceptions + */ + function testStorylineBuilderExceptions($calls) { + $target_post = (object)array( + 'ID' => 1, + 'post_title' => 'Post title', + 'post_date' => '2010-01-01', + 'guid' => 'the-slug', + ); + + wp_insert_post($target_post); + + $dbi = $this->getMock('ComicPressDBInterface', array('get_first_post')); + $dbi->expects($this->any())->method('get_first_post')->will($this->returnValue($target_post)); + + $storyline = new ComicPressStoryline(); + $storyline->set_flattened_storyline('0/1'); + + $core = new ComicPressTagBuilderFactory($dbi); + + foreach ($calls as $call) { + if (is_array($call)) { + $method = array_shift($call); + $core = call_user_func_array(array($core, $method), $call); + } else { + $core = $core->{$call}(); + } + } } function providerTestMethodParser() {