1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StorageBasicTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_paths\PathsException; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class FileTest extends AStorageTest |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @throws FilesException |
14
|
|
|
* @throws PathsException |
15
|
|
|
*/ |
16
|
|
|
public function testRead(): void |
17
|
|
|
{ |
18
|
|
|
$lib = $this->getFileLib(); |
19
|
|
|
$this->assertEquals('qwertzuiopasdfghjklyxcvbnm0123456789', $lib->readFile(['dummy2.txt'])); |
20
|
|
|
$this->assertEquals('asdfghjklyxcvbnm0123456789', $lib->readFile(['dummy2.txt'], 10)); |
21
|
|
|
$this->assertEquals('asdfghjkly', $lib->readFile(['dummy2.txt'], 10, 10)); |
22
|
|
|
$this->assertEquals('qwertzuiop', $lib->readFile(['dummy2.txt'], null, 10)); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws FilesException |
27
|
|
|
* @throws PathsException |
28
|
|
|
*/ |
29
|
|
|
public function testReadStream(): void |
30
|
|
|
{ |
31
|
|
|
$lib = $this->getFileLib(); |
32
|
|
|
$this->assertEquals('qwertzuiopasdfghjklyxcvbnm0123456789', $this->streamToString($lib->readFile(['other1.txt']))); |
33
|
|
|
$this->assertEquals('asdfghjklyxcvbnm0123456789', $this->streamToString($lib->readFile(['other1.txt'], 10))); |
34
|
|
|
$this->assertEquals('asdfghjkly', $this->streamToString($lib->readFile(['other1.txt'], 10, 10))); |
35
|
|
|
$this->assertEquals('qwertzuiop', $this->streamToString($lib->readFile(['other1.txt'], null, 10))); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function streamToString($stream): string |
39
|
|
|
{ |
40
|
|
|
rewind($stream); |
41
|
|
|
return stream_get_contents($stream); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws FilesException |
46
|
|
|
* @throws PathsException |
47
|
|
|
*/ |
48
|
|
|
public function testReadNonExist(): void |
49
|
|
|
{ |
50
|
|
|
$lib = $this->getFileLib(); |
51
|
|
|
$this->expectException(FilesException::class); |
52
|
|
|
$lib->readFile(['unknown']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws FilesException |
57
|
|
|
* @throws PathsException |
58
|
|
|
*/ |
59
|
|
|
public function testReadFalse(): void |
60
|
|
|
{ |
61
|
|
|
$lib = $this->getFileLib(); |
62
|
|
|
$this->expectException(FilesException::class); |
63
|
|
|
$lib->readFile(['sub', 'dummy4.txt']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws FilesException |
68
|
|
|
* @throws PathsException |
69
|
|
|
*/ |
70
|
|
|
public function testSave(): void |
71
|
|
|
{ |
72
|
|
|
$lib = $this->getFileLib(); |
73
|
|
|
$this->assertTrue($lib->saveFile(['extra.txt'], 'qwertzuiopasdfghjklyxcvbnm0123456789')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Insert content |
78
|
|
|
* @throws FilesException |
79
|
|
|
* @throws PathsException |
80
|
|
|
*/ |
81
|
|
|
public function testSave2(): void |
82
|
|
|
{ |
83
|
|
|
$lib = $this->getFileLib(); |
84
|
|
|
$this->assertTrue($lib->saveFile(['extra1.txt'], 'qwertzuiopasdfghjklyxcvbnm0123456789')); |
85
|
|
|
$this->assertTrue($lib->saveFile(['extra1.txt'], '0123456789', 15)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create file with moved content |
90
|
|
|
* @throws FilesException |
91
|
|
|
* @throws PathsException |
92
|
|
|
*/ |
93
|
|
|
public function testSave3(): void |
94
|
|
|
{ |
95
|
|
|
$lib = $this->getFileLib(); |
96
|
|
|
$this->assertTrue($lib->saveFile(['extra2.txt'], 'qwertzuiopasdfgh01234567890123456789', 5)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @throws FilesException |
101
|
|
|
* @throws PathsException |
102
|
|
|
*/ |
103
|
|
|
public function testCopyMoveDelete(): void |
104
|
|
|
{ |
105
|
|
|
$lib = $this->getFileLib(); |
106
|
|
|
$this->assertTrue($lib->copyFile(['dummy2.txt'], ['extra1.txt'])); |
107
|
|
|
$this->assertTrue($lib->moveFile(['extra1.txt'], ['extra2.txt'])); |
108
|
|
|
$this->assertTrue($lib->deleteFile(['extra2.txt'])); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|