Passed
Push — master ( d309df...7ec489 )
by Petr
02:37
created

FileTest::testCreateFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace SourcesTests\Files\Volume;
4
5
6
use CommonTestClass;
7
use kalanis\kw_auth\AuthException;
8
use kalanis\kw_auth\Data\FileUser;
9
use kalanis\kw_auth\Sources\Files\Volume\File;
10
use kalanis\kw_locks\LockException;
11
12
13
class FileTest extends CommonTestClass
14
{
15
    protected $sourcePath = '';
16
17
    protected function setUp(): void
18
    {
19
        $this->sourcePath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . '.passcomb';
20
    }
21
22
    /**
23
     * @throws AuthException
24
     * @throws LockException
25
     */
26
    public function testDataOnly(): void
27
    {
28
        $lib = $this->fileSources();
29
        $this->assertEmpty($lib->getDataOnly('does not exist'));
30
        $user = $lib->getDataOnly('manager');
31
        $this->assertNotEmpty($user);
32
        $this->assertEquals('Manage', $user->getDisplayName());
33
    }
34
35
    /**
36
     * @throws AuthException
37
     * @throws LockException
38
     */
39
    public function testAuthenticate(): void
40
    {
41
        $lib = $this->fileSources();
42
        $this->assertEmpty($lib->authenticate('manager', ['password' => 'thisisnotreal']));
43
        $user = $lib->authenticate('manager', ['password' => 'valid']);
44
        $this->assertNotEmpty($user);
45
        $this->assertEquals('Manage', $user->getDisplayName());
46
    }
47
48
    /**
49
     * @throws AuthException
50
     * @throws LockException
51
     */
52
    public function testAuthenticateNoPass(): void
53
    {
54
        $lib = $this->fileSources();
55
        $this->expectException(AuthException::class);
56
        $lib->authenticate('manager', []);
57
    }
58
59
    /**
60
     * @throws AuthException
61
     * @throws LockException
62
     */
63
    public function testAccountManipulation(): void
64
    {
65
        $lib = $this->fileSources();
66
        $user = $this->wantedUser();
67
68
        // create
69
        $lib->createAccount($user, 'here to set');
70
        // check data
71
        $saved = $lib->getDataOnly($user->getAuthName());
72
        $this->assertEquals('Testing another', $saved->getDisplayName());
73
        $this->assertEquals('why_here', $saved->getDir());
74
        $this->assertEquals(3, $saved->getClass());
75
76
        // check login
77
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
78
79
        // update
80
        $user->setData(
81
            $user->getAuthId(),
82
            $user->getAuthName(),
83
            $user->getGroup(),
84
            2,
85
            'WheĐn yoĐu dđo nođt knđow',
86
            $user->getDir()
87
        );
88
        $lib->updateAccount($user);
89
90
        // check data - again with new values
91
        $saved = $lib->getDataOnly($user->getAuthName());
92
        $this->assertEquals('When you do not know', $saved->getDisplayName());
93
        $this->assertEquals(2, $saved->getClass());
94
95
        // update password
96
        $lib->updatePassword($user->getAuthName(), 'another pass');
97
        // check login
98
        $this->assertEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
99
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'another pass']));
100
101
        // remove
102
        $lib->deleteAccount($user->getAuthName());
103
        // check for existence
104
        $this->assertEmpty($lib->getDataOnly($user->getAuthName()));
105
    }
106
107
    /**
108
     * @throws AuthException
109
     * @throws LockException
110
     */
111
    public function testCreateFail(): void
112
    {
113
        $lib = $this->fileSources();
114
        $user = $this->wantedUser();
115
        $this->expectException(AuthException::class);
116
        $lib->createAccount($user, '');
117
    }
118
119
    /**
120
     * @throws AuthException
121
     * @throws LockException
122
     */
123
    public function testAllUsers(): void
124
    {
125
        $lib = $this->fileSources();
126
        $data = $lib->readAccounts();
127
        $this->assertEquals(1, $data[0]->getClass());
128
        $this->assertEquals('manager', $data[1]->getAuthName());
129
    }
130
131
    /**
132
     * Contains a full comedy/tragedy of work with locks
133
     * @throws LockException
134
     * @return File
135
     */
136
    protected function fileSources(): File
137
    {
138
        return new File(
139
            new \MockModes(),
140
            $this->getLockPath(),
141
            $this->sourcePath
142
        );
143
    }
144
145
    protected function wantedUser(): FileUser
146
    {
147
        $user = new FileUser();
148
        $user->setData(600, 'another', 0, 0, 'Testing another', 'why_here');
149
        return $user;
150
    }
151
}
152