comicpress-core/test/backends/ComicPressBackendAttachmentTest.php

131 lines
3.9 KiB
PHP
Raw Normal View History

2009-11-09 03:11:26 +00:00
<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once('backends/ComicPressBackendAttachment.inc');
class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
2009-11-11 04:11:48 +00:00
$this->ba = new ComicPressBackendAttachment((object)array('ID' => 1));
2009-11-09 03:11:26 +00:00
}
function providerTestGenerateFromPost() {
return array(
2009-11-11 03:09:53 +00:00
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')),
2009-11-09 03:11:26 +00:00
);
}
/**
* @dataProvider providerTestGenerateFromPost
*/
2009-11-11 03:09:53 +00:00
function testGenerateFromPost($get_children_response, $post_meta, $expected_ids) {
2009-11-09 03:11:26 +00:00
_set_get_children(array(
'post_parent' => 1,
'post_type' => 'attachment',
'post_mime_type' => 'image'
), $get_children_response);
update_post_meta(2, 'comicpress', $post_meta);
2009-11-11 03:09:53 +00:00
2009-11-09 03:11:26 +00:00
$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));
}
}
}
2009-11-11 04:11:48 +00:00
function providerTestDims() {
return array(
2009-11-19 12:07:04 +00:00
array('comic', false, array()),
array('comic', true, array()),
array('comic', array(), array()),
array('comic', array('dimensions' => '300x200'), array('width' => 300, 'height' => 200)),
array('default', array('dimensions' => '300x200', 'default' => true), array('width' => 300, 'height' => 200))
);
2009-11-11 04:11:48 +00:00
}
/**
* @dataProvider providerTestDims
*/
2009-11-19 12:07:04 +00:00
function testDims($which, $image_options, $expected_result) {
2009-11-16 17:35:48 +00:00
$comicpress = ComicPress::get_instance();
$comicpress->comicpress_options = array(
'image_types' => array(
'comic' => $image_options
)
);
2009-11-19 12:07:04 +00:00
$this->assertEquals($expected_result, $this->ba->dims($which));
2009-11-11 04:11:48 +00:00
}
function providerTestUrl() {
return array(
array(false, false),
array(true, false),
array(array(), false),
array(array('url', 300, 200, false), 'url')
);
}
/**
* @dataProvider providerTestUrl
*/
function testUrl($image_downsize_result, $expected_result) {
_set_image_downsize_result(1, 'comic', $image_downsize_result);
$this->assertEquals($expected_result, $this->ba->url('comic'));
}
2009-11-12 03:12:27 +00:00
function providerTestGenerateFromID() {
return array(
2009-11-19 12:07:04 +00:00
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),
2009-11-16 17:35:48 +00:00
);
2009-11-12 03:12:27 +00:00
}
/**
* @dataProvider providerTestGenerateFromID
*/
2009-11-19 12:07:04 +00:00
function testGenerateFromID($id, $is_managed, $is_successful) {
2009-11-12 03:12:27 +00:00
wp_insert_post(array('ID' => 1));
2009-11-16 17:35:48 +00:00
wp_insert_post(array('ID' => 3));
2009-11-19 12:07:04 +00:00
update_post_meta(1, 'comicpress', array('managed' => $is_managed));
2009-11-12 03:12:27 +00:00
2009-11-12 03:31:36 +00:00
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));
2009-11-12 03:31:36 +00:00
}
function testGetInfo() {
$ba = $this->getMock('ComicPressBackendAttachment', array('dims', 'url', 'file'), array(), 'Mock_ComicPressBackendAttachment', false);
$ba->expects($this->once())->method('dims')->will($this->returnValue(array('width' => 320, 'height' => 240)));
$ba->expects($this->once())->method('url')->will($this->returnValue('http://blah/file.jpg'));
$ba->expects($this->once())->method('file')->will($this->returnValue('/root/file.jpg'));
$this->assertEquals(array(
'width' => 320,
'height' => 240,
'url' => 'http://blah/file.jpg',
'file' => '/root/file.jpg'
), $ba->get_info());
2009-11-12 03:12:27 +00:00
}
2009-11-16 17:35:48 +00:00
}