From eb840b1d696f34d30ef57784154cdb7fe4aa6c6f Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 17 Jan 2010 21:28:26 -0500 Subject: [PATCH] code coverage --- classes/FixtureBuilder.inc | 5 ++--- classes/PostFixtures.inc | 41 +++++++++++++++++++++++++++++++++++++ test/FixtureBuilderTest.php | 17 +++++++++++++-- test/PostFixturesTest.php | 27 ++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 5 deletions(-) diff --git a/classes/FixtureBuilder.inc b/classes/FixtureBuilder.inc index 4f21668..1883689 100644 --- a/classes/FixtureBuilder.inc +++ b/classes/FixtureBuilder.inc @@ -83,9 +83,6 @@ class FixtureBuilder { 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; } @@ -127,6 +124,8 @@ class FixtureBuilder { return get_post($post_id); } + // @codeCoverageIgnoreStart return false; + // @codeCoverageIgnoreEnd } } diff --git a/classes/PostFixtures.inc b/classes/PostFixtures.inc index 00c7a21..36d5473 100644 --- a/classes/PostFixtures.inc +++ b/classes/PostFixtures.inc @@ -101,10 +101,51 @@ class PostFixtures { /** * Render the admin page. */ + // @codeCoverageIgnoreStart function render_admin() { + + include(dirname(__FILE__) . '/partials/admin.inc'); } + function wpcontent_path() { return ABSPATH . '/wp-content'; } + // @codeCoverageIgnoreEnd + + function find_fixtures() { + $this->fixtures = array(); + + $this->_find_fixtures_recurse($this->wpcontent_path()); + + return $this->fixtures; + } + + function _find_fixtures_recurse($dir) { + $queue = array(); + $dh = opendir($dir); + $is_fixture_dir = (basename($dir) === 'fixtures'); + while ($file = readdir($dh)) { + if ($file[0] != '.') { + $target = $dir . '/' . $file; + if ($is_fixture_dir) { + if (is_file($target)) { + if (preg_match('#\.(inc|json)$#', $target) > 0) { + $this->fixtures[] = $target; + } + } + } else { + if (is_dir($target)) { + $queue[] = $target; + } + } + } + } + closedir($dh); + + foreach ($queue as $dir) { + $this->_find_fixtures_recurse($dir); + } + } + // data handling /** diff --git a/test/FixtureBuilderTest.php b/test/FixtureBuilderTest.php index dd0e10a..a4ab3a5 100644 --- a/test/FixtureBuilderTest.php +++ b/test/FixtureBuilderTest.php @@ -47,6 +47,8 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase { array('date', array('2010-01-01')), array('categories', array('test,test2')), array('metadata', array('key', array('metadata' => 'value'))), + array('post', array('Post title 2')), + array('date', array('2010-01-02')), ), array( 'post' => array( @@ -56,10 +58,15 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase { 'post_date' => '2010-01-01', 'categories' => array('test', 'test2'), 'metadata' => array('key' => array('metadata' => 'value')) - ) + ), + array( + 'post_title' => 'Post title 2', + 'post_type' => 'post', + 'post_date' => '2010-01-02', + ), ), ), - array(true, true, true, true) + array(true, true, true, true, true, true) ), ); } @@ -191,4 +198,10 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase { } } } + + function testBuildEmpty() { + $builder = new FixtureBuilder(); + + $builder->build(); + } } diff --git a/test/PostFixturesTest.php b/test/PostFixturesTest.php index 77fef5f..92e5d2b 100644 --- a/test/PostFixturesTest.php +++ b/test/PostFixturesTest.php @@ -2,6 +2,7 @@ require_once('PHPUnit/Framework.php'); require_once('MockPress/mockpress.php'); +require_once('vfsStream/vfsStream.php'); require_once(dirname(__FILE__) . '/../classes/PostFixtures.inc'); class PostFixturesTest extends PHPUnit_Framework_TestCase { @@ -10,6 +11,9 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase { $this->pf = new PostFixtures(); $_POST = array(); + + vfsStreamWrapper::register(); + vfsStreamWrapper::setRoot(new vfsStreamDirectory('root')); } function tearDown() { @@ -330,4 +334,27 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase { $this->assertTrue(!isset($_POST['pf'])); } + + function testRecurseDirectories() { + mkdir(vfsStream::url('root/dir/fixtures'), 0666, true); + + foreach (array( + 'dir/test.json', + 'dir/test.inc', + 'dir/fixtures/test', + 'dir/fixtures/test.json', + 'dir/fixtures/test.inc', + ) as $path) { + $path = vfsStream::url("root/${path}"); + file_put_contents($path, $path); + } + + $pf = $this->getMock('PostFixtures', array('wpcontent_path')); + $pf->expects($this->once())->method('wpcontent_path')->will($this->returnValue(vfsStream::url('root'))); + + $this->assertEquals(array( + vfsStream::url('root/dir/fixtures/test.json'), + vfsStream::url('root/dir/fixtures/test.inc'), + ), $pf->find_fixtures()); + } }