working on rendering

This commit is contained in:
John Bintz 2009-06-04 17:36:51 -04:00
parent cca552c32c
commit 96a63af533
2 changed files with 163 additions and 4 deletions

View File

@ -1,8 +1,66 @@
<?php
class DailyImageWidget {
function DailyImageWidget() {
$this->default_display_options = array(
'title',
'image',
'styles'
);
}
function get_display_options() {
$display_options = get_option('hubblesite-daily-image-options');
$this->display_options = array();
if (!empty($display_options)) {
$this->display_options = array_intersect(
explode(",", $display_options),
array("title", "image", "styles", "caption", "credits")
);
}
if (empty($this->display_options)) {
$this->display_options = $this->default_display_options;
}
return $this->display_options;
}
function render() {
if (!empty($this->data)) {
if (!empty($this->data) && is_array($this->data)) {
$options = $this->get_display_options();
echo '<div id="hubblesite-daily-image">';
if (in_array("image", $options)) {
echo '<a href="' . $this->data['gallery_url'] . '" title="' . $this->data['title'] . '">';
echo '<img src="' . $this->data['image_url'] . '" alt="' . $this->data['title'] . '" />';
echo '</a>';
}
if (in_array("title", $options)) {
echo '<a id="hubblesite-daily-image-title" href="' . $this->data['gallery_url'] . '">';
echo $this->data['title'];
echo '</a>';
}
if (in_array("caption", $options)) {
echo '<div id="hubblesite-daily-image-caption">';
echo $this->data['caption'];
echo '</div>';
}
if (in_array("credits", $options)) {
echo '<div id="hubblesite-daily-image-credits">';
echo $this->data['credits'];
echo '</div>';
}
echo '</div>';
if (in_array("styles", $options)) {
echo '<style type="text/css">';
echo "div#hubblesite-daily-image { text-align: center }";
echo '</style>';
}
}
}
}

View File

@ -2,14 +2,40 @@
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../classes/DailyImageWidget.php');
require_once(dirname(__FILE__) . '/../../wordpress-phpunit-mocks/wordpress-phpunit-mocks.php');
class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
function setUp() {
$this->diw = new DailyImageWidget();
$this->diw->data = array(
'title' => 'title',
'caption' => 'caption',
'date' => '12345',
'image_url' => 'image_url',
'gallery_url' => 'gallery_url',
'credits' => 'credits'
);
_reset_wp();
}
function testRetrieveDataFailure() {
$this->diw->data = false;
function providerTestRetrieveJunkData() {
return array(
array(0),
array(null),
array(false),
array(true),
array(array()),
array((object)array())
);
}
/**
* @dataProvider providerTestRetrieveJunkData
*/
function testRetrieveJunkData($bad_data) {
$this->diw->data = $bad_data;
ob_start();
$this->diw->render();
@ -17,6 +43,81 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(empty($result));
}
function providerTestTemplateOptions() {
return array(
array(
"image",
array(
'//div[@id="hubblesite-daily-image"]' => true,
'//div/a[@href="gallery_url" and @title="title"]' => true,
'//div/a/img[@src="image_url" and @alt="title"]' => true,
)
),
array(
"title",
array(
'//div/a[@href="gallery_url" and @id="hubblesite-daily-image-title"]' => "title"
)
),
array(
"styles",
array(
'//style[@type="text/css"]' => true
)
),
array(
"caption",
array(
'//div/div[@id="hubblesite-daily-image-caption"]' => 'caption'
)
),
array(
"credits",
array(
'//div/div[@id="hubblesite-daily-image-credits"]' => 'credits'
)
)
);
}
/**
* @dataProvider providerTestTemplateOptions
*/
function testTemplateOptions($option_string, $xpath_tests) {
update_option('hubblesite-daily-image-options', $option_string);
ob_start();
$this->diw->render();
$result = ob_get_clean();
$this->assertTrue(!empty($result));
$this->assertTrue(($xml = _to_xml($result)) !== false);
foreach ($xpath_tests as $xpath => $result) {
$this->assertTrue(_xpath_test($xml, $xpath, $result), $xpath);
}
}
function providerTestGetDisplayOptions() {
return array(
array("", array("title", "image", "styles")),
array("meow", array("title", "image", "styles")),
array("title", array("title")),
array("title,image", array("title", "image")),
array("title,meow", array("title"))
);
}
/**
* @dataProvider providerTestGetDisplayOptions
*/
function testGetDisplayOptions($options, $result) {
update_option('hubblesite-daily-image-options', $options);
$this->assertEquals($result, $this->diw->get_display_options());
}
}
?>