retrieve post info from builder

This commit is contained in:
John Bintz 2010-01-30 10:54:23 -05:00
parent 424382906e
commit ca5d2708d7
2 changed files with 132 additions and 0 deletions

View File

@ -46,8 +46,64 @@ class ComicPressTagBuilder {
if (!isset($arguments[0])) { throw new Exception('Need to specify a category'); }
$this->restrictions[] = array('child_of', $arguments[0]);
break;
case 'id':
return $this->post->ID;
case 'title':
return $this->post->post_title;
case 'timestamp':
return strtotime($this->post->post_date);
case 'permalink':
return get_permalink($this->post->ID);
default:
$methods = $this->parse_method($method);
if (!empty($methods)) {
foreach ($methods as $method_info) {
$new_method = array_shift($method_info);
call_user_func_array(array($this, $new_method), $method_info);
}
}
break;
}
return $this;
}
public function parse_method($method_name) {
$methods = array();
$parts = explode('_', $method_name);
$post_method = null;
while (!empty($parts)) {
$current = strtolower(array_shift($parts));
if (is_null($post_method)) {
if (in_array($current, array('first', 'previous', 'next', 'last'))) {
$post_method = $current;
} else {
break;
}
} else {
if ($current == "in") {
$is_id = false;
if (count($parts) == 1) {
if (is_numeric($parts[0])) {
$methods[] = array('in', $parts[0]);
$is_id = true;
}
}
if (!$is_id) {
$methods[] = array('in', implode('-', $parts));
}
}
}
}
if (!is_null($post_method)) {
$methods[] = array($post_method);
}
return $methods;
}
}

View File

@ -90,4 +90,80 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
$core = call_user_func_array(array($core, $method), $instruction);
}
}
function providerTestStorylineBuilderHandlePost() {
return array(
array('id', 1),
array('title', 'Post title'),
array('timestamp', strtotime('2010-01-01')),
array('permalink', 'the-slug')
);
}
/**
* @dataProvider providerTestStorylineBuilderHandlePost
*/
function testStorylineBuilderHandlePost($info_method, $expected_result) {
$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->once())->method('get_first_post')->with(array('1'))->will($this->returnValue($target_post));
$storyline = new ComicPressStoryline();
$storyline->set_flattened_storyline('0/1');
$core = new ComicPressTagBuilderFactory($dbi);
$core = $core->first_in_1();
$this->assertEquals($expected_result, $core->{$info_method}());
}
function providerTestMethodParser() {
return array(
array(
'last',
array(
array('last')
)
),
array(
'first_in_3',
array(
array('in', 3),
array('first')
)
),
array(
'first_in_category_1',
array(
array('in', 'category-1'),
array('first')
)
)
);
}
/**
* @dataProvider providerTestMethodParser
*/
function testMethodParser($method_name, $expected_pieces) {
foreach (array(
1 => array('cat_name' => 'Test 1', 'category_nicename' => 'category-1', 'category_parent' => 0),
2 => array('cat_name' => 'Test 2', 'category_nicename' => 'category-2', 'category_parent' => 0),
3 => array('cat_name' => 'Test 3', 'category_nicename' => 'category-3', 'category_parent' => 2),
4 => array('cat_name' => 'Test 4', 'category_nicename' => 'category-4', 'category_parent' => 2),
5 => array('cat_name' => 'Test 5', 'category_nicename' => 'category-5', 'category_parent' => 0),
) as $id => $category) {
add_category($id, (object)$category);
}
$this->assertEquals($expected_pieces, ComicPressTagBuilder::parse_method($method_name));
}
}