1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
<?php
/**
* Test: Nette\Utils\Finder basic usage.
*/
declare(strict_types=1);
use Nette\Utils\FileSystem;
use Nette\Utils\Finder;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
function export($iterator, bool $sort = true)
{
$arr = [];
foreach ($iterator as $key => $value) {
$arr[] = FileSystem::unixSlashes($key);
}
if ($sort) {
sort($arr);
}
return $arr;
}
test('empty search', function () {
$finder = (new Finder)->in('fixtures.finder');
Assert::same([], export($finder));
$finder = (new Finder)->from('fixtures.finder');
Assert::same([], export($finder));
Assert::exception(
fn() => Finder::findFiles(''),
Nette\InvalidArgumentException::class,
);
});
test('default mask', function () {
$finder = Finder::find()->in('fixtures.finder');
Assert::same(['fixtures.finder/file.txt', 'fixtures.finder/images', 'fixtures.finder/subdir'], export($finder));
$finder = Finder::findFiles()->in('fixtures.finder');
Assert::same(['fixtures.finder/file.txt'], export($finder));
$finder = Finder::findDirectories()->in('fixtures.finder');
Assert::same(['fixtures.finder/images', 'fixtures.finder/subdir'], export($finder));
$finder = (new Finder)->files()->in('fixtures.finder');
Assert::same(['fixtures.finder/file.txt'], export($finder));
$finder = (new Finder)->directories()->in('fixtures.finder');
Assert::same(['fixtures.finder/images', 'fixtures.finder/subdir'], export($finder));
});
test('current dir', function () {
$finder = Finder::findFiles('fixtures.finder/*.txt');
Assert::same(['fixtures.finder/file.txt'], export($finder));
});
test('non-recursive file search', function () {
$finder = Finder::findFiles('file.txt')->in('fixtures.finder');
Assert::same(['fixtures.finder/file.txt'], export($finder));
});
test('non-recursive file search alt', function () {
$finder = (new Finder)->files('file.txt')->in('fixtures.finder');
Assert::same(['fixtures.finder/file.txt'], export($finder));
});
test('recursive file search', function () {
$finder = Finder::findFiles('file.txt')->from('fixtures.finder');
Assert::same([
'fixtures.finder/file.txt',
'fixtures.finder/subdir/file.txt',
'fixtures.finder/subdir/subdir2/file.txt',
], export($finder));
});
test('recursive file search with depth limit', function () {
$finder = Finder::findFiles('file.txt')->from('fixtures.finder')->limitDepth(1);
Assert::same([
'fixtures.finder/file.txt',
'fixtures.finder/subdir/file.txt',
], export($finder));
});
test('non-recursive file & directory search', function () {
$finder = Finder::find('file.txt')->in('fixtures.finder');
Assert::same([
'fixtures.finder/file.txt',
], export($finder));
});
test('recursive file & directory search', function () {
$finder = Finder::find('file.txt')->from('fixtures.finder');
Assert::same([
'fixtures.finder/file.txt',
'fixtures.finder/subdir/file.txt',
'fixtures.finder/subdir/subdir2/file.txt',
], export($finder));
});
test('recursive file & directory search in child-first order', function () {
$finder = Finder::find('subdir*')->from('fixtures.finder')->childFirst();
Assert::same([
'fixtures.finder/subdir/subdir2',
'fixtures.finder/subdir',
], export($finder, sort: false));
});
test('recursive file & directory search excluding folders', function () {
$finder = Finder::find('file.txt')->from('fixtures.finder')->exclude('images')->exclude('subdir2');
Assert::same([
'fixtures.finder/file.txt',
'fixtures.finder/subdir/file.txt',
], export($finder));
});
test('non-recursive directory search', function () {
$finder = Finder::findDirectories('subdir*')->in('fixtures.finder');
Assert::same([
'fixtures.finder/subdir',
], export($finder));
});
test('non-recursive directory search alt', function () {
$finder = (new Finder)->directories('subdir*')->in('fixtures.finder');
Assert::same([
'fixtures.finder/subdir',
], export($finder));
});
test('recursive directory search', function () {
$finder = Finder::findDirectories('subdir*')->from('fixtures.finder');
Assert::same([
'fixtures.finder/subdir',
'fixtures.finder/subdir/subdir2',
], export($finder));
});
test('absolute path', function () {
$finder = Finder::find('fixtures.finder/im*')->in(__DIR__);
Assert::same([
FileSystem::unixSlashes(__DIR__) . '/fixtures.finder/images',
], export($finder));
});
test('absolute path in mask', function () { // will not work if there are characters [] in the path!!!
$finder = Finder::findDirectories(__DIR__);
Assert::same([
FileSystem::unixSlashes(__DIR__),
], export($finder));
});
test('symlink to file', function () {
$finder = Finder::find('subdir/*.txt')->in('fixtures.finder3');
Assert::same([
'fixtures.finder3/subdir/file.txt',
], export($finder));
});
test('symlink to directory', function () {
$finder = Finder::findDirectories()->in('fixtures.finder3/another_subdir');
Assert::same([
'fixtures.finder3/another_subdir/subdir',
], export($finder));
});
test('symlink to file in symlinked directory', function () {
$finder = Finder::find('subdir/*.txt')->in('fixtures.finder3/another_subdir');
Assert::same([
'fixtures.finder3/another_subdir/subdir/file.txt',
], export($finder));
});
|