|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class DatabaseTest extends TestCase |
|
7
|
|
|
{ |
|
8
|
|
|
protected $className = '\Suricate\Database'; |
|
9
|
|
|
protected $tableName = 'users'; |
|
10
|
|
|
|
|
11
|
|
|
protected function setupData() |
|
12
|
|
|
{ |
|
13
|
|
|
$pdo = new PDO('sqlite:/tmp/test.db'); |
|
14
|
|
|
$pdo->exec("DROP TABLE IF EXISTS `" . $this->tableName ."`"); |
|
15
|
|
|
$pdo->exec("CREATE TABLE `" .$this->tableName. "` (`id` INTEGER PRIMARY KEY,`name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)"); |
|
16
|
|
|
$stmt = $pdo->prepare("INSERT INTO `" . $this->tableName . "` (name, date_added) VALUES (:name, :date)"); |
|
17
|
|
|
$values = [ |
|
18
|
|
|
['John', '2019-01-10 00:00:00'], |
|
19
|
|
|
['Paul', '2019-01-11 00:00:00'], |
|
20
|
|
|
['Robert', '2019-01-12 00:00:00'] |
|
21
|
|
|
]; |
|
22
|
|
|
foreach ($values as $value) { |
|
23
|
|
|
$res = $stmt->execute(['name' => $value[0], 'date' => $value[1]]); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->setupData(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testContructor() |
|
33
|
|
|
{ |
|
34
|
|
|
$className = $this->className; |
|
35
|
|
|
$database = new $className(); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertNull($database->getConfig()); |
|
38
|
|
|
$reflection = new \ReflectionClass(get_class($database)); |
|
39
|
|
|
$property = $reflection->getProperty('handler'); |
|
40
|
|
|
$property->setAccessible(true); |
|
41
|
|
|
$this->assertEquals($property->getValue($database), false); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testGetSetConfig() |
|
45
|
|
|
{ |
|
46
|
|
|
$className = $this->className; |
|
47
|
|
|
$configName = "testConfig"; |
|
48
|
|
|
|
|
49
|
|
|
$database = new $className(); |
|
50
|
|
|
$this->assertNull($database->getConfig()); |
|
51
|
|
|
$retVar = $database->setConfig($configName); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertInstanceOf($className, $retVar); |
|
54
|
|
|
$this->assertEquals($configName, $database->getConfig()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testConnect() |
|
58
|
|
|
{ |
|
59
|
|
|
$className = $this->className; |
|
60
|
|
|
$database = new $className(); |
|
61
|
|
|
$database->configure([ |
|
62
|
|
|
'type' => 'sqlite', |
|
63
|
|
|
'file' => '/tmp/test.db', |
|
64
|
|
|
]); |
|
65
|
|
|
$database->query("SELECT * FROM users"); |
|
66
|
|
|
$reflection = new \ReflectionClass(get_class($database)); |
|
67
|
|
|
$property = $reflection->getProperty('handler'); |
|
68
|
|
|
$property->setAccessible(true); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertInstanceOf('\PDO', $property->getValue($database)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testUnsupportedHandler() |
|
74
|
|
|
{ |
|
75
|
|
|
$className = $this->className; |
|
76
|
|
|
$database = new $className(); |
|
77
|
|
|
$database->configure([ |
|
78
|
|
|
'type' => 'my-pdo-handler', |
|
79
|
|
|
]); |
|
80
|
|
|
$this->expectException(\Exception::class); |
|
81
|
|
|
$database->query("SELECT * FROM users"); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testFetchAll() |
|
85
|
|
|
{ |
|
86
|
|
|
$className = $this->className; |
|
87
|
|
|
$database = new $className(); |
|
88
|
|
|
$database->configure([ |
|
89
|
|
|
'type' => 'sqlite', |
|
90
|
|
|
'file' => '/tmp/test.db', |
|
91
|
|
|
]); |
|
92
|
|
|
$queryResult = $database->query("SELECT * FROM `" . $this->tableName . "`"); |
|
93
|
|
|
$this->assertEquals($queryResult->fetchAll(), [ |
|
94
|
|
|
['id' => '1', 'name' => 'John', 'date_added' => '2019-01-10 00:00:00'], |
|
95
|
|
|
['id' => '2', 'name' => 'Paul', 'date_added' => '2019-01-11 00:00:00'], |
|
96
|
|
|
['id' => '3', 'name' => 'Robert', 'date_added' => '2019-01-12 00:00:00'], |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function tearDown() |
|
101
|
|
|
{ |
|
102
|
|
|
if (is_file('/tmp/test.db')) { |
|
103
|
|
|
unlink('/tmp/test.db'); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|