test for save

This commit is contained in:
John Bintz 2009-11-29 13:27:06 -05:00
parent 090c43691f
commit dde0023ef9
3 changed files with 64 additions and 10 deletions

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
.PHONY : test-coverage
test-coverage :
phpunit --syntax-check --coverage-html coverage test

View File

@ -55,15 +55,13 @@ class QuickAudioEmbed {
function save($info) {
foreach ($info as $key => $value) {
switch ($key) {
case 'dimensions':
if (count($result = explode('x', $value)) !== 2) {
unset($info[$key]);
} else {
$result = array_map('intval', $result);
$info[$key] = implode('x', $result);
}
break;
if (method_exists($this, "_save_${key}")) {
$result = $this->{"_save_${key}"}($value);
if (is_null($result)) {
unset($info[$key]);
} else {
$info[$key] = $result;
}
}
}
@ -71,6 +69,23 @@ class QuickAudioEmbed {
update_option('quick-audio-embed-settings', $info);
}
function _save_dimensions($value) {
if (is_string($value)) {
if (count($result = explode('x', $value)) === 2) {
$new_result = array();
foreach ($result as $v) {
if (is_numeric($v)) {
$new_result[] = $v;
} else {
return null;
}
}
return implode('x', array_map('intval', $new_result));
}
}
return null;
}
function admin_menu() {
add_settings_section('quick-audio-embed', __('Quick Audio Embed', 'quick-audio-embed'), array(&$this, 'media_settings'), 'media');

View File

@ -5,5 +5,40 @@ require_once('MockPress/mockpress.php');
require_once(dirname(__FILE__) . '/../quick-audio-embed.php');
class QuickAudioEmbedTest extends PHPUnit_Framework_TestCase {
function setUp() { _reset_wp(); }
function setUp() {
_reset_wp();
$this->qae = new QuickAudioEmbed();
}
function providerTestSaveDimensions() {
return array(
array('', null),
array(false, null),
array(array(), null),
array('350x', null),
array('x350', null),
array('350xx350', null),
array('350x350', '350x350'),
array('350.1x350.1', '350x350'),
);
}
/**
* @dataProvider providerTestSaveDimensions
*/
function testSaveDimensions($input, $expected_output) {
$this->assertEquals($expected_output, $this->qae->_save_dimensions($input));
}
function testSave() {
$qae = $this->getMock('QuickAudioEmbed', array('_save_test', '_save_test2', '_save_test3'));
$qae->expects($this->once())->method('_save_test')->with('test');
$qae->expects($this->never())->method('_save_test2');
$qae->expects($this->once())->method('_save_test3')->with('test3')->will($this->returnValue('test4'));
$qae->save(array('test' => 'test', 'test3' => 'test3', 'test5' => 'test5'));
$this->assertEquals(array('test3' => 'test4', 'test5' => 'test5'), get_option('quick-audio-embed-settings'));
$this->assertEquals(array('test3' => 'test4', 'test5' => 'test5'), $qae->settings);
}
}