complex builder working, sweet
This commit is contained in:
parent
bebcdf1046
commit
3e803220fd
|
@ -36,6 +36,7 @@ class ComicPressTagBuilder {
|
||||||
|
|
||||||
public function __call($method, $arguments) {
|
public function __call($method, $arguments) {
|
||||||
$ok = false;
|
$ok = false;
|
||||||
|
$return = $this;
|
||||||
|
|
||||||
switch ($method) {
|
switch ($method) {
|
||||||
case 'next':
|
case 'next':
|
||||||
|
@ -66,11 +67,11 @@ class ComicPressTagBuilder {
|
||||||
case 'post':
|
case 'post':
|
||||||
return $this->post;
|
return $this->post;
|
||||||
default:
|
default:
|
||||||
$methods = $this->parse_method($method);
|
$methods = $this->parse_method($method, $arguments);
|
||||||
if (!empty($methods)) {
|
if (!empty($methods)) {
|
||||||
foreach ($methods as $method_info) {
|
foreach ($methods as $method_info) {
|
||||||
$new_method = array_shift($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;
|
$ok = true;
|
||||||
}
|
}
|
||||||
|
@ -78,7 +79,7 @@ class ComicPressTagBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($ok) {
|
if ($ok) {
|
||||||
return $this;
|
return $return;
|
||||||
} else {
|
} else {
|
||||||
throw new ComicPressException("${method} isn't allowed at this point");
|
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_POST_METHOD = 'has post method';
|
||||||
const HAS_EXTRACT_METHOD = 'has extract 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();
|
$methods = array();
|
||||||
|
|
||||||
$parts = explode('_', $method_name);
|
$parts = explode('_', $method_name);
|
||||||
|
@ -138,10 +139,17 @@ class ComicPressTagBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (array('post', 'extract') as $prefix) {
|
if (!is_null($post_method)) {
|
||||||
if (!is_null(${"${prefix}_method"})) {
|
$methods[] = array($post_method);
|
||||||
$methods[] = array(${"${prefix}_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;
|
return $methods;
|
||||||
|
|
|
@ -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() {
|
function providerTestStorylineBuilderHandlePost() {
|
||||||
return array(
|
return array(
|
||||||
array('id', 1),
|
array('id', 1),
|
||||||
|
@ -111,20 +133,7 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
|
||||||
* @dataProvider providerTestStorylineBuilderHandlePost
|
* @dataProvider providerTestStorylineBuilderHandlePost
|
||||||
*/
|
*/
|
||||||
function testStorylineBuilderHandlePost($info_method, $expected_result) {
|
function testStorylineBuilderHandlePost($info_method, $expected_result) {
|
||||||
$target_post = (object)array(
|
extract($this->setupStorylineBuilderTest());
|
||||||
'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 = new ComicPressTagBuilderFactory($dbi);
|
||||||
$core = $core->first_in_1();
|
$core = $core->first_in_1();
|
||||||
|
@ -150,20 +159,7 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
|
||||||
* @dataProvider providerTestStorylineBuilderExceptions
|
* @dataProvider providerTestStorylineBuilderExceptions
|
||||||
*/
|
*/
|
||||||
function testStorylineBuilderExceptions($calls) {
|
function testStorylineBuilderExceptions($calls) {
|
||||||
$target_post = (object)array(
|
extract($this->setupStorylineBuilderTest(true));
|
||||||
'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);
|
$core = new ComicPressTagBuilderFactory($dbi);
|
||||||
|
|
||||||
|
@ -216,4 +212,12 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
|
||||||
function testMethodParser($method_name, $expected_pieces) {
|
function testMethodParser($method_name, $expected_pieces) {
|
||||||
$this->assertEquals($expected_pieces, ComicPressTagBuilder::parse_method($method_name));
|
$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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue