Passed
Push — master ( 983377...eb5b2a )
by Petr
08:18
created

FileTest::testFreeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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
11
12
class FileTest extends CommonTestClass
13
{
14
    protected function tearDown(): void
15
    {
16
        if (is_file($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra.txt')) {
17
            unlink($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra.txt');
18
        }
19
        if (is_file($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra1.txt')) {
20
            unlink($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra1.txt');
21
        }
22
        if (is_file($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra2.txt')) {
23
            unlink($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra2.txt');
24
        }
25
    }
26
27
    /**
28
     * @throws FilesException
29
     */
30
    public function testRead(): void
31
    {
32
        $lib = $this->getLib();
33
        $this->assertEquals('qwertzuiopasdfghjklyxcvbnm0123456789', $lib->readFile(['dummy2.txt']));
34
        $this->assertEquals('asdfghjklyxcvbnm0123456789', $lib->readFile(['dummy2.txt'], 10));
35
        $this->assertEquals('asdfghjkly', $lib->readFile(['dummy2.txt'], 10, 10));
36
        $this->assertEquals('qwertzuiop', $lib->readFile(['dummy2.txt'], null, 10));
37
    }
38
39
    /**
40
     * @throws FilesException
41
     */
42
    public function testReadNonExist(): void
43
    {
44
        $lib = $this->getLib();
45
        $this->expectException(FilesException::class);
46
        $lib->readFile(['unknown']);
47
    }
48
49
    /**
50
     * @throws FilesException
51
     */
52
    public function testSave(): void
53
    {
54
        $lib = $this->getLib();
55
        $this->assertTrue($lib->saveFile(['extra.txt'], 'qwertzuiopasdfghjklyxcvbnm0123456789'));
56
    }
57
58
    /**
59
     * @throws FilesException
60
     */
61
    public function testSaveNonWritable(): void
62
    {
63
        $lib = $this->getLib();
64
        $this->assertTrue($lib->saveFile(['extra.txt'], 'qwertzuiopasdfghjklyxcvbnm0123456789'));
65
        chmod($this->getTestPath() . DIRECTORY_SEPARATOR . 'extra.txt', 0555);
66
        $this->expectException(FilesException::class);
67
        $lib->saveFile(['extra.txt'], '0123456789qwertzuiopasdfghjklyxcvbnm');
68
    }
69
70
    /**
71
     * @throws FilesException
72
     */
73
    public function testCopyMoveDelete(): void
74
    {
75
        $lib = $this->getLib();
76
        $this->assertTrue($lib->copyFile(['dummy2.txt'], ['extra1.txt']));
77
        $this->assertTrue($lib->moveFile(['extra1.txt'], ['extra2.txt']));
78
        $this->assertTrue($lib->deleteFile(['extra2.txt']));
79
    }
80
81
    protected function getLib(): IProcessFiles
82
    {
83
        return new ProcessFile($this->getTestPath());
84
    }
85
86
    protected function getTestPath(): string
87
    {
88
        return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree';
89
    }
90
}
91