setup works
This commit is contained in:
parent
42453a9c62
commit
672d8a2255
|
@ -34,11 +34,17 @@ class ComicPressTagBuilder {
|
||||||
$this->parent_post = $parent_post;
|
$this->parent_post = $parent_post;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function _setup_postdata($post) { setup_postdata($post); }
|
||||||
|
|
||||||
public function __call($method, $arguments) {
|
public function __call($method, $arguments) {
|
||||||
$ok = false;
|
$ok = false;
|
||||||
$return = $this;
|
$return = $this;
|
||||||
|
|
||||||
switch ($method) {
|
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 'next':
|
||||||
case 'previous':
|
case 'previous':
|
||||||
case 'first':
|
case 'first':
|
||||||
|
@ -88,6 +94,7 @@ class ComicPressTagBuilder {
|
||||||
const START_PARSE = 'start parse';
|
const START_PARSE = 'start parse';
|
||||||
const HAS_POST_METHOD = 'has post method';
|
const HAS_POST_METHOD = 'has post method';
|
||||||
const HAS_EXTRACT_METHOD = 'has extract method';
|
const HAS_EXTRACT_METHOD = 'has extract method';
|
||||||
|
const IS_SETUP = 'is setup';
|
||||||
|
|
||||||
public function parse_method($method_name, $extract_method_arguments = null) {
|
public function parse_method($method_name, $extract_method_arguments = null) {
|
||||||
$methods = array();
|
$methods = array();
|
||||||
|
@ -98,6 +105,7 @@ class ComicPressTagBuilder {
|
||||||
|
|
||||||
$post_method = null;
|
$post_method = null;
|
||||||
$extract_method = null;
|
$extract_method = null;
|
||||||
|
$is_setup = false;
|
||||||
|
|
||||||
while (!empty($parts)) {
|
while (!empty($parts)) {
|
||||||
$current = strtolower(array_shift($parts));
|
$current = strtolower(array_shift($parts));
|
||||||
|
@ -121,20 +129,35 @@ class ComicPressTagBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (in_array($current, array('id', 'permalink', 'title', 'timestamp', 'date', 'post'))) {
|
if (in_array($current, array('id', 'permalink', 'title', 'timestamp', 'date', 'post'))) {
|
||||||
if ($state == self::HAS_EXTRACT_METHOD) {
|
if ($is_setup) {
|
||||||
throw new ComicPressException('Only one extract method can be specified');
|
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($state, array(self::START_PARSE, self::IS_SETUP))) {
|
||||||
if (in_array($current, array('first', 'previous', 'next', 'last'))) {
|
if ($current == 'setup') {
|
||||||
$post_method = $current;
|
if ($state == self::IS_SETUP) {
|
||||||
$state = self::HAS_POST_METHOD;
|
throw new ComicPressException('Setup need only be called once');
|
||||||
|
}
|
||||||
|
$is_setup = true;
|
||||||
|
$state = self::IS_SETUP;
|
||||||
} else {
|
} 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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,13 +54,22 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
|
||||||
),
|
),
|
||||||
array('get_first_post', array(2,3,4), 'current-post')
|
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
|
* @dataProvider providerTestBuilder
|
||||||
*/
|
*/
|
||||||
function testStorylineBuilder($instructions, $expected_dbi_call) {
|
function testStorylineBuilder($instructions, $expected_dbi_call, $expects_setup_postdata = false) {
|
||||||
global $post, $wp_test_expectations;
|
global $post, $wp_test_expectations;
|
||||||
$post = 'current-post';
|
$post = 'current-post';
|
||||||
|
|
||||||
|
@ -70,6 +79,10 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
|
||||||
$expectation = $dbi->expects($this->once())->method($method);
|
$expectation = $dbi->expects($this->once())->method($method);
|
||||||
call_user_func_array(array($expectation, 'with'), $expected_dbi_call);
|
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);
|
$core = new ComicPressTagBuilderFactory($dbi);
|
||||||
|
|
||||||
$storyline = new ComicPressStoryline();
|
$storyline = new ComicPressStoryline();
|
||||||
|
@ -89,6 +102,10 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
|
||||||
$method = array_shift($instruction);
|
$method = array_shift($instruction);
|
||||||
$core = call_user_func_array(array($core, $method), $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) {
|
private function setupStorylineBuilderTest($for_exceptions = false) {
|
||||||
|
@ -203,6 +220,14 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
|
||||||
array('permalink'),
|
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() {
|
function providerTestMethodParserExceptions() {
|
||||||
return array(
|
return array(
|
||||||
array('first_in_'),
|
array('first_in_'),
|
||||||
array('first_post_id')
|
array('first_post_id'),
|
||||||
|
array('setup_setup'),
|
||||||
|
array('setup_first_permalink')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue