|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the samshal/scripd package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Samuel Adeshina <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
class JsonDbStructureTest extends PHPUnit_Framework_TestCase |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
public function parseJsonFile($jsonFile) |
|
15
|
|
|
{ |
|
16
|
|
|
$jsonDbStructure = new Samshal\Scripd\JsonDbStructure($jsonFile, 'sqlite'); |
|
17
|
|
|
$jsonDbStructure->parseStructure(); |
|
18
|
|
|
|
|
19
|
|
|
return $jsonDbStructure->getGeneratedSql(';'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function parseArrayInput($jsonFile) |
|
23
|
|
|
{ |
|
24
|
|
|
$jsonArray = json_decode(file_get_contents($jsonFile), JSON_FORCE_OBJECT); |
|
25
|
|
|
|
|
26
|
|
|
$jsonDbStructure = new Samshal\Scripd\JsonDbStructure($jsonArray, 'mysql'); |
|
27
|
|
|
$jsonDbStructure->parseStructure(); |
|
28
|
|
|
|
|
29
|
|
|
return $jsonDbStructure->getGeneratedSql(';'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function parseWithoutVendor($jsonFile) |
|
33
|
|
|
{ |
|
34
|
|
|
$jsonDbStructure = new Samshal\Scripd\JsonDbStructure($jsonFile); |
|
35
|
|
|
$jsonDbStructure->parseStructure(); |
|
36
|
|
|
|
|
37
|
|
|
return $jsonDbStructure->getGeneratedSql(';'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @dataProvider dataProvider |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testStructureParserWithJsonFileInput($expected, $jsonFile) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->assertEquals($expected, self::parseJsonFile($jsonFile)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @dataProvider dataProvider |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testStructureParserWithArrayInput($expected, $jsonFile) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->assertEquals($expected, self::parseArrayInput($jsonFile)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @dataProvider dataProvider |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testStructureParserWithoutVendor($expected, $jsonFile) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->assertEquals($expected, self::parseWithoutVendor($jsonFile)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function dataProvider() |
|
65
|
|
|
{ |
|
66
|
|
|
return array( |
|
67
|
|
|
'Create Database with Multiple Tables' => [ |
|
68
|
|
|
'CREATE DATABASE another_unify_schools;'. |
|
69
|
|
|
'USE another_unify_schools;'. |
|
70
|
|
|
"CREATE TABLE students (id int PRIMARY KEY, first_name varchar(20) DEFAULT 'samuel', last_name varchar(20), class varchar(10));". |
|
71
|
|
|
'CREATE TABLE faculty (fac_id int AUTO_INCREMENT PRIMARY KEY, first_name varchar(20), last_name varchar(20));'. |
|
72
|
|
|
'CREATE TABLE subjects (subject_id int AUTO_INCREMENT PRIMARY KEY, subject_name varchar(30), subject_faculty int , FOREIGN KEY (subject_faculty) REFERENCES faculty(fac_id) ON UPDATE cascade ON DELETE set null)', __DIR__.'/json/1.json', |
|
73
|
|
|
], |
|
74
|
|
|
'Alter Table' => [ |
|
75
|
|
|
"ALTER TABLE facultys ADD COLUMN (full_name varchar(30) NOT NULL DEFAULT 'john doe')", __DIR__.'/json/2.json', |
|
76
|
|
|
], |
|
77
|
|
|
'Drop Objects' => [ |
|
78
|
|
|
'DROP TABLE IF EXISTS faculty;'. |
|
79
|
|
|
'DROP DATABASE another_unify_schools', __DIR__.'/json/3.json', |
|
80
|
|
|
], |
|
81
|
|
|
'Create View' => [ |
|
82
|
|
|
'CREATE VIEW student_vw (id, first_name, last_name, class) AS select * from students where id < 3 WITH LOCAL CHECK OPTION', __DIR__.'/json/4.json', |
|
83
|
|
|
], |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.