media stuff working great, code coverage

This commit is contained in:
John Bintz 2010-02-10 23:59:39 -05:00
parent 2e540fa372
commit 3511ae9b42
4 changed files with 130 additions and 24 deletions

View File

@ -36,11 +36,9 @@ class ComicPressBackend {
}
function ensure_type($type) {
if (is_null($type)) {
if (is_null($type) || ($type == 'default')) {
$comicpress = ComicPress::get_instance();
if (is_null($type)) {
$type = $comicpress->get_default_image_type();
}
$type = $comicpress->get_default_image_type();
}
return $type;
}

View File

@ -53,17 +53,9 @@ class ComicPressTagBuilderFactory {
global $post;
$tag = $this->_new_comicpresstagbuilder($post, $this->storyline, $this->dbi);
$media = $tag->media();
if (is_null($index)) {
return $media;
} else {
$index = (int)$index;
if (isset($media[$index])) {
return $media[$index];
} else {
return false;
}
}
$tag->post = $post;
return $tag->media();
}
/**
@ -221,7 +213,7 @@ class ComicPressTagBuilder {
case 'media':
if (isset($this->post)) {
$comic_post = $this->_new_comicpresscomicpost($this->post);
return $comic_post->get_attachments_with_children(true);
return new ComicPressMediaWrapper($comic_post->get_attachments_with_children(true));
}
case 'category':
if (isset($arguments[0])) {
@ -356,3 +348,47 @@ class ComicPressTagBuilder {
return $methods;
}
}
class ComicPressMediaWrapper {
public $media;
public function __construct($media) {
$this->media = $media;
}
public function __call($method, $arguments) {
$type = (isset($arguments[0])) ? $arguments[0] : 'default';
$which = 'all';
$separator = '';
if (count($arguments) == 2) {
if (is_numeric($arguments[1])) {
$which = (int)$arguments[1];
} else {
$separator = $arguments[1];
}
}
if (is_numeric($which)) {
if (isset($this->media[$which][$type])) {
if ($media = ComicPressBackend::generate_from_id($this->media[$which][$type])) {
return $media->{$method}($type);
}
}
return false;
} else {
$output = array();
foreach ($this->media as $ids) {
if ($media = ComicPressBackend::generate_from_id($ids[$type])) {
$output[] = $media->{$method}($type);
}
}
if (is_string($separator)) {
$output = implode($separator, $output);
}
return $output;
}
return false;
}
}

View File

@ -72,7 +72,8 @@ class ComicPressBackendTest extends PHPUnit_Framework_TestCase {
return array(
array(null, 'comic'),
array('comic', 'comic'),
array('rss', 'rss')
array('rss', 'rss'),
array('default', 'comic'),
);
}
@ -114,4 +115,4 @@ class ComicPressBackendTest extends PHPUnit_Framework_TestCase {
'file' => '/root/file.jpg'
), $ba->get_info());
}
}
}

View File

@ -342,7 +342,7 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
->with('this-post')
->will($this->returnValue($comicpresscomicpost));
$this->assertEquals(array('post-media'), $core->media());
$this->assertEquals(new ComicPressMediaWrapper(array('post-media')), $core->media());
}
function testMediaForCurrentPost() {
@ -362,19 +362,16 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
$tag_builder = $this->getMock('ComicPressTagBuilder', array('media'), array($post, 'storyline', 'dbi'));
$tag_builder->expects($this->any())
$tag_builder->expects($this->once())
->method('media')
->will($this->returnValue(array('post-media')));
$core->expects($this->any())
$core->expects($this->once())
->method('_new_comicpresstagbuilder')
->with($post, 'storyline', 'dbi')
->will($this->returnValue($tag_builder));
$this->assertEquals(array('post-media'), $core->media());
$this->assertEquals('post-media', $core->media(0));
$this->assertEquals(false, $core->media(1));
}
function testComicPressComicPost() {
@ -604,4 +601,78 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(!isset($core->_post));
$this->assertTrue(!isset($core->_wp_query));
}
function providerTestComicPressMediaWrapper() {
return array(
array(
array(), 'default-id-1default-id-2'
),
array(
array('comic'), 'comic-id-1comic-id-2'
),
array(
array('default', '<br />'), 'default-id-1<br />default-id-2'
),
array(
array('default', 0), 'default-id-1'
),
array(
array('default', 2), false
),
array(
array('archive', 0), false
),
array(
array('comic', 0), false, true
),
);
}
/**
* @dataProvider providerTestComicPressMediaWrapper
*/
function testComicPressMediaWrapper($arguments, $expected_return, $is_total_fail = false) {
$backend = $this->getMock('ComicPressMockBackendFactory', array('embed', 'generate_from_id'));
$backend->expects($this->any())
->method('generate_from_id')
->will($this->returnCallback(function($id) use ($backend) {
if (in_array($id, array('comic-id-1', 'default-id-1', 'comic-id-2', 'default-id-2'))) {
$backend->_id = $id;
return $backend;
} else {
return false;
}
}));
$backend->expects($this->any())
->method('embed')
->will($this->returnCallback(function($which) use ($backend) {
switch ($which) {
case 'comic':
case 'default':
return $backend->_id;
break;
default:
return false;
}
}));
$comicpress = ComicPress::get_instance(true);
$comicpress->backends = array($backend);
$media = new ComicPressMediaWrapper(array(
array(
'comic' => $is_total_fail ? 'total-fail' : 'comic-id-1',
'default' => 'default-id-1'
),
array(
'comic' => 'comic-id-2',
'default' => 'default-id-2'
),
));
$this->assertEquals($expected_return, call_user_func_array(array($media, 'embed'), $arguments));
$comicpress = ComicPress::get_instance(true);
}
}