get options merging working

This commit is contained in:
John Bintz 2009-11-12 07:56:32 -05:00
parent b287c05e21
commit 2de2df60ff
2 changed files with 201 additions and 110 deletions

View File

@ -7,18 +7,30 @@ require_once(dirname(__FILE__) . '/ComicPressComicPost.inc');
*/ */
class ComicPress { class ComicPress {
var $comicpress_options = array( var $comicpress_options = array(
'comic_dimensions' => '760x', 'image_types' => array(
'rss_dimensions' => '350x', 'comic' => array(
'archive_dimensions' => '125x', 'default' => true,
'mini_dimensions' => '100x', 'name' => 'Comic',
'dimensions' => '760x'
),
'rss' => array(
'default' => false,
'name' => 'RSS',
'dimensions' => '350x'
),
'archive' => array(
'default' => false,
'name' => 'Archive',
'dimensions' => '125x'
),
'mini' => array(
'default' => false,
'name' => 'Minithumb',
'dimensions' => '100x'
),
),
'helpers' => array(), 'helpers' => array(),
'addons' => array(),
'storyline_order' => '', 'storyline_order' => '',
'subattachment_types' => array(
'rss' => 'RSS',
'archive' => 'Archive',
'mini' => 'Mini Thumb'
)
); );
var $backends = array(); var $backends = array();
@ -39,10 +51,34 @@ class ComicPress {
function load() { function load() {
$result = get_option('comicpress-options'); $result = get_option('comicpress-options');
if (is_array($result)) { if (is_array($result)) {
$this->comicpress_options = array_merge($this->comicpress_options, $result); $this->comicpress_options = $this->_array_merge_replace_recursive($this->comicpress_options, $result);
} }
} }
function _array_merge_replace_recursive() {
$args = func_get_args();
foreach ($args as $arg) {
if (!is_array($arg)) { return end($args); }
}
$all_keys = array();
$result = array();
foreach ($args as $arg) {
$all_keys = array_merge($all_keys, array_keys($arg));
}
foreach ($all_keys as $key) {
$parts_to_merge = array();
foreach ($args as $arg) {
if (isset($arg[$key])) { $parts_to_merge[] = $arg[$key]; }
}
$result[$key] = call_user_func_array(array($this, '_array_merge_replace_recursive'), $parts_to_merge);
}
return $result;
}
/** /**
* Save ComicPress options. * Save ComicPress options.
*/ */

View File

@ -9,119 +9,119 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
function setUp() { function setUp() {
_reset_wp(); _reset_wp();
$this->cp = new ComicPress(); $this->cp = new ComicPress();
vfsStreamWrapper::register(); vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root')); vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
} }
function providerTestCategorySearch() { function providerTestCategorySearch() {
return array( return array(
array( array(
array('comic'), array(vfsStream::url('root/style/comic')) array('comic'), array(vfsStream::url('root/style/comic'))
), ),
array( array(
array('chapter-1', 'comic'), array(vfsStream::url('root/style/comic'), vfsStream::url('root/style/comic/chapter-1')) array('chapter-1', 'comic'), array(vfsStream::url('root/style/comic'), vfsStream::url('root/style/comic/chapter-1'))
), ),
array( array(
array('part-1', 'chapter-1', 'comic'), array(vfsStream::url('root/style/comic'), vfsStream::url('root/style/comic/chapter-1'), vfsStream::url('root/style/comic/chapter-1/part-1')) array('part-1', 'chapter-1', 'comic'), array(vfsStream::url('root/style/comic'), vfsStream::url('root/style/comic/chapter-1'), vfsStream::url('root/style/comic/chapter-1/part-1'))
), ),
array( array(
array('comic', 'chapter-1'), array() array('comic', 'chapter-1'), array()
), ),
array( array(
array(), array() array(), array()
) )
); );
} }
/** /**
* @dataProvider providerTestCategorySearch * @dataProvider providerTestCategorySearch
*/ */
function testCategorySearch($categories, $found_path) { function testCategorySearch($categories, $found_path) {
mkdir(vfsStream::url('root/style/comic/chapter-1/part-1'), 0777, true); mkdir(vfsStream::url('root/style/comic/chapter-1/part-1'), 0777, true);
$this->assertEquals($found_path, $this->cp->category_search($categories, vfsStream::url('root/style'))); $this->assertEquals($found_path, $this->cp->category_search($categories, vfsStream::url('root/style')));
} }
function providerTestFindFile() { function providerTestFindFile() {
return array( return array(
array( array(
array(), 'partials', array(), false, array(), 'partials', array(), false,
), ),
array( array(
array('root/parent/partials/index.inc'), array('root/parent/partials/index.inc'),
'partials', 'partials',
array(), array(),
vfsStream::url('root/parent/partials/index.inc') vfsStream::url('root/parent/partials/index.inc')
), ),
array( array(
array('root/parent/index.inc'), array('root/parent/index.inc'),
'', '',
array(), array(),
vfsStream::url('root/parent/index.inc') vfsStream::url('root/parent/index.inc')
), ),
array( array(
array( array(
'root/parent/partials/index.inc', 'root/parent/partials/index.inc',
'root/child/partials/index.inc' 'root/child/partials/index.inc'
), ),
'partials', 'partials',
array(), array(),
vfsStream::url('root/child/partials/index.inc') vfsStream::url('root/child/partials/index.inc')
), ),
array( array(
array( array(
'root/child/partials/index.inc', 'root/child/partials/index.inc',
'root/child/partials/comic/index.inc' 'root/child/partials/comic/index.inc'
), ),
'partials', 'partials',
array('comic'), array('comic'),
vfsStream::url('root/child/partials/comic/index.inc') vfsStream::url('root/child/partials/comic/index.inc')
), ),
array( array(
array( array(
'root/child/partials/index.inc', 'root/child/partials/index.inc',
'root/child/partials/comic/index.inc' 'root/child/partials/comic/index.inc'
), ),
'partials', 'partials',
array('chapter-1', 'comic'), array('chapter-1', 'comic'),
vfsStream::url('root/child/partials/comic/index.inc') vfsStream::url('root/child/partials/comic/index.inc')
), ),
array( array(
array( array(
'root/child/partials/index.inc', 'root/child/partials/index.inc',
'root/child/partials/comic/index.inc' 'root/child/partials/comic/index.inc'
), ),
'partials', 'partials',
null, null,
vfsStream::url('root/child/partials/comic/index.inc') vfsStream::url('root/child/partials/comic/index.inc')
) )
); );
} }
/** /**
* @dataProvider providerTestFindFile * @dataProvider providerTestFindFile
*/ */
function testFindFile($files_to_setup, $search_path, $post_categories, $expected_path_result) { function testFindFile($files_to_setup, $search_path, $post_categories, $expected_path_result) {
global $post; global $post;
mkdir(vfsStream::url('root/parent/partials/comic/chapter-1'), 0777, true);
mkdir(vfsStream::url('root/child/partials/comic/chapter-1'), 0777, true);
foreach ($files_to_setup as $path) {
file_put_contents(vfsStream::url($path), "test");
}
_set_template_directory(vfsStream::url('root/parent'));
_set_stylesheet_directory(vfsStream::url('root/child'));
$post = (object)array('ID' => 1); mkdir(vfsStream::url('root/parent/partials/comic/chapter-1'), 0777, true);
wp_set_post_categories(1, array(2)); mkdir(vfsStream::url('root/child/partials/comic/chapter-1'), 0777, true);
add_category(1, (object)array('slug' => 'comic', 'parent' => 0)); foreach ($files_to_setup as $path) {
add_category(2, (object)array('slug' => 'chapter-1', 'parent' => 1)); file_put_contents(vfsStream::url($path), "test");
}
$this->assertEquals($expected_path_result, $this->cp->find_file('index.inc', $search_path, $post_categories));
_set_template_directory(vfsStream::url('root/parent'));
_set_stylesheet_directory(vfsStream::url('root/child'));
$post = (object)array('ID' => 1);
wp_set_post_categories(1, array(2));
add_category(1, (object)array('slug' => 'comic', 'parent' => 0));
add_category(2, (object)array('slug' => 'chapter-1', 'parent' => 1));
$this->assertEquals($expected_path_result, $this->cp->find_file('index.inc', $search_path, $post_categories));
} }
function providerTestLoad() { function providerTestLoad() {
@ -129,19 +129,74 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
array(false, 'default'), array(false, 'default'),
array(array(), 'default'), array(array(), 'default'),
array(array( array(array(
'comic_dimensions' => '1000x' 'image_types' => array(
), '1000x') 'comic' => array(
'dimensions' => '1000x'
)
)
), '1000x'),
array(array(
'image_types' => array(
'comic' => array(
'test' => 'hello'
)
)
), 'default')
); );
} }
/** /**
* @dataProvider providerTestLoad * @dataProvider providerTestLoad
*/ */
function testLoad($options_array, $expected_dimensions) { function testLoad($options_array, $expected_dimensions) {
update_option('comicpress-options', $options_array); update_option('comicpress-options', $options_array);
if ($expected_dimensions == 'default') { $expected_dimensions = $this->cp->comicpress_options['comic_dimensions']; } if ($expected_dimensions == 'default') { $expected_dimensions = $this->cp->comicpress_options['image_types']['comic']['dimensions']; }
$this->cp->load(); $this->cp->load();
$this->assertEquals($expected_dimensions, $this->cp->comicpress_options['comic_dimensions']); $this->assertEquals($expected_dimensions, $this->cp->comicpress_options['image_types']['comic']['dimensions']);
}
function providerTestArrayMergeReplaceRecursive() {
return array(
array(
array(1,2,3),
3
),
array(
array(
array(3),
array(5),
),
array(5)
),
array(
array(
array('test' => 3),
array('test' => 5),
),
array('test' => 5)
),
array(
array(
array('test' => array('test2' => 3)),
array('test' => array('test2' => 5)),
),
array('test' => array('test2' => 5))
),
array(
array(
array('test' => array()),
array('test' => array('test2' => 5)),
),
array('test' => array('test2' => 5))
),
);
}
/**
* @dataProvider providerTestArrayMergeReplaceRecursive
*/
function testArrayMergeReplaceRecursive($inputs, $expected_output) {
$this->assertEquals($expected_output, call_user_func_array(array($this->cp, '_array_merge_replace_recursive'), $inputs));
} }
} }