Skip to content

Commit c56abfb

Browse files
committed
Update unit tests for the class Database
1 parent 8edaebf commit c56abfb

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

tests/JSONDBTest.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
class JSONDBTest extends PHPUnit_Framework_TestCase
66
{
77
/**
8-
* @var \JSONDB\JSONDB
8+
* @var \JSONDB\Database
99
*/
1010
private static $database;
1111

12+
/**
13+
* @var \JSONDB\JSONDB
14+
*/
15+
private static $jsondb;
16+
1217
public static function setUpBeforeClass()
1318
{
1419
// create db
15-
self::$database = new JSONDB();
16-
self::$database->createServer('__phpunit_test_server', '__phpunit', '', TRUE);
20+
self::$jsondb = new JSONDB();
21+
self::$database = self::$jsondb->createServer('__phpunit_test_server', '__phpunit', '', TRUE);
1722
self::$database->createDatabase('__phpunit_test_database')->setDatabase('__phpunit_test_database');
1823
self::$database->createTable('__phpunit_test_table_npk', array('php' => array('type' => 'string'), 'unit' => array('type' => 'int')));
1924
self::$database->createTable('__phpunit_test_table_pk', array('id' => array('auto_increment' => TRUE)));
@@ -25,31 +30,31 @@ public static function setUpBeforeClass()
2530
*/
2631
public function testExceptionIsRaisedForExistingServer()
2732
{
28-
self::$database->createServer('__phpunit_test_server', '__phpunit', '', TRUE);
33+
self::$jsondb->createServer('__phpunit_test_server', '__phpunit', '', TRUE);
2934
}
3035

3136
/**
3237
* @expectedException \JSONDB\Exception
3338
*/
3439
public function testExceptionIsRaisedForNonExistingServer()
3540
{
36-
self::$database->connect('NotExistingServerTest', '__phpunit', '');
41+
self::$jsondb->connect('NotExistingServerTest', '__phpunit', '');
3742
}
3843

3944
/**
4045
* @expectedException \JSONDB\Exception
4146
*/
4247
public function testExceptionIsRaisedForInvalidUser()
4348
{
44-
self::$database->connect('__phpunit_test_server', 'InvalidUser', '');
49+
self::$jsondb->connect('__phpunit_test_server', 'InvalidUser', '');
4550
}
4651

4752
/**
4853
* @expectedException \JSONDB\Exception
4954
*/
5055
public function testExceptionIsRaisedForInvalidPassword()
5156
{
52-
self::$database->connect('__phpunit_test_server', '__phpunit', 'InvalidPassword');
57+
self::$jsondb->connect('__phpunit_test_server', '__phpunit', 'InvalidPassword');
5358
}
5459

5560
/**
@@ -67,7 +72,7 @@ public function testExceptionIsRaisedForBadDatabaseConnection()
6772
public function testExceptionIsRaisedForExistingDatabase()
6873
{
6974
self::$database->disconnect();
70-
self::$database->connect('__phpunit_test_server', '__phpunit', '');
75+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '');
7176
self::$database->createDatabase('__phpunit_test_database');
7277
}
7378

@@ -86,70 +91,70 @@ public function testExceptionIsRaisedForBadDatabaseConnection2()
8691
public function testExceptionIsRaisedForExistingTable()
8792
{
8893
self::$database->disconnect();
89-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
94+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
9095
self::$database->createTable('__phpunit_test_table_npk', array('php' => array('type' => 'string'), 'unit' => array('type' => 'int')));
9196
}
9297

9398
public function testForInsertQuery()
9499
{
95100
self::$database->disconnect();
96-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
101+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
97102
$bool = self::$database->query('__phpunit_test_table_npk.insert(\'hello\', 0)');
98103
$this->assertTrue($bool);
99104
}
100105

101106
public function testForUpdateQuery()
102107
{
103108
self::$database->disconnect();
104-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
109+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
105110
$bool = self::$database->query('__phpunit_test_table_npk.update(php, unit).with(\'world\', 1)');
106111
$this->assertTrue($bool);
107112
}
108113

109114
public function testForReplaceQuery()
110115
{
111116
self::$database->disconnect();
112-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
117+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
113118
$bool = self::$database->query('__phpunit_test_table_npk.replace(\'nice\', 2)');
114119
$this->assertTrue($bool);
115120
}
116121

117122
public function testForSelectQuery()
118123
{
119124
self::$database->disconnect();
120-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
125+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
121126
$r = self::$database->query('__phpunit_test_table_npk.select(php, unit)');
122127
$this->assertInstanceOf('\JSONDB\\QueryResult', $r);
123128
}
124129

125130
public function testForSelectQueryFetchObject()
126131
{
127132
self::$database->disconnect();
128-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
133+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
129134
$r = self::$database->query('__phpunit_test_table_npk.select(php, unit)')->fetch(JSONDB::FETCH_OBJECT);
130135
$this->assertInstanceOf('\JSONDB\\QueryResultObject', $r);
131136
}
132137

133138
public function testForDeleteQuery()
134139
{
135140
self::$database->disconnect();
136-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
141+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
137142
$bool = self::$database->query('__phpunit_test_table_npk.delete()');
138143
$this->assertTrue($bool);
139144
}
140145

141146
public function testForTruncateQuery()
142147
{
143148
self::$database->disconnect();
144-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
149+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
145150
$bool = self::$database->query('__phpunit_test_table_npk.truncate()');
146151
$this->assertTrue($bool);
147152
}
148153

149154
public function testForMultipleInsertion()
150155
{
151156
self::$database->disconnect();
152-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
157+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
153158
$bool = self::$database->query('__phpunit_test_table_pk.insert(null).and(null).and(null)');
154159
$this->assertTrue($bool);
155160
}
@@ -159,7 +164,7 @@ public function testForMultipleInsertion()
159164
*/
160165
public function testExceptionIsRaisedForDuplicatePKUKOnInsert() {
161166
self::$database->disconnect();
162-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
167+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
163168
self::$database->query('__phpunit_test_table_pk.insert(1)');
164169
}
165170

@@ -168,7 +173,7 @@ public function testExceptionIsRaisedForDuplicatePKUKOnInsert() {
168173
*/
169174
public function testExceptionIsRaisedForDuplicatePKUKOnUpdate() {
170175
self::$database->disconnect();
171-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
176+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
172177
self::$database->query('__phpunit_test_table_pk.update(id).with(1)');
173178
}
174179

@@ -177,7 +182,7 @@ public function testExceptionIsRaisedForDuplicatePKUKOnUpdate() {
177182
*/
178183
public function testExceptionIsRaisedForDuplicatePKUKOnReplace() {
179184
self::$database->disconnect();
180-
self::$database->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
185+
self::$database = self::$jsondb->connect('__phpunit_test_server', '__phpunit', '', '__phpunit_test_database');
181186
self::$database->query('__phpunit_test_table_pk.replace(2)');
182187
}
183188

tests/autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
require dirname(__DIR__) . '/src/JSONDB/QueryParser.php';
99
require dirname(__DIR__) . '/src/JSONDB/QueryResult.php';
1010
require dirname(__DIR__) . '/src/JSONDB/Util.php';
11+
require dirname(__DIR__) . '/src/JSONDB/Database.php';
1112
require dirname(__DIR__) . '/src/JSONDB/JSONDB.php';
1213

0 commit comments

Comments
 (0)