grouping by pattern match

This commit is contained in:
John Bintz 2009-11-24 22:15:34 -05:00
parent eaebe67023
commit e8abf7766f
2 changed files with 61 additions and 19 deletions

View File

@ -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;
}
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;
}
}

View File

@ -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);
}
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));
}
}