Passed
Push — master ( 3daa2d...2c2b7e )
by Petr
07:39
created

UserTest::testPathSetterInvalid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace BasicTests;
4
5
6
use CommonTestClass;
7
use InvalidArgumentException;
8
use kalanis\kw_paths\PathsException;
9
use kalanis\kw_user_paths\UserDir;
10
use UnexpectedValueException;
11
12
13
class UserTest extends CommonTestClass
14
{
15
    public function testBasics(): void
16
    {
17
        $lib = new UserDir();
18
        $this->assertEmpty($lib->getHomeDir());
19
        $this->assertEmpty($lib->getDataDir());
20
        $this->assertFalse($lib->wantHomeDir(false)->hasHomeDir());
21
        $this->assertTrue($lib->wantHomeDir(true)->hasHomeDir());
22
        $this->assertFalse($lib->wantDataDir(false)->hasDataDir());
23
        $this->assertTrue($lib->wantDataDir(true)->hasDataDir());
24
    }
25
26
    public function testUser(): void
27
    {
28
        $lib = new UserDir();
29
        $lib->setUserName('dummy');
30
        $this->assertEquals('dummy', $lib->getUserName());
31
    }
32
33
    public function testUserEmpty(): void
34
    {
35
        $lib = new UserDir();
36
        $this->expectException(InvalidArgumentException::class);
37
        $lib->setUserName('');
38
    }
39
40
    public function testUserInvalid(): void
41
    {
42
        $lib = new UserDir();
43
        $this->expectException(InvalidArgumentException::class);
44
        $lib->setUserName('which:me');
45
    }
46
47
    public function testPathSetterClear(): void
48
    {
49
        $lib = new UserDir();
50
        $this->assertTrue($lib->setUserPath(null));
51
    }
52
53
    /**
54
     * @throws PathsException
55
     */
56
    public function testFullPathInvalid(): void
57
    {
58
        $lib = new UserDir();
59
        $this->expectException(PathsException::class);
60
        $lib->getFullPath();
61
    }
62
63
    /**
64
     * @param string $path
65
     * @param bool $useHomeDir
66
     * @param bool $useDataDir
67
     * @dataProvider pathsProvider
68
     */
69
    public function testPaths(string $path, bool $useHomeDir, bool $useDataDir): void
70
    {
71
        $lib = new UserDir();
72
        $lib->setUserPath($path);
73
        $this->assertEquals($path, $lib->getUserPath());
74
        $this->assertEquals($useHomeDir, $lib->hasHomeDir());
75
        $this->assertEquals($useDataDir, $lib->hasDataDir());
76
    }
77
78
    public function pathsProvider(): array
79
    {
80
        return [
81
            ['abc/def/ghi', true, true],
82
            ['/abc/def/ghi/', false, false],
83
            ['/abc/def/ghi', false, true],
84
            ['abc/def/ghi/', true, false],
85
            ['/', false, false],
86
            ['', true, true],
87
        ];
88
    }
89
90
    /**
91
     * @throws PathsException
92
     */
93
    public function testProcessInvalid(): void
94
    {
95
        $lib = new UserDir();
96
        $this->expectException(UnexpectedValueException::class);
97
        $lib->process();
98
    }
99
100
    /**
101
     * @param string $name
102
     * @param bool $fromHomeDir
103
     * @param bool $useSubDir
104
     * @param string $resultPath
105
     * @throws PathsException
106
     * @dataProvider processNamesProvider
107
     */
108
    public function testProcessNames(string $name, bool $fromHomeDir, bool $useSubDir, string $resultPath): void
109
    {
110
        $lib = new UserDir();
111
        $lib->setUserName($name)->wantHomeDir($fromHomeDir)->wantDataDir($useSubDir)->process();
112
        $this->assertEquals($resultPath, $lib->getUserPath());
113
    }
114
115
    public function processNamesProvider(): array
116
    {
117
        return [
118
            ['dummy', true, true, 'dummy'],
119
            ['dummy', false, true, '/dummy'],
120
            ['dummy', false, false, '/dummy/'],
121
            ['dummy', true, false, 'dummy/'],
122
        ];
123
    }
124
125
    public function testPathSetterInvalid(): void
126
    {
127
        $lib = new UserDir();
128
        $this->assertFalse($lib->setUserPath('which:me'));
129
    }
130
}
131