find matchign files

This commit is contained in:
John Bintz 2009-11-24 19:19:53 -05:00
parent 7afe5f7f83
commit b0b01a5177
2 changed files with 48 additions and 0 deletions

View File

@ -80,4 +80,25 @@ class ComicPressBackendFilesystem extends ComicPressBackend {
}
return false;
}
function find_matching_files($patterns) {
foreach ($patterns as $pattern) {
$dir = dirname($pattern);
if (is_dir($dir)) {
$pattern = str_replace('*', '.*', basename($pattern));
if (($dh = opendir($dir)) !== false) {
while (($file = readdir($dh)) !== false) {
$target = $dir . '/' . $file;
if (is_file($target) || is_link($target)) {
if (preg_match('#' . $pattern. '#', $file) > 0) {
return $target;
}
}
}
closedir($dh);
}
}
}
return false;
}
}

View File

@ -9,6 +9,9 @@ class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$this->fs = new ComicPressBackendFilesystem();
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
}
function providerTestProcessSearchString() {
@ -60,4 +63,28 @@ class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_searches, $fs->process_search_string($posts[$post_id_to_use], 'comic'));
}
function providerTestFindMatchingFiles() {
return array(
array(array('/blah'), false),
array(array('/comic/2008-01-01.jpg'), false),
array(array('/comic/2009-01-01.jpg'), vfsStream::url('root/comic/2009-01-01.jpg')),
array(array('/comic/2009-01-01-test.jpg'), vfsStream::url('root/comic/2009-01-01-test.jpg')),
);
}
/**
* @dataProvider providerTestFindMatchingFiles
*/
function testFindMatchingFiles($filesystem_layout, $expected_match) {
foreach ($filesystem_layout as $file) {
$parts = pathinfo($file);
mkdir(vfsStream::url("root{$parts['dirname']}"), 0666, true);
file_put_contents(vfsStream::url("root${file}"), 'test');
}
wp_set_post_categories(1, array(2));
$this->assertEquals($expected_match, $this->fs->find_matching_files(array(vfsStream::url('root/comic/2009-01-01*.jpg'))));
}
}