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