From 672d8a22558337efd6c4faed11bf5e2286406b97 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 2 Feb 2010 21:06:39 -0500 Subject: [PATCH] setup works --- classes/ComicPressTagBuilder.inc | 41 ++++++++++++++++++++++++------- test/ComicPressTagBuilderTest.php | 31 +++++++++++++++++++++-- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/classes/ComicPressTagBuilder.inc b/classes/ComicPressTagBuilder.inc index 861480b..4c11c3d 100644 --- a/classes/ComicPressTagBuilder.inc +++ b/classes/ComicPressTagBuilder.inc @@ -34,11 +34,17 @@ class ComicPressTagBuilder { $this->parent_post = $parent_post; } + public function _setup_postdata($post) { setup_postdata($post); } + public function __call($method, $arguments) { $ok = false; $return = $this; switch ($method) { + case 'setup': + if (empty($this->post)) { throw new Exception('You need to have retrieved a post for setup to work'); } + $this->_setup_postdata($this->post); + return $this->post; case 'next': case 'previous': case 'first': @@ -88,6 +94,7 @@ class ComicPressTagBuilder { const START_PARSE = 'start parse'; const HAS_POST_METHOD = 'has post method'; const HAS_EXTRACT_METHOD = 'has extract method'; + const IS_SETUP = 'is setup'; public function parse_method($method_name, $extract_method_arguments = null) { $methods = array(); @@ -98,6 +105,7 @@ class ComicPressTagBuilder { $post_method = null; $extract_method = null; + $is_setup = false; while (!empty($parts)) { $current = strtolower(array_shift($parts)); @@ -121,20 +129,35 @@ class ComicPressTagBuilder { } if (in_array($current, array('id', 'permalink', 'title', 'timestamp', 'date', 'post'))) { - if ($state == self::HAS_EXTRACT_METHOD) { - throw new ComicPressException('Only one extract method can be specified'); + if ($is_setup) { + if ($current != 'post') { + throw new ComicPressException('You can only set up a post'); + } + $extract_method = 'setup'; + } else { + if ($state == self::HAS_EXTRACT_METHOD) { + throw new ComicPressException('Only one extract method can be specified'); + } + $extract_method = $current; + $state = self::HAS_EXTRACT_METHOD; } - $extract_method = $current; - $state = self::HAS_EXTRACT_METHOD; } } - if ($state == self::START_PARSE) { - if (in_array($current, array('first', 'previous', 'next', 'last'))) { - $post_method = $current; - $state = self::HAS_POST_METHOD; + if (in_array($state, array(self::START_PARSE, self::IS_SETUP))) { + if ($current == 'setup') { + if ($state == self::IS_SETUP) { + throw new ComicPressException('Setup need only be called once'); + } + $is_setup = true; + $state = self::IS_SETUP; } else { - throw new ComicPressException("${current} isn't allowed at this point"); + if (in_array($current, array('first', 'previous', 'next', 'last'))) { + $post_method = $current; + $state = self::HAS_POST_METHOD; + } else { + throw new ComicPressException("${current} isn't allowed at this point"); + } } } } diff --git a/test/ComicPressTagBuilderTest.php b/test/ComicPressTagBuilderTest.php index 2a36999..4f5bd8f 100644 --- a/test/ComicPressTagBuilderTest.php +++ b/test/ComicPressTagBuilderTest.php @@ -54,13 +54,22 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { ), array('get_first_post', array(2,3,4), 'current-post') ), + array( + array( + array('in', 2), + array('first'), + array('setup') + ), + array('get_first_post', array(2,3,4), 'current-post'), + true + ), ); } /** * @dataProvider providerTestBuilder */ - function testStorylineBuilder($instructions, $expected_dbi_call) { + function testStorylineBuilder($instructions, $expected_dbi_call, $expects_setup_postdata = false) { global $post, $wp_test_expectations; $post = 'current-post'; @@ -70,6 +79,10 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { $expectation = $dbi->expects($this->once())->method($method); call_user_func_array(array($expectation, 'with'), $expected_dbi_call); + if ($expects_setup_postdata) { + call_user_func(array($expectation, 'will'), $this->returnValue('new-post')); + } + $core = new ComicPressTagBuilderFactory($dbi); $storyline = new ComicPressStoryline(); @@ -89,6 +102,10 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { $method = array_shift($instruction); $core = call_user_func_array(array($core, $method), $instruction); } + + if ($expects_setup_postdata) { + $this->assertEquals('new-post', $post); + } } private function setupStorylineBuilderTest($for_exceptions = false) { @@ -203,6 +220,14 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { array('permalink'), ) ), + array( + 'setup_first_post_in_category_1', + array( + array('in', 'category-1'), + array('first'), + array('setup') + ) + ) ); } @@ -216,7 +241,9 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { function providerTestMethodParserExceptions() { return array( array('first_in_'), - array('first_post_id') + array('first_post_id'), + array('setup_setup'), + array('setup_first_permalink') ); }