GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

HtpasswdServiceTest::testAddUser()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 2
eloc 20
nc 2
nop 0
1
<?php
2
3
namespace HtpasswdManager\Service;
4
5
6
class HtpasswdServiceTest extends \PHPUnit_Framework_TestCase {
7
8
    protected $testDataDir;
9
10
    public function setUp() {
11
        $this->testDataDir = __DIR__ . '/TestData';
12
13
        parent::setUp();
14
    }
15
16
17
    public function testCreateFileIfNotExistant() {
18
        $testFile = $this->testDataDir . '/notExistant';
19
20
        if (true === file_exists($testFile)) {
21
            unlink($testFile);
22
        }
23
24
        $this->assertFileNotExists($testFile);
25
26
        new HtpasswdService($testFile);
27
        $this->assertFileExists($testFile);
28
29
        // Cleanup
30
        unlink($testFile);
31
    }
32
33
    public function testGetUserList_Empty() {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
34
        $testFile = $this->testDataDir . '/htaccess_UserServiceTest_empty';
35
36
        if (true === file_exists($testFile)) {
37
            unlink($testFile);
38
        }
39
        $this->assertFileNotExists($testFile);
40
        touch($testFile);
41
        $this->assertFileExists($testFile);
42
43
        $service = new HtpasswdService($testFile);
44
45
        $userList = $service->getUserList();
46
        $this->assertEquals($expected = [ ], $userList, "UserList");
47
48
        // Cleanup
49
        unlink($testFile);
50
    }
51
52
    public function testGetUserList_Filled() {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
53
        $testFile = $this->testDataDir . '/htaccess_UserServiceTest';
54
        $this->assertFileExists($testFile);
55
56
        $service = new HtpasswdService($testFile);
57
58
        $userList = $service->getUserList();
59
        $this->assertEquals(
60
            $expected = [ 'steven' => '$apr1$UF1l/6iv$tT3IJUH.QL82HAraXetNo0',
61
                          'blub'   => '$apr1$jVosh5nC$q6KJ5EZNU6thOPETMQw8O/'
62
            ], $userList, "UserList");
63
    }
64
65
    public function testAddUser() {
66
        $testFileTemplate = $this->testDataDir . '/htaccess_UserServiceTest';
67
        $testFile         = $this->testDataDir . '/htaccess_UserServiceTest_addUser';
68
        $this->assertFileExists($testFileTemplate);
69
70
        if (true === file_exists($testFile)) {
71
            unlink($testFile);
72
        }
73
        $this->assertFileNotExists($testFile);
74
75
        copy($testFileTemplate, $testFile);
76
        $this->assertFileExists($testFile);
77
78
        $service  = new HtpasswdService($testFile);
79
        $userList = $service->getUserList();
80
        $this->assertCount(2, $userList);
81
        $this->assertEquals(2, $this->getNumberOfLines($testFile), "Number of Lines");
82
83
84
        $service->addUser('test', 'test');
85
86
        $userList = $service->getUserList();
87
        $this->assertCount(3, $userList);
88
        $this->assertEquals($expected = [ 'steven', 'blub', 'test' ],
89
            array_keys($userList));
90
        $this->assertEquals(3, $this->getNumberOfLines($testFile), "Number of Lines");
91
92
93
        // Cleanup
94
        unlink($testFile);
95
    }
96
97
98
    public function testdeleteUser() {
99
        $testFileTemplate = $this->testDataDir . '/htaccess_UserServiceTest';
100
        $testFile         = $this->testDataDir . '/htaccess_UserServiceTest_addUser';
101
        $this->assertFileExists($testFileTemplate);
102
103
        if (true === file_exists($testFile)) {
104
            unlink($testFile);
105
        }
106
        $this->assertFileNotExists($testFile);
107
108
        copy($testFileTemplate, $testFile);
109
        $this->assertFileExists($testFile);
110
111
        $service  = new HtpasswdService($testFile);
112
        $userList = $service->getUserList();
113
        $this->assertCount(2, $userList);
114
        $this->assertEquals(2, $this->getNumberOfLines($testFile), "Number of Lines");
115
116
117
        $service->deleteUser('steven');
118
        $userList = $service->getUserList();
119
        $this->assertCount(1, $userList);
120
        $this->assertEquals($expected = [ 'blub' ],
121
            array_keys($userList));
122
        $this->assertEquals(1, $this->getNumberOfLines($testFile), "Number of Lines");
123
124
125
        // Cleanup
126
        unlink($testFile);
127
    }
128
129
    public function testUserExists() {
130
        $testFile = $this->testDataDir . '/htaccess_UserServiceTest';
131
        $this->assertFileExists($testFile);
132
133
        $service  = new HtpasswdService($testFile);
134
        $service->getUserList();
135
136
        $this->assertTrue($service->userExists('steven'), 'User should exist');
137
        $this->assertTrue($service->userExists('blub'), 'User should exist');
138
        $this->assertFalse($service->userExists(''), "User '' should not exist");
139
        $this->assertFalse($service->userExists('notExistant'), "User 'notExistant' should not exist");
140
        $this->assertFalse($service->userExists('not Existant'), "User 'not Existant' should not exist");
141
    }
142
143
    protected function getNumberOfLines($filename) {
144
        $cont  = file_get_contents($filename);
145
        $lines = explode("\n", $cont);
146
147
        // Remove emtpy lines
148
        foreach ($lines as $i => $line) {
149
            if (empty(trim($line))) {
150
                unset($lines[$i]);
151
            }
152
        }
153
154
        return count($lines);
155
    }
156
157
}