working on builder
This commit is contained in:
parent
c37be6be2b
commit
81f6496efe
@ -62,15 +62,71 @@ class FixtureBuilder {
|
||||
function defer() {
|
||||
if (!empty($this->current_object)) {
|
||||
$this->ensure_type($this->current_object['type']);
|
||||
$this->{"build_{$this->current_object['type']}"}($this->current_object);
|
||||
$this->{"defer_{$this->current_object['type']}"}($this->current_object);
|
||||
}
|
||||
}
|
||||
|
||||
function build() {
|
||||
if (!empty($this->current_object)) {
|
||||
$this->ensure_type($this->current_object['type']);
|
||||
return $this->{"build_{$this->current_object['type']}"}($this->current_object);
|
||||
}
|
||||
}
|
||||
|
||||
function categories($categories) {
|
||||
$this->check_correct_type('post');
|
||||
|
||||
$this->current_object['post']['categories'] = explode(',', $categories);
|
||||
return $this;
|
||||
}
|
||||
|
||||
function metadata($key, $value) {
|
||||
$this->check_correct_type('post');
|
||||
|
||||
if (!isset($this->current_object['post'])) {
|
||||
$this->current_object['post'] = array();
|
||||
}
|
||||
$this->current_object['post']['metadata'][$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function defer_category($obj) {
|
||||
$this->deferred_builds['category'] = array_merge($this->deferred_builds['category'], explode(',', $obj['name']));
|
||||
}
|
||||
|
||||
function defer_post($obj) {
|
||||
$this->deferred_builds['post'][] = $obj['post'];
|
||||
}
|
||||
|
||||
function build_category($obj) {
|
||||
$this->deferred_builds['category'] = array_merge($this->deferred_builds['category'], explode('/', $obj['name']));
|
||||
return PostFixtures::create_category($obj['name']);
|
||||
}
|
||||
|
||||
function build_post($obj) {
|
||||
$this->deferred_builds['post'][] = $obj['post'];
|
||||
$post_obj = $obj['post'];
|
||||
foreach (array('categories', 'metadata') as $field) {
|
||||
unset($post_obj[$field]);
|
||||
}
|
||||
$post_id = PostFixtures::create_post($post_obj);
|
||||
if ($post_id != 0) {
|
||||
$category_ids = array();
|
||||
if (isset($obj['post']['categories'])) {
|
||||
$category_ids_by_slug = array();
|
||||
foreach ($obj['post']['categories'] as $category) {
|
||||
$category_ids_by_slug = array_merge($category_ids_by_slug, PostFixtures::create_category($category));
|
||||
}
|
||||
$category_ids = array_values($category_ids_by_slug);
|
||||
}
|
||||
|
||||
$metadata = array();
|
||||
if (isset($obj['post']['metadata'])) {
|
||||
foreach ($obj['post']['metadata'] as $key => $value) {
|
||||
PostFixtures::create_post_metadata($post_id, $key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return get_post($post_id);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -176,18 +176,24 @@ class PostFixtures {
|
||||
$category_ids_by_slug = array();
|
||||
if (is_array($categories)) {
|
||||
foreach ($categories as $category) {
|
||||
$nodes = explode('/', $category);
|
||||
$parent = 0;
|
||||
$joined_nodes = array();
|
||||
foreach ($nodes as $node) {
|
||||
$joined_nodes[] = $node;
|
||||
$parent = $category_ids_by_slug[implode('/', $joined_nodes)] = wp_insert_category(array('cat_name' => $node, 'slug' => $node, 'category_parent' => $parent));
|
||||
}
|
||||
$category_ids_by_slug = array_merge($category_ids_by_slug, $this->create_category($category));
|
||||
}
|
||||
}
|
||||
return $category_ids_by_slug;
|
||||
}
|
||||
|
||||
function create_category($category) {
|
||||
$category_ids_by_slug = array();
|
||||
$nodes = explode('/', $category);
|
||||
$parent = 0;
|
||||
$joined_nodes = array();
|
||||
foreach ($nodes as $node) {
|
||||
$joined_nodes[] = $node;
|
||||
$parent = $category_ids_by_slug[implode('/', $joined_nodes)] = wp_insert_category(array('cat_name' => $node, 'category_nicename' => $node, 'category_parent' => $parent));
|
||||
}
|
||||
return $category_ids_by_slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* The posts to create.
|
||||
* Post data is passed in just as if you were using wp_insert_post().
|
||||
@ -197,11 +203,7 @@ class PostFixtures {
|
||||
$post_ids_created = array();
|
||||
if (is_array($posts)) {
|
||||
foreach ($posts as $post) {
|
||||
$post = (array)$post;
|
||||
if (!isset($post['post_status'])) {
|
||||
$post['post_status'] = 'publish';
|
||||
}
|
||||
$id = wp_insert_post($post);
|
||||
$id = $this->create_post($post);
|
||||
if ($id != 0) {
|
||||
$post_ids_created[] = $id;
|
||||
if (isset($post['categories'])) {
|
||||
@ -218,7 +220,7 @@ class PostFixtures {
|
||||
|
||||
if (isset($post['metadata'])) {
|
||||
foreach ($post['metadata'] as $key => $value) {
|
||||
update_post_meta($id, $key, $value);
|
||||
$this->create_post_metadata($id, $key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -227,6 +229,18 @@ class PostFixtures {
|
||||
return $post_ids_created;
|
||||
}
|
||||
|
||||
function create_post($post) {
|
||||
$post = (array)$post;
|
||||
if (!isset($post['post_status'])) {
|
||||
$post['post_status'] = 'publish';
|
||||
}
|
||||
return wp_insert_post($post);
|
||||
}
|
||||
|
||||
function create_post_metadata($post_id, $key, $value) {
|
||||
update_post_meta($post_id, $key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create everything from the provided data.
|
||||
* @param array $data The data to use in creation.
|
||||
|
@ -20,7 +20,7 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
),
|
||||
array(
|
||||
array(
|
||||
array('category', array('test/test2'))
|
||||
array('category', array('test,test2'))
|
||||
),
|
||||
array('category' => array('test', 'test2')),
|
||||
array(true)
|
||||
@ -44,16 +44,22 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
array(
|
||||
array(
|
||||
array('post', array('Post title')),
|
||||
array('date', array('2010-01-01'))
|
||||
array('date', array('2010-01-01')),
|
||||
array('categories', array('test,test2')),
|
||||
array('metadata', array('key', array('metadata' => 'value'))),
|
||||
),
|
||||
array('post' => array(
|
||||
array(
|
||||
'post_title' => 'Post title',
|
||||
'post_date' => '2010-01-01',
|
||||
'post_type' => 'post',
|
||||
)
|
||||
)),
|
||||
array(true, true)
|
||||
array(
|
||||
'post' => array(
|
||||
array(
|
||||
'post_title' => 'Post title',
|
||||
'post_type' => 'post',
|
||||
'post_date' => '2010-01-01',
|
||||
'categories' => array('test', 'test2'),
|
||||
'metadata' => array('key' => array('metadata' => 'value'))
|
||||
)
|
||||
),
|
||||
),
|
||||
array(true, true, true, true)
|
||||
),
|
||||
);
|
||||
}
|
||||
@ -72,7 +78,7 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
$return = call_user_func_array(array($builder, $instruction), $parameters);
|
||||
$this->assertTrue($expected_exception);
|
||||
} catch (Exception $e) {
|
||||
$this->assertFalse($expected_exception);
|
||||
$this->assertFalse($expected_exception, $e->getMessage());
|
||||
$this->assertType('LogicException', $e);
|
||||
}
|
||||
}
|
||||
@ -81,4 +87,108 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
$this->assertEquals($expected_deferred_build, $builder->deferred_builds);
|
||||
}
|
||||
|
||||
function providerTestImmediateBuild() {
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
array(
|
||||
'category',
|
||||
array('test'),
|
||||
),
|
||||
array(
|
||||
'category',
|
||||
array('test/test2'),
|
||||
)
|
||||
),
|
||||
array('test' => 1, 'test/test2' => 2),
|
||||
array(
|
||||
'categories' => array(
|
||||
1 => 'test',
|
||||
2 => 'test2'
|
||||
)
|
||||
)
|
||||
),
|
||||
array(
|
||||
array(
|
||||
array(
|
||||
'post',
|
||||
array('Post title')
|
||||
),
|
||||
array(
|
||||
'date',
|
||||
array('2010-01-01')
|
||||
),
|
||||
array(
|
||||
'categories',
|
||||
array('test')
|
||||
),
|
||||
array(
|
||||
'metadata',
|
||||
array('test', 'test2')
|
||||
)
|
||||
),
|
||||
(object)array(
|
||||
'post_title' => 'Post title',
|
||||
'post_type' => 'post',
|
||||
'post_date' => '2010-01-01',
|
||||
'post_status' => 'publish',
|
||||
'ID' => 1
|
||||
),
|
||||
array(
|
||||
'posts' => array(
|
||||
1 => (object)array(
|
||||
'post_title' => 'Post title',
|
||||
'post_type' => 'post',
|
||||
'post_date' => '2010-01-01',
|
||||
'post_status' => 'publish',
|
||||
'ID' => 1
|
||||
)
|
||||
),
|
||||
'categories' => array(
|
||||
1 => 'test'
|
||||
),
|
||||
'post_meta' => array(
|
||||
1 => array(
|
||||
'test' => 'test2'
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestImmediateBuild
|
||||
*/
|
||||
function testImmediateBuild($instructions, $expected_return, $wp_checks) {
|
||||
$builder = new FixtureBuilder();
|
||||
foreach ($instructions as $info) {
|
||||
list($instruction, $parameters) = $info;
|
||||
call_user_func_array(array($builder, $instruction), $parameters);
|
||||
}
|
||||
$this->assertEquals($expected_return, $builder->build());
|
||||
|
||||
foreach ($wp_checks as $type => $info) {
|
||||
switch ($type) {
|
||||
case 'posts':
|
||||
foreach ($info as $key => $post) {
|
||||
$this->assertEquals($post, get_post($key));
|
||||
}
|
||||
break;
|
||||
case 'categories':
|
||||
foreach ($info as $key => $category_name) {
|
||||
$this->assertEquals($category_name, get_cat_name($key));
|
||||
}
|
||||
break;
|
||||
case 'post_meta':
|
||||
foreach ($info as $post_id => $meta_info) {
|
||||
foreach ($meta_info as $key => $value) {
|
||||
$this->assertEquals($value, get_post_meta($post_id, $key, true));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user