diff --git a/classes/backends/ComicPressBackendURL.inc b/classes/backends/ComicPressBackendURL.inc index cd4e939..cfd321f 100644 --- a/classes/backends/ComicPressBackendURL.inc +++ b/classes/backends/ComicPressBackendURL.inc @@ -35,6 +35,46 @@ class ComicPressBackendURL extends ComicPressBackend { } } + function generate_id($post_id, $key) { + if (is_numeric($post_id)) { + if (is_scalar($key)) { + return "url-${post_id}-${key}"; + } + } + + 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'); + } +} + +class ComicPressBackendURLFactory { + function generate_from_id($id) { + if (preg_match('#^url-([0-9]+)-(.+)$#', $id, $matches) > 0) { + list($all, $post_id, $key) = $matches; + $post = get_post($post_id); + if (!empty($post)) { + $meta = get_post_meta($post_id, 'backend_url_image_urls', true); + if (is_array($meta)) { + if (isset($meta[$key])) { + $backend = new ComicPressBackendURL(); + $backend->id = $id; + $backend->urls_by_type = $meta[$key]; + return $backend; + } + } + } + } + return false; + } + + function generate_from_post($post) { $backends = array(); @@ -57,43 +97,6 @@ class ComicPressBackendURL extends ComicPressBackend { } return $backends; } - - function generate_id($post_id, $key) { - if (is_numeric($post_id)) { - if (is_scalar($key)) { - return "url-${post_id}-${key}"; - } - } - - return false; - } - - function generate_from_id($id) { - if (preg_match('#^url-([0-9]+)-(.+)$#', $id, $matches) > 0) { - list($all, $post_id, $key) = $matches; - $post = get_post($post_id); - if (!empty($post)) { - $meta = get_post_meta($post_id, 'backend_url_image_urls', true); - if (is_array($meta)) { - if (isset($meta[$key])) { - $backend = new ComicPressBackendURL(); - $backend->id = $id; - $backend->urls_by_type = $meta[$key]; - return $backend; - } - } - } - } - 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/ComicPressBackendURLFactoryTest.php b/test/backends/ComicPressBackendURLFactoryTest.php new file mode 100644 index 0000000..2639546 --- /dev/null +++ b/test/backends/ComicPressBackendURLFactoryTest.php @@ -0,0 +1,67 @@ +fa = new ComicPressBackendURLFactory(); + } + + function providerTestGenerateFromPost() { + $valid_backend = new ComicPressBackendURL(); + $valid_backend->id = 'url-1-12345'; + $valid_backend->urls_by_type = array('comic' => 'test'); + + return array( + array(false, array()), + array(array(), array()), + array(array('12345' => array('comic' => 'test')), array($valid_backend)), + ); + } + + /** + * @dataProvider providerTestGenerateFromPost + */ + function testGenerateFromPost($metadata, $expected_backends) { + update_post_meta(1, 'backend_url_image_urls', $metadata); + + wp_insert_post((object)array('ID' => 1)); + + foreach (array(1, (object)array('ID' => 1)) as $source) { + $this->assertEquals($expected_backends, $this->fa->generate_from_post($source)); + } + } + + function providerTestGenerateFromID() { + $valid_backend = new ComicPressBackendURL(); + $valid_backend->id = 'url-1-12345'; + $valid_backend->urls_by_type = array( + 'comic' => 'comic', + 'rss' => 'rss' + ); + + return array( + array('', false), + array('url-', false), + array('url-1', false), + array('url-2-12345', false), + array('url-1-123456', false), + array('url-1-12345', $valid_backend) + ); + } + + /** + * @dataProvider providerTestGenerateFromID + */ + function testGenerateFromID($id, $expected_result) { + wp_insert_post((object)array('ID' => 1)); + + update_post_meta(1, 'backend_url_image_urls', array('12345' => array('comic' => 'comic', 'rss' => 'rss'))); + + $this->assertEquals($expected_result, $this->fa->generate_from_id($id)); + } +} diff --git a/test/backends/ComicPressBackendURLTest.php b/test/backends/ComicPressBackendURLTest.php index c8b41f1..5b1222b 100644 --- a/test/backends/ComicPressBackendURLTest.php +++ b/test/backends/ComicPressBackendURLTest.php @@ -50,31 +50,6 @@ class ComicPressBackendUrlTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_urls, get_post_meta(1, 'backend_url_image_urls', true)); } - function providerTestGenerateFromPost() { - $valid_backend = new ComicPressBackendURL(); - $valid_backend->id = 'url-1-12345'; - $valid_backend->urls_by_type = array('comic' => 'test'); - - return array( - array(false, array()), - array(array(), array()), - array(array('12345' => array('comic' => 'test')), array($valid_backend)), - ); - } - - /** - * @dataProvider providerTestGenerateFromPost - */ - function testGenerateFromPost($metadata, $expected_backends) { - update_post_meta(1, 'backend_url_image_urls', $metadata); - - wp_insert_post((object)array('ID' => 1)); - - foreach (array(1, (object)array('ID' => 1)) as $source) { - $this->assertEquals($expected_backends, ComicPressBackendURL::generate_from_post($source)); - } - } - function providerTestGenerateID() { return array( array(null, null, false), @@ -91,33 +66,4 @@ class ComicPressBackendUrlTest extends PHPUnit_Framework_TestCase { function testGenerateID($post_id, $key, $expected_result) { $this->assertEquals($expected_result, ComicPressBackendURL::generate_id($post_id, $key)); } - - function providerTestGenerateFromID() { - $valid_backend = new ComicPressBackendURL(); - $valid_backend->id = 'url-1-12345'; - $valid_backend->urls_by_type = array( - 'comic' => 'comic', - 'rss' => 'rss' - ); - - return array( - array('', false), - array('url-', false), - array('url-1', false), - array('url-2-12345', false), - array('url-1-123456', false), - array('url-1-12345', $valid_backend) - ); - } - - /** - * @dataProvider providerTestGenerateFromID - */ - function testGenerateFromID($id, $expected_result) { - wp_insert_post((object)array('ID' => 1)); - - update_post_meta(1, 'backend_url_image_urls', array('12345' => array('comic' => 'comic', 'rss' => 'rss'))); - - $this->assertEquals($expected_result, ComicPressBackendURL::generate_from_id($id)); - } }