phpunit cleanups

This commit is contained in:
John Bintz 2009-12-21 16:36:57 -05:00
parent 83c30c1e53
commit ad64a7b349
4 changed files with 103 additions and 93 deletions

5
.gitignore vendored
View File

@ -1,3 +1,6 @@
.DS_Store
*~
.settings
.project
.buildpath
coverage

View File

@ -4,6 +4,8 @@
* Show a HubbleSite daily image as a widget.
*/
class DailyImageWidget {
var $display_options = array();
/**
* Initialize the widget.
* For unit testing purposes, you can disable remote data loading by passing true to this function.
@ -14,24 +16,24 @@ class DailyImageWidget {
'title',
'image'
);
$this->_cache_time = 86400;
$this->_source_stamp = "&f=wpw";
$this->data_source = "http://hubblesite.org/gallery/album/daily_image.php";
$this->has_simplexml = class_exists('SimpleXMLElement');
$this->_valid_column_names = array('title', 'date', 'image_url', 'gallery_url', 'credits');
$this->_valid_options = array(
"image" => __("Daily Image", "hubblesite-daily-image-widget"),
"title" => __("Image Title", "hubblesite-daily-image-widget"),
"credits" => __("Credits", "hubblesite-daily-image-widget")
);
add_action('init', array($this, "_init"));
}
/**
* WordPress init hook.
*/
@ -44,7 +46,7 @@ class DailyImageWidget {
'description' => __('Embed a daily HubbleSite Gallery image on your WordPress blog.', 'hubblesite-daily-image-widget')
)
);
register_widget_control(__("HubbleSite Daily Image", "hubblesite-daily-image-widget"), array(&$this, "render_ui"));
register_widget_control(__("HubbleSite Daily Image", "hubblesite-daily-image-widget"), array(&$this, "render_ui"));
if (!$skip_load_data) {
if (!$this->_load_data()) {
@ -53,11 +55,11 @@ class DailyImageWidget {
} else {
$this->data = false;
}
$this->handle_post();
$this->get_display_options();
}
/**
* Display a warning if the connection failed.
*/
@ -67,7 +69,7 @@ class DailyImageWidget {
_e("The widget will appear as empty in your site until data can be downloaded again.", "hubblesite-daily-image-widget");
echo "</div>";
}
/**
* Wrapper around a remote data call for unit testing purposes.
* @return string The data from the remote source.
@ -76,8 +78,8 @@ class DailyImageWidget {
$response = wp_remote_request($this->data_source, array('method' => 'GET'));
if (!is_wp_error($response)) {
if (isset($response['body'])) {
return $response['body'];
}
return $response['body'];
}
}
return false;
}
@ -129,7 +131,7 @@ class DailyImageWidget {
if (!empty($display_options)) {
$this->display_options = array_intersect(explode(",", $display_options), array_keys($this->_valid_options));
}
if (empty($this->display_options)) {
$this->display_options = $this->default_display_options;
}
@ -138,16 +140,16 @@ class DailyImageWidget {
return $this->display_options;
}
/**
* Render the widget.
* @param array $args The theme's widget layout arguments.
*/
function render($args) {
function render($args = array()) {
if (!empty($this->data) && is_array($this->data)) {
extract($args);
$options = $this->get_display_options();
echo $before_widget;
echo $before_title;
echo "HubbleSite Daily Image";
@ -157,7 +159,7 @@ class DailyImageWidget {
echo '<img src="' . $this->data['image_url'] . '" alt="' . $this->data['title'] . '" width="100%" />';
echo '</a>';
}
if (in_array("title", $options)) {
echo '<a id="hubblesite-daily-image-title" href="' . $this->data['gallery_url'] . $this->_source_stamp . '">';
echo $this->_fix_widows($this->data['title']);
@ -169,10 +171,10 @@ class DailyImageWidget {
echo $this->_fix_widows($this->data['credits']);
echo '</div>';
}
echo $after_widget;
echo $after_widget;
}
}
/**
* Render the widget admin UI.
*/
@ -181,7 +183,7 @@ class DailyImageWidget {
echo "<p>";
_e("Show on Widget <em>(must select at least one)</em>:", "hubblesite-daily-image-widget");
echo "</p>";
foreach ($this->_valid_options as $option => $label) {
echo "<label>";
echo "<input type=\"checkbox\" name=\"hubblesite[${option}]\" " . (in_array($option, $this->display_options) ? "checked=\"checked\"" : "") . "/> ";
@ -190,7 +192,7 @@ class DailyImageWidget {
echo "<br />";
}
}
/**
* Parse a string of XML from the HubbleSite Daily Gallery Image feed.
* This will try to use SimpleXML if vailable. If not, will fall back on Expat.
@ -211,19 +213,19 @@ class DailyImageWidget {
$this->data = false;
if (xml_parse($parser, $xml_text)) {
if (count($this->_xml_data) == count($this->_valid_column_names)) {
$this->data = $this->_xml_data;
$this->data = $this->_xml_data;
}
}
return $this->data;
}
/**
* Expat start element handler.
*/
function _start_element_handler($parser, $name, $attributes) {
$this->_character_data = "";
$this->_character_data = "";
}
/**
* Expat end element handler.
*/
@ -232,31 +234,31 @@ class DailyImageWidget {
if (in_array($name, $this->_valid_column_names)) {
$this->_xml_data[$name] = $this->_character_data;
}
$this->_character_data = "";
}
/**
* Expat character data handler.
*/
function _character_data_handler($parser, $data) {
$this->_character_data .= $data;
}
/**
* Retrieve the cached data from WP Options.
* @return array|boolean The cached data or false upon failure.
*/
function _get_cached_data() {
if (($result = get_option('hubblesite-daily-image-cache')) !== false) {
if (($result = get_option('hubblesite-daily-image-cache')) !== false) {
list($timestamp, $cached_data) = $result;
if (($timestamp + $this->_cache_time) > time()) {
$is_valid = true;
foreach ($this->_valid_column_names as $field) {
if (!isset($cached_data[$field])) { $is_valid = false; break; }
}
if ($is_valid) {
$this->data = $cached_data;
return $cached_data;
@ -265,7 +267,7 @@ class DailyImageWidget {
}
return false;
}
/**
* Try to ensure that no words in a paragraph or link are widowed.
* @param string $text The text to process.

6
phpunit.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<php>
<ini name="error_reporting" value="30719" />
</php>
</phpunit>

View File

@ -1,18 +1,17 @@
<?php
error_reporting(E_STRICT);
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../classes/DailyImageWidget.php');
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
require_once('MockPress/mockpress.php');
class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
wp_create_nonce("hubble");
$_POST = array();
$this->diw = new DailyImageWidget(true);
$this->sample_data = array(
'title' => 'title',
'date' => '12345',
@ -20,8 +19,8 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
'gallery_url' => 'gallery_url',
'credits' => 'credits'
);
$this->diw->data = $this->sample_data;
$this->diw->data = $this->sample_data;
}
function testWidgetRegistered() {
@ -37,10 +36,10 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
array(true),
array(array()),
array((object)array())
);
);
}
/**
/**
* @dataProvider providerTestRetrieveJunkData
*/
function testRetrieveJunkData($bad_data) {
@ -49,11 +48,11 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
ob_start();
$this->diw->render();
$result = ob_get_clean();
$this->assertTrue(empty($result));
}
function providerTestTemplateOptions() {
return array(
array(
@ -67,7 +66,7 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
array(
"title",
array(
'//a[@href="gallery_url&f=wpw" and @id="hubblesite-daily-image-title"]' => "title"
'//a[@href="gallery_url&f=wpw" and @id="hubblesite-daily-image-title"]' => "title"
)
),
array(
@ -76,15 +75,15 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
'//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(array(
'before_widget' => "",
@ -93,15 +92,15 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
'after_title' => ""
));
$result = ob_get_clean();
$this->assertTrue(!empty($result));
$this->assertTrue(($xml = _to_xml($result, true)) !== false);
foreach ($xpath_tests as $xpath => $result) {
$this->assertTrue(_xpath_test($xml, $xpath, $result), $xpath);
}
}
function providerTestGetDisplayOptions() {
return array(
array("", array("title", "image")),
@ -109,38 +108,38 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
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());
}
function testGetDefaultDisplayOptions() {
_reset_wp();
$this->assertFalse(get_option('hubblesite-daily-image-options'));
$this->assertFalse(get_option('hubblesite-daily-image-options'));
$this->diw->get_display_options();
$this->assertTrue(get_option('hubblesite-daily-image-options') !== false);
}
function testCheckedOptions() {
$this->diw->display_options = array_keys($this->diw->_valid_options);
ob_start();
$this->diw->render_ui();
$result = ob_get_clean();
$this->assertTrue(($xml = _to_xml($result, true)) !== false);
foreach ($this->diw->display_options as $option) {
$this->assertTrue(_node_exists($xml, '//input[@name="hubblesite[' . $option . ']" and @checked="checked"]'));
}
}
function providerTestUpdateOptions() {
$d = new DailyImageWidget(true);
$default_display_options = $d->default_display_options;
@ -175,22 +174,22 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
),
);
}
/**
* @dataProvider providerTestUpdateOptions
*/
function testUpdateOptions($post, $result) {
function testUpdateOptions($post, $result) {
$_POST = $post;
if (isset($_POST['hubblesite']['_wpnonce'])) {
$_POST['hubblesite']['_wpnonce'] = _get_nonce('hubble');
}
$this->diw->handle_post();
$this->diw->get_display_options();
$this->assertEquals($result, $this->diw->display_options);
}
function providerTestParseBadXML() {
return array(
array(null),
@ -199,7 +198,7 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
array("<xml></yml>")
);
}
/**
* @dataProvider providerTestParseBadXML
*/
@ -208,13 +207,13 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
$this->diw->has_simplexml = $simplexml;
$this->assertFalse($this->diw->parse_xml($xml));
}
}
}
function testParseXML() {
foreach (array(true, false) as $simplexml) {
$this->diw->has_simplexml = $simplexml;
$result = $this->diw->parse_xml(
"<gallery>" .
"<title>title</title>" .
@ -225,7 +224,7 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
"<credits>credits</credits>" .
"</gallery>"
);
$this->assertEquals(
$this->sample_data,
$result,
@ -233,35 +232,35 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
);
}
}
function testWidgetUI() {
ob_start();
$this->diw->render_ui();
$result = ob_get_clean();
$this->assertTrue(!empty($result));
$this->assertTrue(($xml = _to_xml($result, true)) !== false);
foreach ($this->diw->_valid_options as $option => $label) {
$xpath = "//label[contains(text(), '${label}')]";
$xpath = "//label[contains(text(), '${label}')]";
$this->assertTrue(_xpath_test($xml, $xpath, true), $xpath);
}
foreach (array(
'//input[@type="hidden" and @name="hubblesite[_wpnonce]"]' => true
) as $xpath => $value) {
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
}
}
function providerTestGetCachedData() {
return array(
array(time() + 86500, true),
array(time() - 86500, false),
array(null, false)
);
);
}
/**
* @dataProvider providerTestGetCachedData
*/
@ -269,27 +268,27 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
if (!is_null($test_time)) {
update_option('hubblesite-daily-image-cache', array($test_time, $this->sample_data));
} else {
update_option('hubblesite-daily-image-cache', null);
update_option('hubblesite-daily-image-cache', null);
}
$this->assertEquals($has_sample_data ? $this->sample_data : false, $this->diw->_get_cached_data());
}
function providerTestLoadData() {
return array(
array(true, null, null, true),
array(false, false, null, false),
array(false, true, false, false),
array(false, true, true, true)
);
);
}
/**
* @dataProvider providerTestLoadData
*/
function testLoadData($get_cached_data, $get_from_data_source, $parse_xml_result, $expected_return) {
function testLoadData($get_cached_data, $get_from_data_source, $parse_xml_result, $expected_return) {
$diw = $this->getMock('DailyImageWidget', array('_get_from_data_source', '_get_cached_data', 'parse_xml'));
$diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue($get_cached_data));
if ($get_cached_data == false) {
$diw->expects($this->once())->method('_get_from_data_source')->will($this->returnValue($get_from_data_source));
@ -297,13 +296,13 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
$diw->expects($this->once())->method('parse_xml')->will($this->returnValue($parse_xml_result));
}
}
$this->assertEquals($expected_return, $diw->_load_data());
$this->assertEquals($parse_xml_result, is_array(get_option('hubblesite-daily-image-cache')));
}
function providerTestWidowProtection() {
return array(
array("this is fixed", "this is&nbsp;fixed"),
@ -311,9 +310,9 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
array("<a>this is fixed</a>", "<a>this is&nbsp;fixed</a>"),
array("<a href='meow'>word</a>", "<a href='meow'>word</a>"),
array("<p>this is fixed</p><p>Also fixed</p>", '<p>this is&nbsp;fixed</p><p>Also&nbsp;fixed</p>')
);
);
}
/**
* @dataProvider providerTestWidowProtection
*/
@ -322,4 +321,4 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
}
}
?>
?>