start move to backend factories

This commit is contained in:
John Bintz 2009-12-06 16:09:32 -05:00
parent 9525ed566a
commit 57c83c74fb
4 changed files with 43 additions and 95 deletions

View File

@ -154,8 +154,8 @@ class ComicPress {
$this->normalize_image_size_options(); $this->normalize_image_size_options();
foreach (get_declared_classes() as $class) { foreach (get_declared_classes() as $class) {
if (preg_match('#^ComicPressBackend.+$#', $class) > 0) { if (preg_match('#^ComicPressBackend.+Factory$#', $class) > 0) {
$this->backends[] = $class; $this->backends[] = new $class();
} }
} }
} }

View File

@ -6,38 +6,11 @@ class ComicPressBackendAttachment extends ComicPressBackend {
var $root_id = 'attachment'; var $root_id = 'attachment';
var $source_name = "Post attachment"; var $source_name = "Post attachment";
function generate_from_post($post) {
$result = array();
if (is_object($post)) {
if (isset($post->ID)) {
$children = get_children(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if (!empty($children)) {
foreach ($children as $child) {
$meta = get_post_meta($child->ID, 'comicpress', true);
if (isset($meta['managed'])) {
if ($meta['managed']) {
$result[] = new ComicPressBackendAttachment($child);
}
}
}
}
}
}
return $result;
}
function ComicPressBackendAttachment($attachment = null) { function ComicPressBackendAttachment($attachment = null) {
if (!is_null($attachment)) {
$this->attachment = $attachment; $this->attachment = $attachment;
$this->id = sprintf('%s-%d', $this->root_id, $this->attachment->ID); $this->id = sprintf('%s-%d', $this->root_id, $this->attachment->ID);
$this->type = get_post_meta($this->attachment->ID, 'comic_image_type', true); $this->type = get_post_meta($this->attachment->ID, 'comic_image_type', true);
} }
}
function dims($size = 'comic') { function dims($size = 'comic') {
$comicpress = ComicPress::get_instance(); $comicpress = ComicPress::get_instance();
@ -89,6 +62,34 @@ class ComicPressBackendAttachment extends ComicPressBackend {
} }
return $info; return $info;
} }
}
class ComicPressBackendAttachmentFactory {
function generate_from_post($post) {
$result = array();
if (is_object($post)) {
if (isset($post->ID)) {
$children = get_children(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if (!empty($children)) {
foreach ($children as $child) {
$meta = get_post_meta($child->ID, 'comicpress', true);
if (isset($meta['managed'])) {
if ($meta['managed']) {
$result[] = new ComicPressBackendAttachment($child);
}
}
}
}
}
}
return $result;
}
function generate_from_id($id) { function generate_from_id($id) {
if (strpos($id, 'attachment-') === 0) { if (strpos($id, 'attachment-') === 0) {
@ -108,5 +109,3 @@ class ComicPressBackendAttachment extends ComicPressBackend {
return false; return false;
} }
} }
?>

View File

@ -86,4 +86,14 @@ class ComicPressBackendURL extends ComicPressBackend {
} }
return false; return false;
} }
function post_meta_box($post_id) {
echo "hello";
} }
function admin_menu() {
add_meta_box('comicpess-url-backend-url', __('ComicPress Remote URL Images', 'comicpress'), array('ComicPressBackendURL', 'post_meta_box'), 'post', 'normal', 'low');
}
}
add_action('admin_menu', array('ComicPressBackendURL', 'admin_menu'));

View File

@ -11,38 +11,6 @@ class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
$this->ba = new ComicPressBackendAttachment((object)array('ID' => 1)); $this->ba = new ComicPressBackendAttachment((object)array('ID' => 1));
} }
function providerTestGenerateFromPost() {
return array(
array(array(), array(), false),
array(array((object)array('ID' => 2)), array(), array()),
array(array((object)array('ID' => 2)), array('managed' => false), array()),
array(array((object)array('ID' => 2)), array('managed' => true), array('attachment-2')),
);
}
/**
* @dataProvider providerTestGenerateFromPost
*/
function testGenerateFromPost($get_children_response, $post_meta, $expected_ids) {
_set_get_children(array(
'post_parent' => 1,
'post_type' => 'attachment',
'post_mime_type' => 'image'
), $get_children_response);
update_post_meta(2, 'comicpress', $post_meta);
$results = ComicPressBackendAttachment::generate_from_post((object)array('ID' => 1));
if ($expected_ids === false) {
$this->assertTrue(empty($results));
} else {
$this->assertEquals(count($expected_ids), count($results));
foreach ($results as $result) {
$this->assertTrue(in_array($result->id, $expected_ids));
}
}
}
function providerTestDims() { function providerTestDims() {
return array( return array(
array('comic', false, array()), array('comic', false, array()),
@ -84,35 +52,6 @@ class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_result, $this->ba->url('comic')); $this->assertEquals($expected_result, $this->ba->url('comic'));
} }
function providerTestGenerateFromID() {
return array(
array(null, false, false),
array(1, false, false),
array('attachment-1', true, true),
array('attachment-1', false, false),
array('attachment-2', false, false),
array('attachment-3', false, false),
);
}
/**
* @dataProvider providerTestGenerateFromID
*/
function testGenerateFromID($id, $is_managed, $is_successful) {
wp_insert_post(array('ID' => 1));
wp_insert_post(array('ID' => 3));
update_post_meta(1, 'comicpress', array('managed' => $is_managed));
if ($is_successful) {
$return = new ComicPressBackendAttachment((object)array('ID' => 1));
} else {
$return = false;
}
$this->assertEquals($return, call_user_func(array('ComicPressBackendAttachment', 'generate_from_id'), $id));
}
function testGetInfo() { function testGetInfo() {
$ba = $this->getMock('ComicPressBackendAttachment', array('dims', 'url', 'file'), array(), 'Mock_ComicPressBackendAttachment', false); $ba = $this->getMock('ComicPressBackendAttachment', array('dims', 'url', 'file'), array(), 'Mock_ComicPressBackendAttachment', false);