From dde0023ef91d39fecdf567f251091cb87bbd8cf5 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 29 Nov 2009 13:27:06 -0500 Subject: [PATCH] test for save --- Makefile | 4 ++++ quick-audio-embed.php | 33 +++++++++++++++++++++++--------- test/QuickAudioEmbedTest.php | 37 +++++++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8ee6751 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PHONY : test-coverage + +test-coverage : + phpunit --syntax-check --coverage-html coverage test diff --git a/quick-audio-embed.php b/quick-audio-embed.php index 2375e50..54a2569 100644 --- a/quick-audio-embed.php +++ b/quick-audio-embed.php @@ -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'); diff --git a/test/QuickAudioEmbedTest.php b/test/QuickAudioEmbedTest.php index 28b0032..36b3c97 100644 --- a/test/QuickAudioEmbedTest.php +++ b/test/QuickAudioEmbedTest.php @@ -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); + } }