From 1cafcc9e3b852d38d1d2b2f5a4e82f713c9f6840 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 15 Nov 2009 10:18:42 -0500 Subject: [PATCH] setting up functions tests and cleaning up code coverage --- functions.php | 26 +++++++++++----- test/FunctionsTest.php | 70 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/functions.php b/functions.php index e4eb916..a2ce588 100644 --- a/functions.php +++ b/functions.php @@ -4,6 +4,8 @@ add_action('init', '__comicpress_init'); add_action('template_redirect', '__comicpress_template_redirect', 100); +// @codeCoverageIgnoreStart + function __comicpress_init() { global $comicpress, $wp_query; @@ -57,6 +59,8 @@ function finish_comicpress() { include(F('application.php', '')); } +// @codeCoverageIgnoreEnd + /** * Protect global $post and $wp_query. */ @@ -89,12 +93,7 @@ function Unprotect() { $__post = $__wp_query = null; } -function R($which, $restrictions = null, $override_post = null) { - global $post; - $post_to_use = !is_null($override_post) ? $override_post : $post; - - $storyline = new ComicPressStoryline(); - +function __prep_R($restrictions, $post_to_use) { if (is_string($restrictions)) { switch ($restrictions) { case 'from_post': @@ -119,7 +118,18 @@ function R($which, $restrictions = null, $override_post = null) { $restrictions = $new_restrictions; } - $categories = $storyline->build_from_restrictions($restrictions); + return $restrictions; +} + +// @codeCoverageIgnoreStart + +function R($which, $restrictions = null, $override_post = null) { + global $post; + $post_to_use = !is_null($override_post) ? $override_post : $post; + + $storyline = new ComicPressStoryline(); + + $categories = $storyline->build_from_restrictions(__prep_R($restrictions)); $dbi = ComicPressDBInterface::get_instance(); @@ -150,6 +160,8 @@ function RT($which, $restrictions = null, $override_post = null) { return $post; } +// @codeCoverageIgnoreEnd + function M($override_post = null) { global $post, $__attachments; $post_to_use = !is_null($override_post) ? $override_post : $post; diff --git a/test/FunctionsTest.php b/test/FunctionsTest.php index 358746a..564d231 100644 --- a/test/FunctionsTest.php +++ b/test/FunctionsTest.php @@ -40,4 +40,74 @@ class FunctionsTest extends PHPUnit_Framework_TestCase { 'test-1' => array('enabled' => true, 'children' => array('rss' => 'test-2')) ), get_post_meta($post_to_test->ID, 'image-ordering', true)); } + + function testProtect() { + global $post, $wp_query, $__post, $__wp_query; + + $__post = null; + $__wp_query = null; + + $post = "test"; + $wp_query = "test2"; + + Protect(); + + $this->assertEquals($post, $__post); + $this->assertEquals($wp_query, $__wp_query); + } + + function testRestore() { + global $post, $__post; + + $post = 'not'; + $__post = 'test'; + + Restore(); + + $this->assertEquals($__post, $post); + } + + function testUnprotect() { + global $post, $__post, $wp_query, $__wp_query; + + $__post = $__wp_query = 'test'; + $post = $wp_query = 'not'; + + Unprotect(); + + $this->assertEquals('test', $post); + $this->assertEquals('test', $wp_query); + + $this->assertTrue(is_null($__post)); + $this->assertTrue(is_null($__wp_query)); + } + + function providerTestPrepR() { + $post = (object)array('ID' => 1); + + return array( + array( + array(), array() + ), + array( + 'from_post', array('from_post' => $post) + ), + array( + array('test' => 'test'), array('test' => 'test') + ), + array( + array('test' => '__post'), array('test' => $post) + ), + array( + array('test' => array('test')), array('test' => array('test')) + ), + ); + } + + /** + * @dataProvider providerTestPrepR + */ + function testPrepR($restrictions, $expected_result) { + $this->assertEquals($expected_result, __prep_R($restrictions, (object)array('ID' => 1))); + } }