2010-01-28 01:21:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once('PHPUnit/Framework.php');
|
|
|
|
require_once('MockPress/mockpress.php');
|
|
|
|
require_once(dirname(__FILE__) . '/../classes/ComicPressTagBuilder.inc');
|
|
|
|
require_once(dirname(__FILE__) . '/../classes/ComicPressStoryline.inc');
|
|
|
|
|
|
|
|
class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
|
|
|
|
function setUp() {
|
|
|
|
_reset_wp();
|
|
|
|
}
|
|
|
|
|
2010-01-28 03:05:29 +00:00
|
|
|
function testNewFactory() {
|
|
|
|
$core = new ComicPressTagBuilderFactory();
|
|
|
|
}
|
|
|
|
|
2010-01-28 01:21:53 +00:00
|
|
|
function providerTestBuilder() {
|
|
|
|
return array(
|
|
|
|
array(
|
2010-01-28 03:05:29 +00:00
|
|
|
array(
|
|
|
|
array('next')
|
|
|
|
),
|
2010-01-28 01:21:53 +00:00
|
|
|
array('get_next_post', array(1,2,3,4,5), 'current-post')
|
2010-01-28 03:05:29 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
array('previous'),
|
|
|
|
),
|
|
|
|
array('get_previous_post', array(1,2,3,4,5), 'current-post')
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
array('first'),
|
|
|
|
),
|
|
|
|
array('get_first_post', array(1,2,3,4,5), 'current-post')
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
array('last'),
|
|
|
|
),
|
|
|
|
array('get_last_post', array(1,2,3,4,5), 'current-post')
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
array('in', 'category-1'),
|
|
|
|
array('last')
|
|
|
|
),
|
|
|
|
array('get_last_post', array(1), 'current-post')
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
array('in', 2),
|
|
|
|
array('first')
|
|
|
|
),
|
|
|
|
array('get_first_post', array(2,3,4), 'current-post')
|
|
|
|
),
|
2010-02-03 02:06:39 +00:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
array('in', 2),
|
|
|
|
array('first'),
|
|
|
|
array('setup')
|
|
|
|
),
|
|
|
|
array('get_first_post', array(2,3,4), 'current-post'),
|
|
|
|
true
|
|
|
|
),
|
2010-02-03 02:12:31 +00:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
array('from', (object)array('other-post' => 'post')),
|
|
|
|
array('in', 2),
|
|
|
|
array('first')
|
|
|
|
),
|
|
|
|
array('get_first_post', array(2,3,4), (object)array('other-post' => 'post')),
|
2010-02-04 01:00:55 +00:00
|
|
|
),
|
2010-01-28 01:21:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestBuilder
|
|
|
|
*/
|
2010-02-03 02:06:39 +00:00
|
|
|
function testStorylineBuilder($instructions, $expected_dbi_call, $expects_setup_postdata = false) {
|
2010-01-28 03:05:29 +00:00
|
|
|
global $post, $wp_test_expectations;
|
2010-01-28 01:21:53 +00:00
|
|
|
$post = 'current-post';
|
|
|
|
|
|
|
|
$method = array_shift($expected_dbi_call);
|
|
|
|
|
|
|
|
$dbi = $this->getMock('ComicPressDBInterface', array($method));
|
|
|
|
$expectation = $dbi->expects($this->once())->method($method);
|
|
|
|
call_user_func_array(array($expectation, 'with'), $expected_dbi_call);
|
|
|
|
|
2010-02-03 02:06:39 +00:00
|
|
|
if ($expects_setup_postdata) {
|
|
|
|
call_user_func(array($expectation, 'will'), $this->returnValue('new-post'));
|
|
|
|
}
|
|
|
|
|
2010-01-28 01:21:53 +00:00
|
|
|
$core = new ComicPressTagBuilderFactory($dbi);
|
|
|
|
|
|
|
|
$storyline = new ComicPressStoryline();
|
|
|
|
$storyline->set_flattened_storyline('0/1,0/2,0/2/3,0/2/4,0/5');
|
|
|
|
|
|
|
|
foreach (array(
|
2010-01-28 03:05:29 +00:00
|
|
|
1 => array('cat_name' => 'Test 1', 'category_nicename' => 'category-1', 'category_parent' => 0),
|
|
|
|
2 => array('cat_name' => 'Test 2', 'category_nicename' => 'category-2', 'category_parent' => 0),
|
|
|
|
3 => array('cat_name' => 'Test 3', 'category_nicename' => 'category-3', 'category_parent' => 2),
|
|
|
|
4 => array('cat_name' => 'Test 4', 'category_nicename' => 'category-4', 'category_parent' => 2),
|
|
|
|
5 => array('cat_name' => 'Test 5', 'category_nicename' => 'category-5', 'category_parent' => 0),
|
|
|
|
) as $id => $category) {
|
|
|
|
add_category($id, (object)$category);
|
2010-01-28 01:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($instructions as $instruction) {
|
2010-01-28 03:05:29 +00:00
|
|
|
$method = array_shift($instruction);
|
|
|
|
$core = call_user_func_array(array($core, $method), $instruction);
|
2010-01-28 01:21:53 +00:00
|
|
|
}
|
2010-02-03 02:06:39 +00:00
|
|
|
|
|
|
|
if ($expects_setup_postdata) {
|
|
|
|
$this->assertEquals('new-post', $post);
|
|
|
|
}
|
2010-01-28 01:21:53 +00:00
|
|
|
}
|
2010-01-30 15:54:23 +00:00
|
|
|
|
2010-02-03 02:29:09 +00:00
|
|
|
function providerTestPostIn() {
|
|
|
|
return array(
|
|
|
|
array(null, false),
|
|
|
|
array((object)array('ID' => 2), true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestPostIn
|
|
|
|
*/
|
|
|
|
function testPostIn($post_to_use = null, $expected_result) {
|
|
|
|
global $post;
|
|
|
|
|
|
|
|
$post = (object)array('ID' => 1);
|
|
|
|
wp_insert_post($post);
|
|
|
|
|
|
|
|
$storyline = new ComicPressStoryline();
|
|
|
|
$storyline->set_flattened_storyline('0/1,0/2,0/2/3,0/2/4,0/5');
|
|
|
|
|
|
|
|
foreach (array(
|
|
|
|
1 => array('cat_name' => 'Test 1', 'category_nicename' => 'category-1', 'category_parent' => 0),
|
|
|
|
2 => array('cat_name' => 'Test 2', 'category_nicename' => 'category-2', 'category_parent' => 0),
|
|
|
|
3 => array('cat_name' => 'Test 3', 'category_nicename' => 'category-3', 'category_parent' => 2),
|
|
|
|
4 => array('cat_name' => 'Test 4', 'category_nicename' => 'category-4', 'category_parent' => 2),
|
|
|
|
5 => array('cat_name' => 'Test 5', 'category_nicename' => 'category-5', 'category_parent' => 0),
|
|
|
|
) as $id => $category) {
|
|
|
|
add_category($id, (object)$category);
|
|
|
|
}
|
|
|
|
|
|
|
|
wp_set_post_categories(1, array(1));
|
|
|
|
wp_set_post_categories(2, array(2));
|
|
|
|
|
|
|
|
$dbi = $this->getMock('ComicPressDBInterface');
|
|
|
|
$core = new ComicPressTagBuilderFactory($dbi);
|
|
|
|
|
|
|
|
$post_to_use = empty($post_to_use) ? $post : $post_to_use;
|
|
|
|
|
|
|
|
$this->assertEquals($expected_result, $core->post_in('category-2', $post_to_use));
|
|
|
|
}
|
|
|
|
|
2010-01-30 21:00:41 +00:00
|
|
|
private function setupStorylineBuilderTest($for_exceptions = false) {
|
|
|
|
$target_post = (object)array(
|
|
|
|
'ID' => 1,
|
|
|
|
'post_title' => 'Post title',
|
|
|
|
'post_date' => '2010-01-01',
|
|
|
|
'guid' => 'the-slug',
|
|
|
|
);
|
|
|
|
|
|
|
|
wp_insert_post($target_post);
|
|
|
|
|
|
|
|
$dbi = $this->getMock('ComicPressDBInterface', array('get_first_post'));
|
|
|
|
$expectation = $dbi->expects($this->any())->method('get_first_post');
|
|
|
|
if (!$for_exceptions) {
|
|
|
|
$expectation->with(array('1'))->will($this->returnValue($target_post));
|
|
|
|
}
|
|
|
|
|
|
|
|
$storyline = new ComicPressStoryline();
|
|
|
|
$storyline->set_flattened_storyline('0/1');
|
|
|
|
|
|
|
|
return compact('target_post', 'dbi', 'storyline');
|
|
|
|
}
|
|
|
|
|
2010-01-30 15:54:23 +00:00
|
|
|
function providerTestStorylineBuilderHandlePost() {
|
|
|
|
return array(
|
|
|
|
array('id', 1),
|
|
|
|
array('title', 'Post title'),
|
|
|
|
array('timestamp', strtotime('2010-01-01')),
|
2010-01-30 15:55:39 +00:00
|
|
|
array('permalink', 'the-slug'),
|
|
|
|
array('post', (object)array(
|
|
|
|
'ID' => 1,
|
|
|
|
'post_title' => 'Post title',
|
|
|
|
'post_date' => '2010-01-01',
|
|
|
|
'guid' => 'the-slug',
|
2010-01-30 16:11:07 +00:00
|
|
|
)),
|
|
|
|
array(array('date', 'Ymd'), '20100101'),
|
2010-01-30 15:54:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestStorylineBuilderHandlePost
|
|
|
|
*/
|
|
|
|
function testStorylineBuilderHandlePost($info_method, $expected_result) {
|
2010-01-30 21:00:41 +00:00
|
|
|
extract($this->setupStorylineBuilderTest());
|
2010-01-30 15:54:23 +00:00
|
|
|
|
|
|
|
$core = new ComicPressTagBuilderFactory($dbi);
|
|
|
|
$core = $core->first_in_1();
|
|
|
|
|
2010-01-30 16:11:07 +00:00
|
|
|
if (is_array($info_method)) {
|
|
|
|
$method = array_shift($info_method);
|
|
|
|
$this->assertEquals($expected_result, call_user_func_array(array($core, $method), $info_method));
|
|
|
|
} else {
|
|
|
|
$this->assertEquals($expected_result, $core->{$info_method}());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function providerTestStorylineBuilderExceptions() {
|
|
|
|
return array(
|
|
|
|
array(array('bad')),
|
|
|
|
array(array(array('in', 1), 'bad')),
|
|
|
|
array(array(array('in', 1), 'date')),
|
2010-02-03 02:12:31 +00:00
|
|
|
array(array(array('from', 'test'))),
|
|
|
|
array(array(array('in'))),
|
|
|
|
array(array(array('from'))),
|
2010-01-30 16:11:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException ComicPressException
|
|
|
|
* @dataProvider providerTestStorylineBuilderExceptions
|
|
|
|
*/
|
|
|
|
function testStorylineBuilderExceptions($calls) {
|
2010-01-30 21:00:41 +00:00
|
|
|
extract($this->setupStorylineBuilderTest(true));
|
2010-01-30 16:11:07 +00:00
|
|
|
|
|
|
|
$core = new ComicPressTagBuilderFactory($dbi);
|
|
|
|
|
|
|
|
foreach ($calls as $call) {
|
|
|
|
if (is_array($call)) {
|
|
|
|
$method = array_shift($call);
|
|
|
|
$core = call_user_func_array(array($core, $method), $call);
|
|
|
|
} else {
|
|
|
|
$core = $core->{$call}();
|
|
|
|
}
|
|
|
|
}
|
2010-01-30 15:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function providerTestMethodParser() {
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'last',
|
|
|
|
array(
|
|
|
|
array('last')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'first_in_3',
|
|
|
|
array(
|
|
|
|
array('in', 3),
|
|
|
|
array('first')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'first_in_category_1',
|
|
|
|
array(
|
|
|
|
array('in', 'category-1'),
|
|
|
|
array('first')
|
|
|
|
)
|
2010-01-30 16:25:49 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'first_permalink_in_category_1',
|
|
|
|
array(
|
|
|
|
array('in', 'category-1'),
|
|
|
|
array('first'),
|
|
|
|
array('permalink'),
|
|
|
|
)
|
|
|
|
),
|
2010-02-03 02:06:39 +00:00
|
|
|
array(
|
|
|
|
'setup_first_post_in_category_1',
|
|
|
|
array(
|
|
|
|
array('in', 'category-1'),
|
|
|
|
array('first'),
|
|
|
|
array('setup')
|
|
|
|
)
|
2010-02-04 01:11:03 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'media_for_first_post_in_category_1',
|
|
|
|
array(
|
|
|
|
array('in', 'category-1'),
|
|
|
|
array('first'),
|
|
|
|
array('media')
|
|
|
|
)
|
2010-02-03 02:06:39 +00:00
|
|
|
)
|
2010-01-30 15:54:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestMethodParser
|
|
|
|
*/
|
|
|
|
function testMethodParser($method_name, $expected_pieces) {
|
|
|
|
$this->assertEquals($expected_pieces, ComicPressTagBuilder::parse_method($method_name));
|
|
|
|
}
|
2010-01-30 21:00:41 +00:00
|
|
|
|
2010-01-31 22:34:59 +00:00
|
|
|
function providerTestMethodParserExceptions() {
|
|
|
|
return array(
|
|
|
|
array('first_in_'),
|
2010-02-03 02:06:39 +00:00
|
|
|
array('first_post_id'),
|
|
|
|
array('setup_setup'),
|
2010-02-04 01:11:03 +00:00
|
|
|
array('setup_first_permalink'),
|
|
|
|
array('setup_media'),
|
|
|
|
array('media_setup'),
|
|
|
|
array('setup_for')
|
2010-01-31 22:34:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestMethodParserExceptions
|
|
|
|
* @expectedException ComicPressException
|
|
|
|
*/
|
|
|
|
function testMethodParserExceptions($method_name) {
|
|
|
|
ComicPressTagBuilder::parse_method($method_name);
|
|
|
|
}
|
|
|
|
|
2010-01-30 21:00:41 +00:00
|
|
|
function testMethodParserWithParam() {
|
|
|
|
extract($this->setupStorylineBuilderTest());
|
|
|
|
|
|
|
|
$core = new ComicPressTagBuilderFactory($dbi);
|
|
|
|
|
|
|
|
$this->assertEquals('2010-01-01', $core->first_date_in_1('Y-m-d'));
|
|
|
|
}
|
2010-02-04 01:00:55 +00:00
|
|
|
|
|
|
|
function testMedia() {
|
|
|
|
$core = $this->getMock('ComicPressTagBuilder', array('_new_comicpresscomicpost'), array(), 'ComicPressTagBuilder_Mock', false);
|
|
|
|
$core->post = 'this-post';
|
|
|
|
|
|
|
|
$comicpresscomicpost = $this->getMock('ComicPressComicPost', array('get_attachments_with_children'));
|
|
|
|
$comicpresscomicpost->expects($this->once())
|
|
|
|
->method('get_attachments_with_children')
|
|
|
|
->with(true)
|
|
|
|
->will($this->returnValue(array('post-media')));
|
|
|
|
|
|
|
|
$core->expects($this->once())
|
|
|
|
->method('_new_comicpresscomicpost')
|
|
|
|
->with('this-post')
|
|
|
|
->will($this->returnValue($comicpresscomicpost));
|
|
|
|
|
|
|
|
$this->assertEquals(array('post-media'), $core->media());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testComicPressComicPost() {
|
|
|
|
$a = ComicPressTagBuilder::_new_comicpresscomicpost('test');
|
|
|
|
$this->assertTrue(is_a($a, 'ComicPressComicPost'));
|
|
|
|
}
|
2010-01-28 01:21:53 +00:00
|
|
|
}
|