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 .DS_Store
*~ *~
.settings
.project
.buildpath
coverage

View File

@ -4,6 +4,8 @@
* Show a HubbleSite daily image as a widget. * Show a HubbleSite daily image as a widget.
*/ */
class DailyImageWidget { class DailyImageWidget {
var $display_options = array();
/** /**
* Initialize the widget. * Initialize the widget.
* For unit testing purposes, you can disable remote data loading by passing true to this function. * For unit testing purposes, you can disable remote data loading by passing true to this function.
@ -14,24 +16,24 @@ class DailyImageWidget {
'title', 'title',
'image' 'image'
); );
$this->_cache_time = 86400; $this->_cache_time = 86400;
$this->_source_stamp = "&f=wpw"; $this->_source_stamp = "&f=wpw";
$this->data_source = "http://hubblesite.org/gallery/album/daily_image.php"; $this->data_source = "http://hubblesite.org/gallery/album/daily_image.php";
$this->has_simplexml = class_exists('SimpleXMLElement'); $this->has_simplexml = class_exists('SimpleXMLElement');
$this->_valid_column_names = array('title', 'date', 'image_url', 'gallery_url', 'credits'); $this->_valid_column_names = array('title', 'date', 'image_url', 'gallery_url', 'credits');
$this->_valid_options = array( $this->_valid_options = array(
"image" => __("Daily Image", "hubblesite-daily-image-widget"), "image" => __("Daily Image", "hubblesite-daily-image-widget"),
"title" => __("Image Title", "hubblesite-daily-image-widget"), "title" => __("Image Title", "hubblesite-daily-image-widget"),
"credits" => __("Credits", "hubblesite-daily-image-widget") "credits" => __("Credits", "hubblesite-daily-image-widget")
); );
add_action('init', array($this, "_init")); add_action('init', array($this, "_init"));
} }
/** /**
* WordPress init hook. * WordPress init hook.
*/ */
@ -44,7 +46,7 @@ class DailyImageWidget {
'description' => __('Embed a daily HubbleSite Gallery image on your WordPress blog.', 'hubblesite-daily-image-widget') '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 (!$skip_load_data) {
if (!$this->_load_data()) { if (!$this->_load_data()) {
@ -53,11 +55,11 @@ class DailyImageWidget {
} else { } else {
$this->data = false; $this->data = false;
} }
$this->handle_post(); $this->handle_post();
$this->get_display_options(); $this->get_display_options();
} }
/** /**
* Display a warning if the connection failed. * 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"); _e("The widget will appear as empty in your site until data can be downloaded again.", "hubblesite-daily-image-widget");
echo "</div>"; echo "</div>";
} }
/** /**
* Wrapper around a remote data call for unit testing purposes. * Wrapper around a remote data call for unit testing purposes.
* @return string The data from the remote source. * @return string The data from the remote source.
@ -76,8 +78,8 @@ class DailyImageWidget {
$response = wp_remote_request($this->data_source, array('method' => 'GET')); $response = wp_remote_request($this->data_source, array('method' => 'GET'));
if (!is_wp_error($response)) { if (!is_wp_error($response)) {
if (isset($response['body'])) { if (isset($response['body'])) {
return $response['body']; return $response['body'];
} }
} }
return false; return false;
} }
@ -129,7 +131,7 @@ class DailyImageWidget {
if (!empty($display_options)) { if (!empty($display_options)) {
$this->display_options = array_intersect(explode(",", $display_options), array_keys($this->_valid_options)); $this->display_options = array_intersect(explode(",", $display_options), array_keys($this->_valid_options));
} }
if (empty($this->display_options)) { if (empty($this->display_options)) {
$this->display_options = $this->default_display_options; $this->display_options = $this->default_display_options;
} }
@ -138,16 +140,16 @@ class DailyImageWidget {
return $this->display_options; return $this->display_options;
} }
/** /**
* Render the widget. * Render the widget.
* @param array $args The theme's widget layout arguments. * @param array $args The theme's widget layout arguments.
*/ */
function render($args) { function render($args = array()) {
if (!empty($this->data) && is_array($this->data)) { if (!empty($this->data) && is_array($this->data)) {
extract($args); extract($args);
$options = $this->get_display_options(); $options = $this->get_display_options();
echo $before_widget; echo $before_widget;
echo $before_title; echo $before_title;
echo "HubbleSite Daily Image"; echo "HubbleSite Daily Image";
@ -157,7 +159,7 @@ class DailyImageWidget {
echo '<img src="' . $this->data['image_url'] . '" alt="' . $this->data['title'] . '" width="100%" />'; echo '<img src="' . $this->data['image_url'] . '" alt="' . $this->data['title'] . '" width="100%" />';
echo '</a>'; echo '</a>';
} }
if (in_array("title", $options)) { if (in_array("title", $options)) {
echo '<a id="hubblesite-daily-image-title" href="' . $this->data['gallery_url'] . $this->_source_stamp . '">'; echo '<a id="hubblesite-daily-image-title" href="' . $this->data['gallery_url'] . $this->_source_stamp . '">';
echo $this->_fix_widows($this->data['title']); echo $this->_fix_widows($this->data['title']);
@ -169,10 +171,10 @@ class DailyImageWidget {
echo $this->_fix_widows($this->data['credits']); echo $this->_fix_widows($this->data['credits']);
echo '</div>'; echo '</div>';
} }
echo $after_widget; echo $after_widget;
} }
} }
/** /**
* Render the widget admin UI. * Render the widget admin UI.
*/ */
@ -181,7 +183,7 @@ class DailyImageWidget {
echo "<p>"; echo "<p>";
_e("Show on Widget <em>(must select at least one)</em>:", "hubblesite-daily-image-widget"); _e("Show on Widget <em>(must select at least one)</em>:", "hubblesite-daily-image-widget");
echo "</p>"; echo "</p>";
foreach ($this->_valid_options as $option => $label) { foreach ($this->_valid_options as $option => $label) {
echo "<label>"; echo "<label>";
echo "<input type=\"checkbox\" name=\"hubblesite[${option}]\" " . (in_array($option, $this->display_options) ? "checked=\"checked\"" : "") . "/> "; echo "<input type=\"checkbox\" name=\"hubblesite[${option}]\" " . (in_array($option, $this->display_options) ? "checked=\"checked\"" : "") . "/> ";
@ -190,7 +192,7 @@ class DailyImageWidget {
echo "<br />"; echo "<br />";
} }
} }
/** /**
* Parse a string of XML from the HubbleSite Daily Gallery Image feed. * 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. * This will try to use SimpleXML if vailable. If not, will fall back on Expat.
@ -211,19 +213,19 @@ class DailyImageWidget {
$this->data = false; $this->data = false;
if (xml_parse($parser, $xml_text)) { if (xml_parse($parser, $xml_text)) {
if (count($this->_xml_data) == count($this->_valid_column_names)) { if (count($this->_xml_data) == count($this->_valid_column_names)) {
$this->data = $this->_xml_data; $this->data = $this->_xml_data;
} }
} }
return $this->data; return $this->data;
} }
/** /**
* Expat start element handler. * Expat start element handler.
*/ */
function _start_element_handler($parser, $name, $attributes) { function _start_element_handler($parser, $name, $attributes) {
$this->_character_data = ""; $this->_character_data = "";
} }
/** /**
* Expat end element handler. * Expat end element handler.
*/ */
@ -232,31 +234,31 @@ class DailyImageWidget {
if (in_array($name, $this->_valid_column_names)) { if (in_array($name, $this->_valid_column_names)) {
$this->_xml_data[$name] = $this->_character_data; $this->_xml_data[$name] = $this->_character_data;
} }
$this->_character_data = ""; $this->_character_data = "";
} }
/** /**
* Expat character data handler. * Expat character data handler.
*/ */
function _character_data_handler($parser, $data) { function _character_data_handler($parser, $data) {
$this->_character_data .= $data; $this->_character_data .= $data;
} }
/** /**
* Retrieve the cached data from WP Options. * Retrieve the cached data from WP Options.
* @return array|boolean The cached data or false upon failure. * @return array|boolean The cached data or false upon failure.
*/ */
function _get_cached_data() { 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; list($timestamp, $cached_data) = $result;
if (($timestamp + $this->_cache_time) > time()) { if (($timestamp + $this->_cache_time) > time()) {
$is_valid = true; $is_valid = true;
foreach ($this->_valid_column_names as $field) { foreach ($this->_valid_column_names as $field) {
if (!isset($cached_data[$field])) { $is_valid = false; break; } if (!isset($cached_data[$field])) { $is_valid = false; break; }
} }
if ($is_valid) { if ($is_valid) {
$this->data = $cached_data; $this->data = $cached_data;
return $cached_data; return $cached_data;
@ -265,7 +267,7 @@ class DailyImageWidget {
} }
return false; return false;
} }
/** /**
* Try to ensure that no words in a paragraph or link are widowed. * Try to ensure that no words in a paragraph or link are widowed.
* @param string $text The text to process. * @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 <?php
error_reporting(E_STRICT);
require_once('PHPUnit/Framework.php'); require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../classes/DailyImageWidget.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 { class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
function setUp() { function setUp() {
_reset_wp(); _reset_wp();
wp_create_nonce("hubble"); wp_create_nonce("hubble");
$_POST = array(); $_POST = array();
$this->diw = new DailyImageWidget(true); $this->diw = new DailyImageWidget(true);
$this->sample_data = array( $this->sample_data = array(
'title' => 'title', 'title' => 'title',
'date' => '12345', 'date' => '12345',
@ -20,8 +19,8 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
'gallery_url' => 'gallery_url', 'gallery_url' => 'gallery_url',
'credits' => 'credits' 'credits' => 'credits'
); );
$this->diw->data = $this->sample_data; $this->diw->data = $this->sample_data;
} }
function testWidgetRegistered() { function testWidgetRegistered() {
@ -37,10 +36,10 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
array(true), array(true),
array(array()), array(array()),
array((object)array()) array((object)array())
); );
} }
/** /**
* @dataProvider providerTestRetrieveJunkData * @dataProvider providerTestRetrieveJunkData
*/ */
function testRetrieveJunkData($bad_data) { function testRetrieveJunkData($bad_data) {
@ -49,11 +48,11 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
ob_start(); ob_start();
$this->diw->render(); $this->diw->render();
$result = ob_get_clean(); $result = ob_get_clean();
$this->assertTrue(empty($result)); $this->assertTrue(empty($result));
} }
function providerTestTemplateOptions() { function providerTestTemplateOptions() {
return array( return array(
array( array(
@ -67,7 +66,7 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
array( array(
"title", "title",
array( 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( array(
@ -76,15 +75,15 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
'//div[@id="hubblesite-daily-image-credits"]' => 'credits' '//div[@id="hubblesite-daily-image-credits"]' => 'credits'
) )
) )
); );
} }
/** /**
* @dataProvider providerTestTemplateOptions * @dataProvider providerTestTemplateOptions
*/ */
function testTemplateOptions($option_string, $xpath_tests) { function testTemplateOptions($option_string, $xpath_tests) {
update_option('hubblesite-daily-image-options', $option_string); update_option('hubblesite-daily-image-options', $option_string);
ob_start(); ob_start();
$this->diw->render(array( $this->diw->render(array(
'before_widget' => "", 'before_widget' => "",
@ -93,15 +92,15 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
'after_title' => "" 'after_title' => ""
)); ));
$result = ob_get_clean(); $result = ob_get_clean();
$this->assertTrue(!empty($result)); $this->assertTrue(!empty($result));
$this->assertTrue(($xml = _to_xml($result, true)) !== false); $this->assertTrue(($xml = _to_xml($result, true)) !== false);
foreach ($xpath_tests as $xpath => $result) { foreach ($xpath_tests as $xpath => $result) {
$this->assertTrue(_xpath_test($xml, $xpath, $result), $xpath); $this->assertTrue(_xpath_test($xml, $xpath, $result), $xpath);
} }
} }
function providerTestGetDisplayOptions() { function providerTestGetDisplayOptions() {
return array( return array(
array("", array("title", "image")), array("", array("title", "image")),
@ -109,38 +108,38 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
array("title", array("title")), array("title", array("title")),
array("title,image", array("title", "image")), array("title,image", array("title", "image")),
array("title,meow", array("title")) array("title,meow", array("title"))
); );
} }
/** /**
* @dataProvider providerTestGetDisplayOptions * @dataProvider providerTestGetDisplayOptions
*/ */
function testGetDisplayOptions($options, $result) { function testGetDisplayOptions($options, $result) {
update_option('hubblesite-daily-image-options', $options); update_option('hubblesite-daily-image-options', $options);
$this->assertEquals($result, $this->diw->get_display_options()); $this->assertEquals($result, $this->diw->get_display_options());
} }
function testGetDefaultDisplayOptions() { function testGetDefaultDisplayOptions() {
_reset_wp(); _reset_wp();
$this->assertFalse(get_option('hubblesite-daily-image-options')); $this->assertFalse(get_option('hubblesite-daily-image-options'));
$this->diw->get_display_options(); $this->diw->get_display_options();
$this->assertTrue(get_option('hubblesite-daily-image-options') !== false); $this->assertTrue(get_option('hubblesite-daily-image-options') !== false);
} }
function testCheckedOptions() { function testCheckedOptions() {
$this->diw->display_options = array_keys($this->diw->_valid_options); $this->diw->display_options = array_keys($this->diw->_valid_options);
ob_start(); ob_start();
$this->diw->render_ui(); $this->diw->render_ui();
$result = ob_get_clean(); $result = ob_get_clean();
$this->assertTrue(($xml = _to_xml($result, true)) !== false); $this->assertTrue(($xml = _to_xml($result, true)) !== false);
foreach ($this->diw->display_options as $option) { foreach ($this->diw->display_options as $option) {
$this->assertTrue(_node_exists($xml, '//input[@name="hubblesite[' . $option . ']" and @checked="checked"]')); $this->assertTrue(_node_exists($xml, '//input[@name="hubblesite[' . $option . ']" and @checked="checked"]'));
} }
} }
function providerTestUpdateOptions() { function providerTestUpdateOptions() {
$d = new DailyImageWidget(true); $d = new DailyImageWidget(true);
$default_display_options = $d->default_display_options; $default_display_options = $d->default_display_options;
@ -175,22 +174,22 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
), ),
); );
} }
/** /**
* @dataProvider providerTestUpdateOptions * @dataProvider providerTestUpdateOptions
*/ */
function testUpdateOptions($post, $result) { function testUpdateOptions($post, $result) {
$_POST = $post; $_POST = $post;
if (isset($_POST['hubblesite']['_wpnonce'])) { if (isset($_POST['hubblesite']['_wpnonce'])) {
$_POST['hubblesite']['_wpnonce'] = _get_nonce('hubble'); $_POST['hubblesite']['_wpnonce'] = _get_nonce('hubble');
} }
$this->diw->handle_post(); $this->diw->handle_post();
$this->diw->get_display_options(); $this->diw->get_display_options();
$this->assertEquals($result, $this->diw->display_options); $this->assertEquals($result, $this->diw->display_options);
} }
function providerTestParseBadXML() { function providerTestParseBadXML() {
return array( return array(
array(null), array(null),
@ -199,7 +198,7 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
array("<xml></yml>") array("<xml></yml>")
); );
} }
/** /**
* @dataProvider providerTestParseBadXML * @dataProvider providerTestParseBadXML
*/ */
@ -208,13 +207,13 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
$this->diw->has_simplexml = $simplexml; $this->diw->has_simplexml = $simplexml;
$this->assertFalse($this->diw->parse_xml($xml)); $this->assertFalse($this->diw->parse_xml($xml));
} }
} }
function testParseXML() { function testParseXML() {
foreach (array(true, false) as $simplexml) { foreach (array(true, false) as $simplexml) {
$this->diw->has_simplexml = $simplexml; $this->diw->has_simplexml = $simplexml;
$result = $this->diw->parse_xml( $result = $this->diw->parse_xml(
"<gallery>" . "<gallery>" .
"<title>title</title>" . "<title>title</title>" .
@ -225,7 +224,7 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
"<credits>credits</credits>" . "<credits>credits</credits>" .
"</gallery>" "</gallery>"
); );
$this->assertEquals( $this->assertEquals(
$this->sample_data, $this->sample_data,
$result, $result,
@ -233,35 +232,35 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
); );
} }
} }
function testWidgetUI() { function testWidgetUI() {
ob_start(); ob_start();
$this->diw->render_ui(); $this->diw->render_ui();
$result = ob_get_clean(); $result = ob_get_clean();
$this->assertTrue(!empty($result)); $this->assertTrue(!empty($result));
$this->assertTrue(($xml = _to_xml($result, true)) !== false); $this->assertTrue(($xml = _to_xml($result, true)) !== false);
foreach ($this->diw->_valid_options as $option => $label) { 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); $this->assertTrue(_xpath_test($xml, $xpath, true), $xpath);
} }
foreach (array( foreach (array(
'//input[@type="hidden" and @name="hubblesite[_wpnonce]"]' => true '//input[@type="hidden" and @name="hubblesite[_wpnonce]"]' => true
) as $xpath => $value) { ) as $xpath => $value) {
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
} }
} }
function providerTestGetCachedData() { function providerTestGetCachedData() {
return array( return array(
array(time() + 86500, true), array(time() + 86500, true),
array(time() - 86500, false), array(time() - 86500, false),
array(null, false) array(null, false)
); );
} }
/** /**
* @dataProvider providerTestGetCachedData * @dataProvider providerTestGetCachedData
*/ */
@ -269,27 +268,27 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
if (!is_null($test_time)) { if (!is_null($test_time)) {
update_option('hubblesite-daily-image-cache', array($test_time, $this->sample_data)); update_option('hubblesite-daily-image-cache', array($test_time, $this->sample_data));
} else { } 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()); $this->assertEquals($has_sample_data ? $this->sample_data : false, $this->diw->_get_cached_data());
} }
function providerTestLoadData() { function providerTestLoadData() {
return array( return array(
array(true, null, null, true), array(true, null, null, true),
array(false, false, null, false), array(false, false, null, false),
array(false, true, false, false), array(false, true, false, false),
array(false, true, true, true) array(false, true, true, true)
); );
} }
/** /**
* @dataProvider providerTestLoadData * @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 = $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)); $diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue($get_cached_data));
if ($get_cached_data == false) { if ($get_cached_data == false) {
$diw->expects($this->once())->method('_get_from_data_source')->will($this->returnValue($get_from_data_source)); $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)); $diw->expects($this->once())->method('parse_xml')->will($this->returnValue($parse_xml_result));
} }
} }
$this->assertEquals($expected_return, $diw->_load_data()); $this->assertEquals($expected_return, $diw->_load_data());
$this->assertEquals($parse_xml_result, is_array(get_option('hubblesite-daily-image-cache'))); $this->assertEquals($parse_xml_result, is_array(get_option('hubblesite-daily-image-cache')));
} }
function providerTestWidowProtection() { function providerTestWidowProtection() {
return array( return array(
array("this is fixed", "this is&nbsp;fixed"), 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>this is fixed</a>", "<a>this is&nbsp;fixed</a>"),
array("<a href='meow'>word</a>", "<a href='meow'>word</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>') array("<p>this is fixed</p><p>Also fixed</p>", '<p>this is&nbsp;fixed</p><p>Also&nbsp;fixed</p>')
); );
} }
/** /**
* @dataProvider providerTestWidowProtection * @dataProvider providerTestWidowProtection
*/ */
@ -322,4 +321,4 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
} }
} }
?> ?>