From 57c83c74fbdd3c279a6beed44d2b2574c1caa7a0 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 6 Dec 2009 16:09:32 -0500 Subject: [PATCH] start move to backend factories --- classes/ComicPress.inc | 4 +- .../backends/ComicPressBackendAttachment.inc | 63 +++++++++---------- classes/backends/ComicPressBackendURL.inc | 10 +++ .../ComicPressBackendAttachmentTest.php | 61 ------------------ 4 files changed, 43 insertions(+), 95 deletions(-) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index d5cda14..30bddf6 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -154,8 +154,8 @@ class ComicPress { $this->normalize_image_size_options(); foreach (get_declared_classes() as $class) { - if (preg_match('#^ComicPressBackend.+$#', $class) > 0) { - $this->backends[] = $class; + if (preg_match('#^ComicPressBackend.+Factory$#', $class) > 0) { + $this->backends[] = new $class(); } } } diff --git a/classes/backends/ComicPressBackendAttachment.inc b/classes/backends/ComicPressBackendAttachment.inc index cd7a553..88c246e 100644 --- a/classes/backends/ComicPressBackendAttachment.inc +++ b/classes/backends/ComicPressBackendAttachment.inc @@ -6,37 +6,10 @@ class ComicPressBackendAttachment extends ComicPressBackend { var $root_id = '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) { - if (!is_null($attachment)) { - $this->attachment = $attachment; - $this->id = sprintf('%s-%d', $this->root_id, $this->attachment->ID); - $this->type = get_post_meta($this->attachment->ID, 'comic_image_type', true); - } + $this->attachment = $attachment; + $this->id = sprintf('%s-%d', $this->root_id, $this->attachment->ID); + $this->type = get_post_meta($this->attachment->ID, 'comic_image_type', true); } function dims($size = 'comic') { @@ -89,6 +62,34 @@ class ComicPressBackendAttachment extends ComicPressBackend { } 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) { if (strpos($id, 'attachment-') === 0) { @@ -108,5 +109,3 @@ class ComicPressBackendAttachment extends ComicPressBackend { return false; } } - -?> diff --git a/classes/backends/ComicPressBackendURL.inc b/classes/backends/ComicPressBackendURL.inc index 97e05cd..cd4e939 100644 --- a/classes/backends/ComicPressBackendURL.inc +++ b/classes/backends/ComicPressBackendURL.inc @@ -86,4 +86,14 @@ class ComicPressBackendURL extends ComicPressBackend { } 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')); diff --git a/test/backends/ComicPressBackendAttachmentTest.php b/test/backends/ComicPressBackendAttachmentTest.php index aa11485..309da37 100644 --- a/test/backends/ComicPressBackendAttachmentTest.php +++ b/test/backends/ComicPressBackendAttachmentTest.php @@ -11,38 +11,6 @@ class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase { $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() { return array( array('comic', false, array()), @@ -84,35 +52,6 @@ class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase { $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() { $ba = $this->getMock('ComicPressBackendAttachment', array('dims', 'url', 'file'), array(), 'Mock_ComicPressBackendAttachment', false);