post-fixtures/test/FixtureBuilderTest.php

266 lines
6.1 KiB
PHP
Raw Normal View History

2010-01-16 17:23:15 +00:00
<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once(dirname(__FILE__) . '/../classes/FixtureBuilder.inc');
class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
}
function providerTestDeferBuild() {
return array(
array(
array(
array('category', array('test'))
),
array('category' => array('test')),
array(true)
),
array(
array(
2010-01-18 00:41:10 +00:00
array('category', array('test,test2'))
2010-01-16 17:23:15 +00:00
),
array('category' => array('test', 'test2')),
array(true)
),
array(
array(
array('category', array('test')),
array('post', array('Post title')),
),
array('category' => array('test')),
array(true, false)
),
array(
array(
array('category', array('test')),
array('category', array('test2')),
),
array('category' => array('test', 'test2')),
array(true, true)
),
array(
array(
array('post', array('Post title')),
2010-01-18 00:41:10 +00:00
array('date', array('2010-01-01')),
2010-01-23 16:32:34 +00:00
array('categories', array('test,test2/test3')),
2010-01-18 00:41:10 +00:00
array('metadata', array('key', array('metadata' => 'value'))),
2010-01-18 02:28:26 +00:00
array('post', array('Post title 2')),
array('date', array('2010-01-02')),
2010-01-16 17:23:15 +00:00
),
2010-01-18 00:41:10 +00:00
array(
'post' => array(
array(
'post_title' => 'Post title',
'post_type' => 'post',
'post_date' => '2010-01-01',
2010-01-23 16:32:34 +00:00
'categories' => array('test', 'test2/test3'),
2010-01-18 00:41:10 +00:00
'metadata' => array('key' => array('metadata' => 'value'))
2010-01-18 02:28:26 +00:00
),
array(
'post_title' => 'Post title 2',
'post_type' => 'post',
'post_date' => '2010-01-02',
),
2010-01-18 00:41:10 +00:00
),
),
2010-01-18 02:28:26 +00:00
array(true, true, true, true, true, true)
2010-01-16 17:23:15 +00:00
),
);
}
/**
* @dataProvider providerTestDeferBuild
*/
function testDeferBuild($instructions, $expected_deferred_build, $expected_exceptions) {
$builder = new FixtureBuilder();
foreach ($instructions as $info) {
list($instruction, $parameters) = $info;
$expected_exception = array_shift($expected_exceptions);
try {
$return = call_user_func_array(array($builder, $instruction), $parameters);
$this->assertTrue($expected_exception);
} catch (Exception $e) {
2010-01-18 00:41:10 +00:00
$this->assertFalse($expected_exception, $e->getMessage());
2010-01-16 17:23:15 +00:00
$this->assertType('LogicException', $e);
}
}
$builder->defer();
$this->assertEquals($expected_deferred_build, $builder->deferred_builds);
}
2010-01-18 00:41:10 +00:00
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')
),
2010-01-18 05:15:11 +00:00
array(
'content',
array('Post content')
),
2010-01-18 00:41:10 +00:00
array(
'categories',
array('test/test2')
2010-01-18 00:41:10 +00:00
),
array(
'metadata',
array('test', 'test2')
2010-01-18 04:56:41 +00:00
),
array(
'tags',
array('tag1,tag2')
2010-01-18 00:41:10 +00:00
)
),
(object)array(
'post_title' => 'Post title',
2010-01-18 05:15:11 +00:00
'post_content' => 'Post content',
2010-01-18 00:41:10 +00:00
'post_type' => 'post',
'post_date' => '2010-01-01',
'post_status' => 'publish',
2010-01-18 04:56:41 +00:00
'ID' => 1
2010-01-18 00:41:10 +00:00
),
array(
'posts' => array(
1 => (object)array(
'post_title' => 'Post title',
2010-01-18 05:15:11 +00:00
'post_content' => 'Post content',
2010-01-18 00:41:10 +00:00
'post_type' => 'post',
'post_date' => '2010-01-01',
'post_status' => 'publish',
'ID' => 1
)
),
'categories' => array(
1 => 'test',
2 => 'test2'
2010-01-18 00:41:10 +00:00
),
2010-01-18 04:56:41 +00:00
'tags' => array(
1 => array('tag1', 'tag2')
),
2010-01-18 00:41:10 +00:00
'post_meta' => array(
1 => array(
'test' => 'test2'
)
),
'post_categories' => array(
1 => array(2)
2010-01-18 00:41:10 +00:00
)
)
2010-01-18 05:15:11 +00:00
),
2010-01-18 00:41:10 +00:00
);
}
/**
* @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;
case 'post_categories':
foreach ($info as $post_id => $expected_categories) {
$this->assertEquals($expected_categories, wp_get_post_categories($post_id));
}
break;
2010-01-18 00:41:10 +00:00
}
}
2010-02-12 23:39:10 +00:00
$this->assertTrue(!isset($builder->current_object));
2010-01-18 00:41:10 +00:00
}
2010-01-18 02:28:26 +00:00
function testBuildEmpty() {
$builder = new FixtureBuilder();
$builder->build();
}
2010-01-18 03:59:05 +00:00
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();
}
2010-01-18 05:15:11 +00:00
2010-01-23 16:32:34 +00:00
function providerTestOption() {
return array(
array('test', 'value', 'value'),
array('test2', false, false)
);
}
/**
* @dataProvider providerTestOption
*/
function testOption($key, $value, $expected_value) {
2010-01-18 05:15:11 +00:00
$fb = new FixtureBuilder();
2010-01-23 16:32:34 +00:00
$fb->option($key, $value);
2010-01-18 05:15:11 +00:00
2010-01-23 16:32:34 +00:00
$this->assertEquals($expected_value, get_option($key));
2010-01-18 05:15:11 +00:00
}
2010-01-16 17:23:15 +00:00
}