From e8abf7766f7229c6220992fd96a7e9054d60c6d0 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 24 Nov 2009 22:15:34 -0500 Subject: [PATCH] grouping by pattern match --- .../backends/ComicPressBackendFilesystem.inc | 36 ++++++++++++--- .../ComicPressBackendFilesystemTest.php | 44 ++++++++++++++----- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/classes/backends/ComicPressBackendFilesystem.inc b/classes/backends/ComicPressBackendFilesystem.inc index d2922ee..6747f21 100644 --- a/classes/backends/ComicPressBackendFilesystem.inc +++ b/classes/backends/ComicPressBackendFilesystem.inc @@ -4,7 +4,7 @@ require_once(dirname(__file__) . '/../ComicPressBackend.inc'); class ComicPressBackendFilesystem extends ComicPressBackend { var $search_string = ''; - var $id, $type, $filepath; + var $id, $files_by_type = array(); function process_search_string($post, $type) { $this->_searches = array($this->search_string); @@ -116,21 +116,43 @@ class ComicPressBackendFilesystem extends ComicPressBackend { } if (isset($comicpress->comicpress_options['image_types'])) { + $files = array(); foreach (array_keys($comicpress->comicpress_options['image_types']) as $type) { $patterns = $this->process_search_string($post, $type); if (!empty($patterns)) { $result = $this->find_matching_files($patterns); if (!empty($result)) { - $fs = new ComicPressBackendFilesystem(); - $fs->id = 'filesystem-' . $post->ID . '-' . md5($result); - $fs->type = $type; - $fs->filepath = $result; - $return[] = $fs; + $files[$type] = $result; } } } + if (!empty($files)) { + $fs = new ComicPressBackendFilesystem(); + $fs->id = 'filesystem-' . $post->ID; + $fs->files_by_type = $files; + $return[] = $fs; + } } return $return; } -} \ No newline at end of file + + function group_by_root($filename_pattern, $all_files) { + $roots = array(); + $filename_pattern = str_replace('*', '(.*)', basename($filename_pattern)); + + foreach ($all_files as $type => $files) { + foreach ($files as $file) { + $filename = basename($file); + if (preg_match('#^' . $filename_pattern . '$#', $filename, $matches) > 0) { + $filename = $matches[1]; + } + + if (!isset($roots[$filename])) { $roots[$filename] = array(); } + $roots[$filename][$type] = $file; + } + } + + return $roots; + } +} diff --git a/test/backends/ComicPressBackendFilesystemTest.php b/test/backends/ComicPressBackendFilesystemTest.php index 4f998a8..f95457f 100644 --- a/test/backends/ComicPressBackendFilesystemTest.php +++ b/test/backends/ComicPressBackendFilesystemTest.php @@ -89,10 +89,6 @@ class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_match, $this->fs->find_matching_files(array(vfsStream::url('root/comic/2009-01-01*.jpg')))); } - function testUpdateFilesystemPostMeta() { - $this->markTestIncomplete(); - } - function testGenerateFromPost() { $post = (object)array('ID' => 1); @@ -113,12 +109,36 @@ class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase { $return = $fs->generate_from_post($post); - $this->assertEquals(2, count($return)); - $this->assertEquals('filesystem-1-' . md5('comic'), $return[0]->id); - $this->assertEquals('filesystem-1-' . md5('rss'), $return[1]->id); - $this->assertEquals('comic', $return[0]->type); - $this->assertEquals('rss', $return[1]->type); - $this->assertEquals('comic', $return[0]->filepath); - $this->assertEquals('rss', $return[1]->filepath); + $this->assertEquals(1, count($return)); + $this->assertEquals('filesystem-1', $return[0]->id); + $this->assertEquals(array( + 'comic' => 'comic', + 'rss' => 'rss' + ), $return[0]->files_by_type); } -} \ No newline at end of file + + function providerTestGroupByRoot() { + return array( + array( + 'test*.jpg', + array('comic' => array('/test/test1.jpg', '/test/test2.jpg')), + array('1' => array('comic' => '/test/test1.jpg'), '2' => array('comic' => '/test/test2.jpg')) + ), + array( + '2009-01-01*.jpg', + array( + 'comic' => array('/comic/2009-01-01-01-yeah.jpg'), + 'rss' => array('/rss/2009-01-01-01-yeah.jpg') + ), + array('-01-yeah' => array('comic' => '/comic/2009-01-01-01-yeah.jpg', 'rss' => '/rss/2009-01-01-01-yeah.jpg')) + ), + ); + } + + /** + * @dataProvider providerTestGroupByRoot + */ + function testGroupByRoot($pattern, $files, $expected_groupings) { + $this->assertEquals($expected_groupings, $this->fs->group_by_root($pattern, $files)); + } +}