bad builder method tests

This commit is contained in:
John Bintz 2010-01-30 11:11:07 -05:00
parent 037669c63e
commit 1f1f3eac96
3 changed files with 66 additions and 3 deletions

View File

@ -294,3 +294,6 @@ class ComicPress {
return false; return false;
} }
} }
class ComicPressException extends Exception {}

View File

@ -35,16 +35,20 @@ class ComicPressTagBuilder {
} }
public function __call($method, $arguments) { public function __call($method, $arguments) {
$ok = false;
switch ($method) { switch ($method) {
case 'next': case 'next':
case 'previous': case 'previous':
case 'first': case 'first':
case 'last': case 'last':
$this->post = call_user_func(array($this->dbi, "get_${method}_post"), $this->storyline->build_from_restrictions($this->restrictions), $this->parent_post); $this->post = call_user_func(array($this->dbi, "get_${method}_post"), $this->storyline->build_from_restrictions($this->restrictions), $this->parent_post);
$ok = true;
break; break;
case 'in': case 'in':
if (!isset($arguments[0])) { throw new Exception('Need to specify a category'); } if (!isset($arguments[0])) { throw new Exception('Need to specify a category'); }
$this->restrictions[] = array('child_of', $arguments[0]); $this->restrictions[] = array('child_of', $arguments[0]);
$ok = true;
break; break;
case 'id': case 'id':
return $this->post->ID; return $this->post->ID;
@ -52,6 +56,11 @@ class ComicPressTagBuilder {
return $this->post->post_title; return $this->post->post_title;
case 'timestamp': case 'timestamp':
return strtotime($this->post->post_date); return strtotime($this->post->post_date);
case 'date':
if (isset($arguments[0])) {
return date($arguments[0], strtotime($this->post->post_date));
}
break;
case 'permalink': case 'permalink':
return get_permalink($this->post->ID); return get_permalink($this->post->ID);
case 'post': case 'post':
@ -63,11 +72,16 @@ class ComicPressTagBuilder {
$new_method = array_shift($method_info); $new_method = array_shift($method_info);
call_user_func_array(array($this, $new_method), $method_info); call_user_func_array(array($this, $new_method), $method_info);
} }
$ok = true;
} }
break; break;
} }
return $this; if ($ok) {
return $this;
} else {
throw new ComicPressException("${method} isn't allowed at this point");
}
} }
public function parse_method($method_name) { public function parse_method($method_name) {

View File

@ -102,7 +102,8 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
'post_title' => 'Post title', 'post_title' => 'Post title',
'post_date' => '2010-01-01', 'post_date' => '2010-01-01',
'guid' => 'the-slug', 'guid' => 'the-slug',
)) )),
array(array('date', 'Ymd'), '20100101'),
); );
} }
@ -128,7 +129,52 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
$core = new ComicPressTagBuilderFactory($dbi); $core = new ComicPressTagBuilderFactory($dbi);
$core = $core->first_in_1(); $core = $core->first_in_1();
$this->assertEquals($expected_result, $core->{$info_method}()); if (is_array($info_method)) {
$method = array_shift($info_method);
$this->assertEquals($expected_result, call_user_func_array(array($core, $method), $info_method));
} else {
$this->assertEquals($expected_result, $core->{$info_method}());
}
}
function providerTestStorylineBuilderExceptions() {
return array(
array(array('bad')),
array(array(array('in', 1), 'bad')),
array(array(array('in', 1), 'date')),
);
}
/**
* @expectedException ComicPressException
* @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');
$core = new ComicPressTagBuilderFactory($dbi);
foreach ($calls as $call) {
if (is_array($call)) {
$method = array_shift($call);
$core = call_user_func_array(array($core, $method), $call);
} else {
$core = $core->{$call}();
}
}
} }
function providerTestMethodParser() { function providerTestMethodParser() {