SQLiteUserFactoryTest::testItGrantOptionOnTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.9666
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 SQLiteUserFactoryTest 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
        $user->setPlatform('SQLite');
37
        $this->assertEquals(<<<T
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
        $user->setPlatform('SQLite');
50
        $this->assertEquals(<<<T
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
        $user->setPlatform('SQLite');
67
        $this->assertEquals(<<<T
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
        $user->setPlatform('SQLite');
80
        $this->assertEquals(<<<T
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->setPlatform('SQLite');
97
        $user->setGrant($permission);
98
        $this->assertEquals(<<<T
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->setPlatform('SQLite');
116
        $user->setGrant($permission, $database);
117
        $this->assertEquals(<<<T
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->setPlatform('SQLite');
136
        $user->setGrant($permission, $database, $table);
137
        $this->assertEquals(<<<T
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->setPlatform('SQLite');
155
        $user->setGrants($permissions, $database, $table);
156
        $this->assertEquals(<<<T
157
T
158
, $user->toSqlPrivileges());
159
    }
160
161
    public function getMappingPermission(string $permission): string
162
    {
163
        return SQLSrvUserFactory::MAPPING_PERMISSION[$permission];
164
    }
165
166
    public function getHostDefault(): array
167
    {
168
        return [
169
            [''],
170
            [' '],
171
            ['%'],
172
        ];
173
    }
174
175
    public function getNameInvalid(): array
176
    {
177
        return [
178
            ['jon doe'],
179
        ];
180
    }
181
182
    public function getPermissionValid(): array
183
    {
184
        return [
185
            ['ALL PRIVILEGES'],
186
            ['CREATE'],
187
            ['DROP'],
188
            ['DELETE'],
189
            ['INSERT'],
190
            ['SELECT'],
191
            ['UPDATE'],
192
            ['GRANT OPTION'],
193
        ];
194
    }
195
}
196