2011-02-27 03:41:54 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Assets\Tests;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Assets\FileFinder;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2011-02-27 03:41:54 +01:00
|
|
|
/**
|
|
|
|
* Tests for the {@link SS_FileFinder} class.
|
|
|
|
*/
|
|
|
|
class FileFinderTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $base;
|
|
|
|
|
|
|
|
public function __construct() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->base = __DIR__ . '/FileFinderTest';
|
2011-02-27 03:41:54 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicOperation() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$this->assertFinderFinds(new FileFinder(), array(
|
2011-02-27 03:41:54 +01:00
|
|
|
'file1.txt',
|
|
|
|
'file2.txt',
|
|
|
|
'dir1/dir1file1.txt',
|
|
|
|
'dir1/dir1file2.txt',
|
|
|
|
'dir1/dir2/dir2file1.txt',
|
|
|
|
'dir1/dir2/dir3/dir3file1.txt'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testInvalidOptionThrowsException() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$finder = new FileFinder();
|
2011-02-27 03:41:54 +01:00
|
|
|
$finder->setOption('this_doesnt_exist', 'ok');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilenameRegex() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$finder = new FileFinder();
|
2011-02-27 03:41:54 +01:00
|
|
|
$finder->setOption('name_regex', '/file2\.txt$/');
|
|
|
|
|
|
|
|
$this->assertFinderFinds(
|
|
|
|
$finder,
|
|
|
|
array(
|
|
|
|
'file2.txt',
|
|
|
|
'dir1/dir1file2.txt'),
|
|
|
|
'The finder only returns files matching the name regex.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIgnoreFiles() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$finder = new FileFinder();
|
2011-02-27 03:41:54 +01:00
|
|
|
$finder->setOption('ignore_files', array('file1.txt', 'dir1file1.txt', 'dir2file1.txt'));
|
|
|
|
|
|
|
|
$this->assertFinderFinds(
|
|
|
|
$finder,
|
|
|
|
array(
|
|
|
|
'file2.txt',
|
|
|
|
'dir1/dir1file2.txt',
|
|
|
|
'dir1/dir2/dir3/dir3file1.txt'),
|
|
|
|
'The finder ignores files with the basename in the ignore_files setting.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIgnoreDirs() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$finder = new FileFinder();
|
2011-02-27 03:41:54 +01:00
|
|
|
$finder->setOption('ignore_dirs', array('dir2'));
|
|
|
|
|
|
|
|
$this->assertFinderFinds(
|
|
|
|
$finder,
|
|
|
|
array(
|
|
|
|
'file1.txt',
|
|
|
|
'file2.txt',
|
|
|
|
'dir1/dir1file1.txt',
|
|
|
|
'dir1/dir1file2.txt'),
|
|
|
|
'The finder ignores directories in ignore_dirs.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMinDepth() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$finder = new FileFinder();
|
2011-02-27 03:41:54 +01:00
|
|
|
$finder->setOption('min_depth', 2);
|
|
|
|
|
|
|
|
$this->assertFinderFinds(
|
|
|
|
$finder,
|
|
|
|
array(
|
|
|
|
'dir1/dir2/dir2file1.txt',
|
|
|
|
'dir1/dir2/dir3/dir3file1.txt'),
|
|
|
|
'The finder respects the min depth setting.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMaxDepth() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$finder = new FileFinder();
|
2011-02-27 03:41:54 +01:00
|
|
|
$finder->setOption('max_depth', 1);
|
|
|
|
|
|
|
|
$this->assertFinderFinds(
|
|
|
|
$finder,
|
|
|
|
array(
|
|
|
|
'file1.txt',
|
|
|
|
'file2.txt',
|
|
|
|
'dir1/dir1file1.txt',
|
|
|
|
'dir1/dir1file2.txt'),
|
|
|
|
'The finder respects the max depth setting.');
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
public function assertFinderFinds(FileFinder $finder, $expect, $message = null) {
|
2011-02-27 03:41:54 +01:00
|
|
|
$found = $finder->find($this->base);
|
|
|
|
|
|
|
|
foreach ($expect as $k => $file) {
|
|
|
|
$expect[$k] = "{$this->base}/$file";
|
|
|
|
}
|
|
|
|
|
|
|
|
sort($expect);
|
|
|
|
sort($found);
|
|
|
|
|
|
|
|
$this->assertEquals($expect, $found, $message);
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|