1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProcessingTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use CommonTestClass; |
7
|
|
|
use kalanis\kw_files\Extended\FindFreeName; |
8
|
|
|
use kalanis\kw_files\FilesException; |
9
|
|
|
use kalanis\kw_files\Interfaces\IProcessNodes; |
10
|
|
|
use kalanis\kw_files\Processing\TPathTransform; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class FinderTest extends CommonTestClass |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param string[] $known |
17
|
|
|
* @param string[] $path |
18
|
|
|
* @param string $name |
19
|
|
|
* @param string $result |
20
|
|
|
* @throws FilesException |
21
|
|
|
* @dataProvider finder1Provider |
22
|
|
|
*/ |
23
|
|
|
public function testCompactFrom1(array $known, array $path, string $name, string $result): void |
24
|
|
|
{ |
25
|
|
|
$lib = new FindFreeName(new XNameFinder($known)); |
26
|
|
|
$this->assertEquals($result, $lib->findFreeName($path, $name, '')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function finder1Provider(): array |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
|
|
// basic one - not exists, no change |
33
|
|
|
[['wsx', 'edc', 'rfv', ], [], 'uhb', 'uhb', ], |
34
|
|
|
// basic one - somewhere in subdir, no change need |
35
|
|
|
[['wsx', 'edc', 'rfv', ], ['edc', ], 'rfv', 'rfv', ], |
36
|
|
|
// a bit complicated one - somewhere in subdir, change need |
37
|
|
|
[['edc' . DIRECTORY_SEPARATOR . 'wsx', 'edc' . DIRECTORY_SEPARATOR . 'edc', 'edc' . DIRECTORY_SEPARATOR . 'rfv', ], ['edc', ], 'rfv', 'rfv_0', ], |
38
|
|
|
// already exists - simple |
39
|
|
|
[['okmijnuhb', ], [], 'okmijnuhb', 'okmijnuhb_0', ], |
40
|
|
|
// already exists - more examples |
41
|
|
|
[['wsx', 'wsx_0', 'wsx_1', ], [], 'wsx', 'wsx_2', ], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string[] $known |
47
|
|
|
* @param string[] $path |
48
|
|
|
* @param string $name |
49
|
|
|
* @param string $result |
50
|
|
|
* @throws FilesException |
51
|
|
|
* @dataProvider finder2Provider |
52
|
|
|
*/ |
53
|
|
|
public function testCompactFrom2(array $known, array $path, string $name, string $result): void |
54
|
|
|
{ |
55
|
|
|
$lib = new XFindFreeName(new XNameFinder($known)); |
56
|
|
|
$this->assertEquals($result, $lib->findFreeName($path, $name, '')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function finder2Provider(): array |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
// basic one - not exists, no change |
63
|
|
|
[['wsx', 'edc', 'rfv', ], [], 'uhb', 'uhb', ], |
64
|
|
|
// basic one - somewhere in subdir, no change need |
65
|
|
|
[['wsx', 'edc', 'rfv', ], ['edc', ], 'rfv', 'rfv', ], |
66
|
|
|
// a bit complicated one - somewhere in subdir, change need |
67
|
|
|
[['edc' . DIRECTORY_SEPARATOR . 'wsx', 'edc' . DIRECTORY_SEPARATOR . 'edc', 'edc' . DIRECTORY_SEPARATOR . 'rfv', ], ['edc', ], 'rfv', 'rfv--#0', ], |
68
|
|
|
// already exists - simple |
69
|
|
|
[['okmijnuhb', ], [], 'okmijnuhb', 'okmijnuhb--#0', ], |
70
|
|
|
// already exists - more examples |
71
|
|
|
[['wsx', 'wsx--#0', 'wsx--#1', ], [], 'wsx', 'wsx--#2', ], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
class XFindFreeName extends FindFreeName |
78
|
|
|
{ |
79
|
|
|
protected function getNameSeparator(): string |
80
|
|
|
{ |
81
|
|
|
return '--#'; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
class XNameFinder implements IProcessNodes |
87
|
|
|
{ |
88
|
|
|
use TPathTransform; |
89
|
|
|
|
90
|
|
|
protected $knownNames; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array<string> $knownNames |
94
|
|
|
*/ |
95
|
|
|
public function __construct(array $knownNames) |
96
|
|
|
{ |
97
|
|
|
$this->knownNames = $knownNames; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function exists(array $entry): bool |
101
|
|
|
{ |
102
|
|
|
return in_array($this->compactName($entry), $this->knownNames); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function isReadable(array $entry): bool |
106
|
|
|
{ |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function isWritable(array $entry): bool |
111
|
|
|
{ |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function isDir(array $entry): bool |
116
|
|
|
{ |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function isFile(array $entry): bool |
121
|
|
|
{ |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function size(array $entry): ?int |
126
|
|
|
{ |
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function created(array $entry): ?int |
131
|
|
|
{ |
132
|
|
|
return null; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|