File: Finder.mask.phpt

package info (click to toggle)
php-nette-utils 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: php: 4,219; xml: 9; makefile: 4
file content (239 lines) | stat: -rw-r--r-- 6,605 bytes parent folder | download | duplicates (3)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php

/**
 * Test: Nette\Utils\Finder mask tests.
 */

declare(strict_types=1);

use Nette\Utils\Finder;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


function export($iterator)
{
	$arr = [];
	foreach ($iterator as $key => $value) {
		$arr[] = strtr($key, '\\', '/');
	}

	sort($arr);
	return $arr;
}


test('multiple file masks find matching files in a directory', function () {
	$finder = Finder::findFiles('*.txt', '*.gif')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/file.txt',
		'fixtures.finder/images/logo.gif',
		'fixtures.finder/subdir/file.txt',
		'fixtures.finder/subdir/subdir2/file.txt',
	], export($finder));
});


test('array of file masks returns expected matching files', function () {
	$finder = Finder::findFiles(['*.txt', '*.gif'])->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/file.txt',
		'fixtures.finder/images/logo.gif',
		'fixtures.finder/subdir/file.txt',
		'fixtures.finder/subdir/subdir2/file.txt',
	], export($finder));
});


test('multiple masks in a subdirectory return files and directories', function () {
	$finder = Finder::findFiles('*.txt', '*')->in('fixtures.finder/subdir');
	Assert::same([
		'fixtures.finder/subdir/file.txt',
		'fixtures.finder/subdir/readme',
	], export($finder));
});


test('mask with dot character excludes files without extension', function () {
	$finder = Finder::findFiles('*.*')->in('fixtures.finder/subdir');
	Assert::same([
		'fixtures.finder/subdir/file.txt',
	], export($finder));
});


test('excluding subdirectories by pattern filters out unwanted files', function () {
	$finder = Finder::findFiles('*')->exclude('*i*/*')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/file.txt',
	], export($finder));
});


test('nested wildcard pattern finds files in deeper subdirectories', function () {
	$finder = Finder::findFiles('*/*2/*')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/subdir/subdir2/file.txt',
	], export($finder));
});


test('excluding files by pattern in subdirectory returns remaining entries', function () {
	$finder = Finder::findFiles('*')->exclude('*i*')->in('fixtures.finder/subdir');
	Assert::same([
		'fixtures.finder/subdir/readme',
	], export($finder));
});


test('excluding nested patterns in base directory filters correctly', function () {
	$finder = Finder::findFiles('*')->exclude('*i*/*')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/file.txt',
	], export($finder));
});


test('complex mask with character classes matches the expected file', function () {
	$finder = Finder::findFiles('*2*/fi??.*')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/subdir/subdir2/file.txt',
	], export($finder));
});


test('masks with character classes and escaped brackets match files', function () {
	$finder = Finder::findFiles('*[efd][a-z][!a-r]*')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/images/logo.gif',
	], export($finder));

	$finder = Finder::findFiles('[[]x[]]/fil[e].*')->in('fixtures.finder2');
	Assert::same([
		'fixtures.finder2/[x]/file.txt',
	], export($finder));
});


test('bracket patterns match directory names correctly', function () {
	$finder = Finder::findFiles('[x]/fil[e].*')->in('fixtures.finder2');
	Assert::same([
		'fixtures.finder2/x/file.txt',
	], export($finder));

	$finder = Finder::findFiles('[x]/fil[e].*')->from('fixtures.finder2');
	Assert::same([
		'fixtures.finder2/x/file.txt',
	], export($finder));
});


test('wildcard with bracketed directory pattern matches files', function () {
	$finder = Finder::findFiles('*')->in('fixtures.finder*/[x]');
	Assert::same([
		'fixtures.finder2/[x]/file.txt',
	], export($finder));

	$finder = Finder::findFiles('*')->from('fixtures.finder*/[x]');
	Assert::same([
		'fixtures.finder2/[x]/file.txt',
	], export($finder));
});


test('double asterisk wildcard behaves differently in from() and in()', function () {
	$finder = Finder::findFiles('**/f*')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/file.txt',
		'fixtures.finder/subdir/file.txt',
		'fixtures.finder/subdir/subdir2/file.txt',
	], export($finder));

	$finder = Finder::findFiles('**/f*')->in('fixtures.finder');
	Assert::same([
		'fixtures.finder/file.txt',
	], export($finder));
});


test('relative path masks with "./" prefix work correctly', function () {
	$finder = Finder::findFiles('./f*')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/file.txt',
	], export($finder));

	$finder = Finder::findFiles('./*/f*')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/subdir/file.txt',
	], export($finder));

	$finder = Finder::findFiles('./f*')->in('fixtures.finder');
	Assert::same([
		'fixtures.finder/./file.txt',
	], export($finder));
});


test('parent directory references are handled differently in from() versus in()', function () {
	// not supported
	$finder = Finder::findFiles('../f*')->from('fixtures.finder/subdir');
	Assert::same([], export($finder));

	$finder = Finder::findFiles('../f*')->in('fixtures.finder/subdir');
	Assert::same([
		'fixtures.finder/subdir/../file.txt',
	], export($finder));
});


test('combined relative and recursive wildcards match nested files', function () {
	$finder = Finder::findFiles('./**/f*')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/file.txt',
		'fixtures.finder/subdir/file.txt',
		'fixtures.finder/subdir/subdir2/file.txt',
	], export($finder));
});


test('finder returns directories matching the given pattern', function () {
	$finder = Finder::find('s*/**')->from('fixtures.finder');
	Assert::same([
		'fixtures.finder/subdir/file.txt',
		'fixtures.finder/subdir/readme',
		'fixtures.finder/subdir/subdir2',
		'fixtures.finder/subdir/subdir2/file.txt',
	], export($finder));
});


test('glob pattern for directory names returns different file sets', function () {
	$finder = Finder::findFiles('f*')->in('*.finder');
	Assert::same([
		'fixtures.finder/file.txt',
	], export($finder));

	$finder = Finder::findFiles('f*')->from('*.finder');
	Assert::same([
		'fixtures.finder/file.txt',
		'fixtures.finder/subdir/file.txt',
		'fixtures.finder/subdir/subdir2/file.txt',
	], export($finder));
});


test('recursive glob patterns yield varying results with in() and from()', function () {
	$finder = Finder::findFiles('f*')->in('**/fixtures.finder');
	Assert::same([
		'fixtures.finder/file.txt',
	], export($finder));

	$finder = Finder::findFiles('f*')->from('**/fixtures.finder');
	Assert::same([
		'fixtures.finder/file.txt',
		'fixtures.finder/subdir/file.txt',
		'fixtures.finder/subdir/subdir2/file.txt',
	], export($finder));
});