|
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; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use FlexPHP\Database\Builder; |
|
14
|
|
|
use FlexPHP\Database\Tests\TestCase; |
|
15
|
|
|
use FlexPHP\Schema\Constants\Keyword; |
|
16
|
|
|
use FlexPHP\Schema\Schema; |
|
17
|
|
|
use FlexPHP\Schema\SchemaInterface; |
|
18
|
|
|
|
|
19
|
|
|
class BuilderTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function testItPlatformErrorThrowException(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$this->expectException(Exception::class); |
|
24
|
|
|
$this->expectExceptionMessage('try: MySQL, SQLSrv'); |
|
25
|
|
|
|
|
26
|
|
|
new Builder('Unknow'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testItCreateMySQLDatabase(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$name = 'db'; |
|
32
|
|
|
|
|
33
|
|
|
$builder = new Builder('MySQL'); |
|
34
|
|
|
$builder->createDatabase($name); |
|
35
|
|
|
$this->assertEquals(<<<T |
|
36
|
|
|
CREATE DATABASE $name CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; |
|
37
|
|
|
T |
|
38
|
|
|
, $builder->toSql()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testItCreateSQLSrvDatabase(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$name = 'db'; |
|
44
|
|
|
|
|
45
|
|
|
$builder = new Builder('SQLSrv'); |
|
46
|
|
|
$builder->createDatabase($name); |
|
47
|
|
|
$this->assertEquals(<<<T |
|
48
|
|
|
CREATE DATABASE $name COLLATE latin1_general_100_ci_ai_sc; |
|
49
|
|
|
T |
|
50
|
|
|
, $builder->toSql()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testItCreateMySQLUser(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$name = 'mysql'; |
|
56
|
|
|
$password = 'p4sw00rd'; |
|
57
|
|
|
|
|
58
|
|
|
$builder = new Builder('MySQL'); |
|
59
|
|
|
$builder->createUser($name, $password); |
|
60
|
|
|
$this->assertEquals(<<<T |
|
61
|
|
|
CREATE USER '$name'@'%' IDENTIFIED BY '$password'; |
|
62
|
|
|
T |
|
63
|
|
|
, $builder->toSql()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testItCreateSQLSrvUser(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$name = 'sqlsrv'; |
|
69
|
|
|
$password = 'p4sw00rd'; |
|
70
|
|
|
|
|
71
|
|
|
$builder = new Builder('SQLSrv'); |
|
72
|
|
|
$builder->createUser($name, $password); |
|
73
|
|
|
$this->assertEquals(<<<T |
|
74
|
|
|
CREATE LOGIN $name WITH PASSWORD = '$password'; |
|
75
|
|
|
GO |
|
76
|
|
|
CREATE USER $name FOR LOGIN $name; |
|
77
|
|
|
GO |
|
78
|
|
|
T |
|
79
|
|
|
, $builder->toSql()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testItCreateMySQLTable(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$builder = new Builder('MySQL'); |
|
85
|
|
|
$builder->createTable($this->getSchema()); |
|
86
|
|
|
$this->assertEquals(<<<T |
|
87
|
|
|
CREATE TABLE bar ( |
|
88
|
|
|
foo VARCHAR(255) DEFAULT NULL COMMENT 'foo' |
|
89
|
|
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB; |
|
90
|
|
|
T |
|
91
|
|
|
, $builder->toSql()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testItCreateSQLSrvTable(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$builder = new Builder('SQLSrv'); |
|
97
|
|
|
$builder->createTable($this->getSchema()); |
|
98
|
|
|
$this->assertEquals(<<<T |
|
99
|
|
|
CREATE TABLE bar ( |
|
100
|
|
|
foo NVARCHAR(255) |
|
101
|
|
|
); |
|
102
|
|
|
T |
|
103
|
|
|
, $builder->toSql()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testItCreateMySQLComplete(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$dbname = 'complete'; |
|
109
|
|
|
$username = 'username'; |
|
110
|
|
|
$password = 'password'; |
|
111
|
|
|
$host = 'host'; |
|
112
|
|
|
$schema = new Schema('bar', 'title', [ |
|
113
|
|
|
[ |
|
114
|
|
|
Keyword::NAME => 'foo', |
|
115
|
|
|
Keyword::DATATYPE => 'string', |
|
116
|
|
|
Keyword::CONSTRAINTS => [ |
|
117
|
|
|
'minlength' => 10, |
|
118
|
|
|
'maxlength' => 100, |
|
119
|
|
|
], |
|
120
|
|
|
], [ |
|
121
|
|
|
Keyword::NAME => 'bar', |
|
122
|
|
|
Keyword::DATATYPE => 'integer', |
|
123
|
|
|
Keyword::CONSTRAINTS => [ |
|
124
|
|
|
'min' => 10, |
|
125
|
|
|
'max' => 100, |
|
126
|
|
|
], |
|
127
|
|
|
], |
|
128
|
|
|
]); |
|
129
|
|
|
|
|
130
|
|
|
$builder = new Builder('MySQL'); |
|
131
|
|
|
$builder->createDatabase($dbname); |
|
132
|
|
|
$builder->createUser($username, $password, $host); |
|
133
|
|
|
$builder->createTable($schema); |
|
134
|
|
|
$this->assertEquals(<<<T |
|
135
|
|
|
CREATE DATABASE $dbname CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; |
|
136
|
|
|
CREATE USER '$username'@'$host' IDENTIFIED BY '$password'; |
|
137
|
|
|
CREATE TABLE bar ( |
|
138
|
|
|
foo VARCHAR(100) DEFAULT NULL COMMENT 'foo', |
|
139
|
|
|
bar INT DEFAULT NULL COMMENT 'bar' |
|
140
|
|
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB; |
|
141
|
|
|
T |
|
142
|
|
|
, $builder->toSql()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function getSchema(): SchemaInterface |
|
146
|
|
|
{ |
|
147
|
|
|
return new Schema('bar', 'title', [[ |
|
148
|
|
|
Keyword::NAME => 'foo', |
|
149
|
|
|
Keyword::DATATYPE => 'string', |
|
150
|
|
|
Keyword::CONSTRAINTS => [ |
|
151
|
|
|
'min' => 10, |
|
152
|
|
|
'max' => 100, |
|
153
|
|
|
], |
|
154
|
|
|
]]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|