MySQLUserFactoryTest::testItDropWithDefaultHost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Database\Tests\Unit\Factories\User;
11
12
use FlexPHP\Database\Exception\UserDatabaseException;
13
use FlexPHP\Database\Factories\User\SQLSrvUserFactory;
14
use FlexPHP\Database\Tests\TestCase;
15
use FlexPHP\Database\User;
16
17
class MySQLUserFactoryTest extends TestCase
18
{
19
    /**
20
     * @dataProvider getNameInvalid
21
     *
22
     * @param mixed $name
23
     */
24
    public function testItCreateWithNameInvalidThrownException($name): void
25
    {
26
        $this->expectException(UserDatabaseException::class);
27
        (new User($name, 'password'))->toSqlCreate();
28
    }
29
30
    public function testItCreateWithDefaultHost(): void
31
    {
32
        $name = 'jon';
33
        $password = 'p4sw00rd';
34
35
        $user = new User($name, $password);
36
        $this->assertEquals(<<<T
37
CREATE USER '$name'@'%' IDENTIFIED BY '$password';
38
T
39
, $user->toSqlCreate());
40
    }
41
42
    public function testItUserCreateWithCustomHost(): void
43
    {
44
        $name = 'jon';
45
        $password = 'p4sw00rd';
46
        $host = 'custom.host';
47
48
        $user = new User($name, $password, $host);
49
        $this->assertEquals(<<<T
50
CREATE USER '$name'@'$host' IDENTIFIED BY '$password';
51
T
52
, $user->toSqlCreate());
53
    }
54
55
    /**
56
     * @dataProvider getHostDefault
57
     *
58
     * @param mixed $host
59
     */
60
    public function testItDropWithDefaultHost($host): void
61
    {
62
        $name = 'jon';
63
        $password = 'p4sw00rd';
64
65
        $user = new User($name, $password, $host);
66
        $this->assertEquals(<<<T
67
DROP USER '$name'@'%';
68
T
69
, $user->toSqlDrop());
70
    }
71
72
    public function testItDropWithCustomHost(): void
73
    {
74
        $name = 'jon';
75
        $password = 'p4sw00rd';
76
        $host = 'custom.host';
77
78
        $user = new User($name, $password, $host);
79
        $this->assertEquals(<<<T
80
DROP USER '$name'@'$host';
81
T
82
, $user->toSqlDrop());
83
    }
84
85
    /**
86
     * @dataProvider getPermissionValid
87
     *
88
     * @param mixed $permission
89
     */
90
    public function testItGrantOptionOnAll($permission): void
91
    {
92
        $name = 'jon';
93
        $password = 'p4sw00rd';
94
95
        $user = new User($name, $password);
96
        $user->setGrant($permission);
97
        $this->assertEquals(<<<T
98
GRANT $permission ON *.* TO '$name'@'%';
99
T
100
, $user->toSqlPrivileges());
101
    }
102
103
    /**
104
     * @dataProvider getPermissionValid
105
     *
106
     * @param mixed $permission
107
     */
108
    public function testItGrantOptionOnDatabase($permission): void
109
    {
110
        $name = 'jon';
111
        $password = 'p4sw00rd';
112
        $database = 'db';
113
114
        $user = new User($name, $password);
115
        $user->setGrant($permission, $database);
116
        $this->assertEquals(<<<T
117
GRANT $permission ON $database.* TO '$name'@'%';
118
T
119
, $user->toSqlPrivileges());
120
    }
121
122
    /**
123
     * @dataProvider getPermissionValid
124
     *
125
     * @param mixed $permission
126
     */
127
    public function testItGrantOptionOnTable($permission): void
128
    {
129
        $name = 'jon';
130
        $password = 'p4sw00rd';
131
        $database = 'db';
132
        $table = 'table';
133
134
        $user = new User($name, $password);
135
        $user->setGrant($permission, $database, $table);
136
        $this->assertEquals(<<<T
137
GRANT $permission ON $database.$table TO '$name'@'%';
138
T
139
, $user->toSqlPrivileges());
140
    }
141
142
    public function testItGrantOptionsMultiple(): void
143
    {
144
        $name = 'jon';
145
        $password = 'p4sw00rd';
146
        $database = 'db';
147
        $table = 'table';
148
        $permissions = [
149
            'CREATE',
150
            'UPDATE',
151
        ];
152
153
        $user = new User($name, $password);
154
        $user->setGrants($permissions, $database, $table);
155
        $this->assertEquals(<<<T
156
GRANT CREATE ON $database.$table TO '$name'@'%';
157
GRANT UPDATE ON $database.$table TO '$name'@'%';
158
T
159
, $user->toSqlPrivileges());
160
    }
161
162
    public function getMappingPermission(string $permission): string
163
    {
164
        return SQLSrvUserFactory::MAPPING_PERMISSION[$permission];
165
    }
166
167
    public function getHostDefault(): array
168
    {
169
        return [
170
            [''],
171
            [' '],
172
            ['%'],
173
        ];
174
    }
175
176
    public function getNameInvalid(): array
177
    {
178
        return [
179
            ['jon doe'],
180
        ];
181
    }
182
183
    public function getPermissionValid(): array
184
    {
185
        return [
186
            ['ALL PRIVILEGES'],
187
            ['CREATE'],
188
            ['DROP'],
189
            ['DELETE'],
190
            ['INSERT'],
191
            ['SELECT'],
192
            ['UPDATE'],
193
            ['GRANT OPTION'],
194
        ];
195
    }
196
}
197