Passed
Push — master ( 1f33ad...fba3db )
by Petr
08:38
created

FileTest::testSave2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace VolumeTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_files\Interfaces\IProcessFiles;
9
use kalanis\kw_files\Processing\Volume\ProcessFile;
10
use kalanis\kw_paths\PathsException;
11
12
13
class FileTest extends CommonTestClass
14
{
15
    protected function tearDown(): void
16
    {
17
        if (is_file($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra.txt')) {
18
            unlink($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra.txt');
19
        }
20
        if (is_file($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra1.txt')) {
21
            unlink($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra1.txt');
22
        }
23
        if (is_file($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra2.txt')) {
24
            unlink($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra2.txt');
25
        }
26
    }
27
28
    /**
29
     * @throws FilesException
30
     * @throws PathsException
31
     */
32
    public function testRead(): void
33
    {
34
        $lib = $this->getLib();
35
        $this->assertEquals('qwertzuiopasdfghjklyxcvbnm0123456789', $lib->readFile(['dummy2.txt']));
36
        $this->assertEquals('asdfghjklyxcvbnm0123456789', $lib->readFile(['dummy2.txt'], 10));
37
        $this->assertEquals('asdfghjkly', $lib->readFile(['dummy2.txt'], 10, 10));
38
        $this->assertEquals('qwertzuiop', $lib->readFile(['dummy2.txt'], null, 10));
39
    }
40
41
    /**
42
     * @throws FilesException
43
     * @throws PathsException
44
     */
45
    public function testReadNonExist(): void
46
    {
47
        $lib = $this->getLib();
48
        $this->expectException(FilesException::class);
49
        $lib->readFile(['unknown']);
50
    }
51
52
    /**
53
     * @throws FilesException
54
     * @throws PathsException
55
     */
56
    public function testSave(): void
57
    {
58
        $lib = $this->getLib();
59
        $this->assertTrue($lib->saveFile(['extra.txt'], 'qwertzuiopasdfghjklyxcvbnm0123456789'));
60
    }
61
62
    /**
63
     * Insert content
64
     * @throws FilesException
65
     * @throws PathsException
66
     */
67
    public function testSave2(): void
68
    {
69
        $lib = $this->getLib();
70
        $this->assertTrue($lib->saveFile(['extra1.txt'], 'qwertzuiopasdfghjklyxcvbnm0123456789'));
71
        $this->assertTrue($lib->saveFile(['extra1.txt'], '0123456789', 15));
72
    }
73
74
    /**
75
     * Create file with moved content
76
     * @throws FilesException
77
     * @throws PathsException
78
     */
79
    public function testSave3(): void
80
    {
81
        $lib = $this->getLib();
82
        $this->assertTrue($lib->saveFile(['extra2.txt'], 'qwertzuiopasdfgh01234567890123456789', 5));
83
    }
84
85
    /**
86
     * @throws FilesException
87
     * @throws PathsException
88
     */
89
    public function testSaveNonWritable(): void
90
    {
91
        $lib = $this->getLib();
92
        $this->assertTrue($lib->saveFile(['extra.txt'], 'qwertzuiopasdfghjklyxcvbnm0123456789'));
93
        chmod($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra.txt', 0555);
94
        $this->expectException(FilesException::class);
95
        $lib->saveFile(['extra.txt'], '0123456789qwertzuiopasdfghjklyxcvbnm');
96
    }
97
98
    /**
99
     * @throws FilesException
100
     * @throws PathsException
101
     */
102
    public function testCopyMoveDelete(): void
103
    {
104
        $lib = $this->getLib();
105
        $this->assertTrue($lib->copyFile(['dummy2.txt'], ['extra1.txt']));
106
        $this->assertTrue($lib->moveFile(['extra1.txt'], ['extra2.txt']));
107
        $this->assertTrue($lib->deleteFile(['extra2.txt']));
108
    }
109
110
    protected function getLib(): IProcessFiles
111
    {
112
        return new ProcessFile($this->getTestPath());
113
    }
114
115
    protected function getTestPath(): string
116
    {
117
        return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree';
118
    }
119
}
120