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;
}
}
class ComicPressException extends Exception {}

View File

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

View File

@ -102,7 +102,8 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
'post_title' => 'Post title',
'post_date' => '2010-01-01',
'guid' => 'the-slug',
))
)),
array(array('date', 'Ymd'), '20100101'),
);
}
@ -128,8 +129,53 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
$core = new ComicPressTagBuilderFactory($dbi);
$core = $core->first_in_1();
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() {
return array(