setup works
This commit is contained in:
parent
42453a9c62
commit
672d8a2255
@ -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,6 +129,12 @@ class ComicPressTagBuilder {
|
||||
}
|
||||
|
||||
if (in_array($current, array('id', 'permalink', 'title', 'timestamp', 'date', 'post'))) {
|
||||
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');
|
||||
}
|
||||
@ -128,8 +142,16 @@ class ComicPressTagBuilder {
|
||||
$state = self::HAS_EXTRACT_METHOD;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($state == self::START_PARSE) {
|
||||
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 {
|
||||
if (in_array($current, array('first', 'previous', 'next', 'last'))) {
|
||||
$post_method = $current;
|
||||
$state = self::HAS_POST_METHOD;
|
||||
@ -138,6 +160,7 @@ class ComicPressTagBuilder {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_null($post_method)) {
|
||||
$methods[] = array($post_method);
|
||||
|
@ -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')
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user