-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.php
More file actions
82 lines (63 loc) · 1.71 KB
/
index.php
File metadata and controls
82 lines (63 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* DB connection constants
*/
const DB_HOST = 'database';
const DB_USER = 'root';
const DB_PASS = '';
const DB_DATABASE = 'test';
const DB_TABLE = 'test_table';
function getLink(): mysqli
{
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS);
if (mysqli_connect_errno()) {
throw new Exception("Connection failed: %s\n" . mysqli_connect_error());
}
return $link;
}
/**
* Function to be tested
*
* Creates a table with JSON fields, a feature only available in MySQL 5.7+.
*/
function createTableWithJsonFields(): bool
{
$link = getLink();
mysqli_select_db($link, DB_DATABASE);
$createTableQuery = 'CREATE TABLE IF NOT EXISTS '.DB_TABLE.' (id INT, json_field JSON);';
if (!$result = mysqli_query($link, $createTableQuery)) {
throw new Exception("Table could not be created.");
}
mysqli_close($link);
return $result;
}
/**
* Gets the schema for our newly created table.
*/
function getSchema(): array
{
$link = getLink();
$createTableQuery = 'DESCRIBE '.DB_DATABASE.'.'.DB_TABLE.';';
if (!$result = mysqli_query($link, $createTableQuery)) {
throw new Exception("Table could not be created.");
}
mysqli_close($link);
return $result->fetch_all();
}
/**
* PHPUnit test code
*
* This ensures that the database connection is made and the table is created.
*/
use PHPUnit\Framework\TestCase;
class CreateJsonFieldTest extends TestCase
{
function testItConnectsAndCreatesTableWithJsonField()
{
$result = createTableWithJsonFields();
$newTable = getSchema();
$this->assertTrue($result);
$this->assertEquals('int(11)', $newTable[0][1]);
$this->assertEquals('json', $newTable[1][1]);
}
}