more coverage

This commit is contained in:
John Bintz 2010-01-17 22:59:05 -05:00
parent f3e65a200d
commit 9258e1f4e7
6 changed files with 136 additions and 23 deletions

View File

@ -64,6 +64,7 @@ class FixtureBuilder {
$this->ensure_type($this->current_object['type']);
$this->{"defer_{$this->current_object['type']}"}($this->current_object);
}
unset($this->current_object);
}
function build() {
@ -113,6 +114,7 @@ class FixtureBuilder {
$category_ids_by_slug = array_merge($category_ids_by_slug, PostFixtures::create_category($category));
}
$category_ids = array_values($category_ids_by_slug);
PostFixtures::set_post_categories($post_id, $category_ids);
}
$metadata = array();
@ -128,4 +130,23 @@ class FixtureBuilder {
return false;
// @codeCoverageIgnoreEnd
}
function finalize() {
$this->defer();
foreach ($this->deferred_builds as $type => $entries) {
foreach ($entries as $entry) {
$object = null;
switch ($type) {
case 'category':
$object = array('name' => $entry);
break;
case 'post':
$object = array('post' => $entry);
break;
}
$this->{"build_${type}"}($object);
}
}
}
}

View File

@ -49,15 +49,40 @@ class PostFixtures {
*/
function handle_update($info) {
if (isset($info['is_ok'])) {
$data = $this->parse_json(stripslashes($info['data']));
if (!empty($data)) {
$data = $this->process_data($data);
$this->remove();
$this->create($data);
$this->messages[] = __("New data set loaded into WordPress.", 'post-fixtures');
} else {
$this->messages[] = __("Data is not valid JSON.", 'post-fixtures');
}
if (strpos($info['data'], 'file:') === 0) {
$this->handle_load_file(substr($info['data'], 5));
} else {
$this->handle_json($info['data']);
}
}
}
function handle_load_file($file) {
$file = realpath($this->wpcontent_path() . '/' . $file);
if (file_exists($file)) {
switch (pathinfo($file, PATHINFO_EXTENSION)) {
case "json":
$this->handle_json(file_get_contents($file));
break;
case "inc";
$this->remove();
$builder = new FixtureBuilder();
include($file);
$builder->finalize();
break;
}
}
}
function handle_json($input) {
$data = $this->parse_json(stripslashes($input));
if (!empty($data)) {
$data = $this->process_data($data);
$this->remove();
$this->create($data);
$this->messages[] = __("New data set loaded into WordPress.", 'post-fixtures');
} else {
$this->messages[] = __("Data is not valid JSON.", 'post-fixtures');
}
}
@ -128,7 +153,7 @@ class PostFixtures {
$dh = opendir($dir);
$is_fixture_dir = (basename($dir) === 'fixtures');
while ($file = readdir($dh)) {
if ($file[0] != '.') {
if ($file[0] != '.') {
$target = $dir . '/' . $file;
if ($is_fixture_dir) {
if (is_file($target)) {
@ -286,6 +311,10 @@ class PostFixtures {
update_post_meta($post_id, $key, $value);
}
function set_post_categories($post_id, $categories) {
wp_set_post_categories($post_id, $categories);
}
/**
* Create everything from the provided data.
* @param array $data The data to use in creation.
@ -339,9 +368,9 @@ class PostFixtures {
if (count($parts) == 1) {
$index = reset($parts);
if (isset($this->{"_${source}"})) {
if (isset($this->{"_${source}"}[$index])) {
$value = $this->{"_${source}"}[$index];
}
if (isset($this->{"_${source}"}[$index])) {
$value = $this->{"_${source}"}[$index];
}
}
}
}

View File

@ -9,8 +9,20 @@
<form action="" method="post" id="post-fixtures-form">
<input type="hidden" name="pf[_nonce]" value="<?php echo wp_create_nonce('post-fixtures') ?>" />
<h3><?php _e('JSON data to load into the database:', 'post-fixtures') ?></h3>
<textarea name="pf[data]" rows="20" style="width: 100%"></textarea>
<div style="overflow: hidden">
<div style="float: left;display: inline;width: 60%">
<h3><?php _e('JSON data to load into the database:', 'post-fixtures') ?></h3>
<textarea name="pf[data]" rows="20" style="width: 100%"></textarea>
</div>
<div style="float: left;display: inline;width: 38%;margin-left:2%">
<h3><?php _e('Pre-built fixtures:', 'post-fixtures') ?></h3>
<ul id="pre-built-fixture-holder">
<?php foreach ($fixtures as $fixture) { ?>
<li><a href="<?php echo esc_attr($fixture) ?>"><?php echo esc_html($fixture) ?></a></li>
<?php } ?>
</ul>
</div>
</div>
<label>
<input type="checkbox" name="pf[is_ok]" value="yes" /> <?php _e('Yes, I want Post Fixtures to <strong>delete all of my data and load this data instead.</strong>', 'post-fixtures') ?><br />
</label>
@ -27,6 +39,11 @@
$('#post-fixtures-additional-options').hide();
$('input[name*=is_ok]').get(0).checked = false;
$('#pre-built-fixture-holder a').click(function() {
$('textarea[name*=data]').val('file:' + $(this).attr('href'));
return false;
});
$('#post-fixtures-form').submit(function() {
var checkbox = $('input[name*=is_ok]', this.target).get(0);
var ok = false

View File

@ -27,7 +27,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
function __post_fixtures_plugins_loaded() {
if (version_compare(PHP_VERSION, '5.0.0') === 1) {
require_once('classes/PostFixtures.inc');
foreach (glob(dirname(__FILE__) . '/classes/*.inc') as $file) {
require_once($file);
}
$post_fixtures = new PostFixtures();
$post_fixtures->init();
} else {

View File

@ -204,4 +204,21 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
$builder->build();
}
function testFinalize() {
$fb = $this->getMock('FixtureBuilder', array('defer', 'build_category', 'build_post'));
$fb->deferred_builds = array(
'category' => array('test', 'test2'),
'post' => array('post3', 'post4')
);
$fb->expects($this->at(0))->method('defer');
$fb->expects($this->at(1))->method('build_category')->with(array('name' => 'test'));
$fb->expects($this->at(2))->method('build_category')->with(array('name' => 'test2'));
$fb->expects($this->at(3))->method('build_post')->with(array('post' => 'post3'));
$fb->expects($this->at(4))->method('build_post')->with(array('post' => 'post4'));
$fb->finalize();
}
}

View File

@ -288,25 +288,52 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
}
}
function providerTestHandleUpdate() {
function providerTestHandleJSON() {
return array(
array(array(), false),
array(array('is_ok' => true, 'data' => '{]'), false),
array(array('is_ok' => true, 'data' => '{"test": "test"}'), true),
array('', false),
array('{]', false),
array('{"test": "test"}', true),
);
}
/**
* @dataProvider providerTestHandleUpdate
* @dataProvider providerTestHandleJSON
*/
function testHandleUpdate($info, $should_succeed) {
function testHandleJSON($info, $should_succeed) {
$pf = $this->getMock('PostFixtures', array('process_data', 'remove', 'create'));
foreach (array('process_data', 'remove', 'create') as $method) {
$pf->expects($this->{$should_succeed ? 'once' : 'never'}())->method($method);
}
$pf->handle_update($info);
$pf->handle_json($info);
}
function providerTestHandleUpdate() {
return array(
array(
array(), false
),
array(
array('is_ok' => true, 'data' => 'json'), array('handle_json', 'json')
),
array(
array('is_ok' => true, 'data' => 'file:test'), array('handle_load_file', 'test')
),
);
}
/**
* @dataProvider providerTestHandleUpdate
*/
function testHandleUpdate($input, $expected_method) {
$pf = $this->getMock('PostFixtures', array('handle_load_file', 'handle_json'));
if (!empty($expected_method)) {
$pf->expects($this->once())->method($expected_method[0])->with($expected_method[1]);
}
$pf->handle_update($input);
}
function providerTestAdminInit() {