finish up factory bits
This commit is contained in:
parent
603f11fc3a
commit
ed222ef320
@ -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'));
|
||||
|
67
test/backends/ComicPressBackendURLFactoryTest.php
Normal file
67
test/backends/ComicPressBackendURLFactoryTest.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
require_once('PHPUnit/Framework.php');
|
||||
require_once('MockPress/mockpress.php');
|
||||
require_once('backends/ComicPressBackendURL.inc');
|
||||
|
||||
class ComicPressBackendUrlFactoryTest extends PHPUnit_Framework_TestCase {
|
||||
function setUp() {
|
||||
_reset_wp();
|
||||
|
||||
$this->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));
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user