complex builder working, sweet

This commit is contained in:
John Bintz 2010-01-30 16:00:41 -05:00
parent bebcdf1046
commit 3e803220fd
2 changed files with 47 additions and 35 deletions

View File

@ -36,6 +36,7 @@ class ComicPressTagBuilder {
public function __call($method, $arguments) {
$ok = false;
$return = $this;
switch ($method) {
case 'next':
@ -66,11 +67,11 @@ class ComicPressTagBuilder {
case 'post':
return $this->post;
default:
$methods = $this->parse_method($method);
$methods = $this->parse_method($method, $arguments);
if (!empty($methods)) {
foreach ($methods as $method_info) {
$new_method = array_shift($method_info);
call_user_func_array(array($this, $new_method), $method_info);
$return = call_user_func_array(array($return, $new_method), $method_info);
}
$ok = true;
}
@ -78,7 +79,7 @@ class ComicPressTagBuilder {
}
if ($ok) {
return $this;
return $return;
} else {
throw new ComicPressException("${method} isn't allowed at this point");
}
@ -88,7 +89,7 @@ class ComicPressTagBuilder {
const HAS_POST_METHOD = 'has post method';
const HAS_EXTRACT_METHOD = 'has extract method';
public function parse_method($method_name) {
public function parse_method($method_name, $extract_method_arguments = null) {
$methods = array();
$parts = explode('_', $method_name);
@ -138,10 +139,17 @@ class ComicPressTagBuilder {
}
}
foreach (array('post', 'extract') as $prefix) {
if (!is_null(${"${prefix}_method"})) {
$methods[] = array(${"${prefix}_method"});
if (!is_null($post_method)) {
$methods[] = array($post_method);
}
if (!is_null($extract_method)) {
$extract_method = array($extract_method);
if (is_array($extract_method_arguments)) {
$extract_method = array_merge($extract_method, $extract_method_arguments);
}
$methods[] = $extract_method;
}
return $methods;

View File

@ -91,6 +91,28 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
}
}
private function setupStorylineBuilderTest($for_exceptions = false) {
$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'));
$expectation = $dbi->expects($this->any())->method('get_first_post');
if (!$for_exceptions) {
$expectation->with(array('1'))->will($this->returnValue($target_post));
}
$storyline = new ComicPressStoryline();
$storyline->set_flattened_storyline('0/1');
return compact('target_post', 'dbi', 'storyline');
}
function providerTestStorylineBuilderHandlePost() {
return array(
array('id', 1),
@ -111,20 +133,7 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
* @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');
extract($this->setupStorylineBuilderTest());
$core = new ComicPressTagBuilderFactory($dbi);
$core = $core->first_in_1();
@ -150,20 +159,7 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
* @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');
extract($this->setupStorylineBuilderTest(true));
$core = new ComicPressTagBuilderFactory($dbi);
@ -216,4 +212,12 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
function testMethodParser($method_name, $expected_pieces) {
$this->assertEquals($expected_pieces, ComicPressTagBuilder::parse_method($method_name));
}
function testMethodParserWithParam() {
extract($this->setupStorylineBuilderTest());
$core = new ComicPressTagBuilderFactory($dbi);
$this->assertEquals('2010-01-01', $core->first_date_in_1('Y-m-d'));
}
}