Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cvgellhorn committed Sep 5, 2014
1 parent a973306 commit 55f4ff1
Showing 1 changed file with 115 additions and 28 deletions.
143 changes: 115 additions & 28 deletions tests/OneDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,20 @@ public static function setUpBeforeClass()
'password' => $GLOBALS['db_password']
));

// Init test data and also perform test on query method
// Init test data and perform test on query method
self::$_db->query(
'CREATE TABLE IF NOT EXISTS '
. self::$_db->btick(self::$_table). ' ('
'CREATE TABLE IF NOT EXISTS ' . self::$_table . ' ('
. 'id INT(9) NOT NULL PRIMARY KEY AUTO_INCREMENT,'
. 'name VARCHAR(50) NOT NULL'
. 'name VARCHAR(50) NOT NULL,'
. 'email VARCHAR(50) NOT NULL,'
. 'tel VARCHAR(30) NOT NULL'
. ') ENGINE = InnoDB'
);
}

public static function tearDownAfterClass()
{
self::$_db->query(
'DROP TABLE IF EXISTS ' . self::$_db->btick(self::$_table)
);
self::$_db->query('DROP TABLE IF EXISTS ' . self::$_table);
}

public function testGetPDO()
Expand All @@ -65,42 +64,101 @@ public function testBtick()

public function testInsert()
{
$id = self::$_db->insert(self::$_table, array(
'name' => 'John Doe'
$aID = self::$_db->insert(self::$_table, array(
'name' => 'John Doe',
'email' => 'jd@jd.com',
'tel' => 55555555
));

$this->assertTrue(is_int($aID) && $aID > 0);

$bID = self::$_db->insert(self::$_table, array(
'name' => 'Skywalker',
'email' => 'sw@sw.com',
'tel' => 44444444
));

$this->assertTrue(is_int($id) && $id > 0);
$this->assertTrue(is_int($bID) && $bID > 0);
}

public function testMultiInsert()
{
self::$_db->multiInsert(self::$_table,
array('name', 'email', 'tel'),
array(
array(
'John Doe',
'john@doe.com',
12345678
),
array(
'John Smith',
'john@smith.com',
11223344
),
array(
'Jack Smith',
'jack@smith.com',
87654321
)
)
);

$result = self::$_db->fetchRow(
'SELECT * FROM ' . self::$_table . ' WHERE tel = 87654321'
);

// Check if last value was inserted successfully
$this->assertEquals('jack@smith.com', $result['email']);
}

public function testSave()
{
$testTel = '22222222';

$tmpId = self::$_db->save(self::$_table, array(
'name' => 'Bill Gates',
'email' => 'bg@microsoft.com',
'tel' => '11111111'
));

$id = self::$_db->insert(self::$_table, array(
'name' => 'Skywalker'
$id = self::$_db->save(self::$_table, array(
'id' => $tmpId,
'name' => 'Bill Gates',
'email' => 'bg@microsoft.com',
'tel' => $testTel
));

$this->assertTrue(is_int($id) && $id > 0);
$this->assertSame($tmpId, $id);

$tel = self::$_db->fetchOne(
'SELECT tel FROM ' . self::$_table . ' WHERE id = ' . $tmpId
);

$this->assertEquals($tel, $testTel);
}

public function testUpdate()
{
$name = 'Steve Jobs';
$testName = 'Steve Jobs';

self::$_db->update(
self::$_table,
array('name' => $name),
array('name' => $testName),
array('id = ?' => 1)
);

$result = self::$_db->fetchRow(
'SELECT * FROM ' . self::$_db->btick(self::$_table)
. ' WHERE ' . self::$_db->btick('id') . ' = 1'
'SELECT * FROM ' . self::$_table . ' WHERE id = 1'
);

$this->assertEquals($name, $result['name']);
$this->assertEquals($testName, $result['name']);
}

public function testFetchAll()
{
$result = self::$_db->fetchAll(
'SELECT * FROM ' . self::$_db->btick(self::$_table)
'SELECT * FROM ' . self::$_table
);

$this->assertTrue(is_array($result[0]));
Expand All @@ -110,10 +168,11 @@ public function testFetchAll()
public function testFetchAssoc()
{
$result = self::$_db->fetchAssoc(
'SELECT * FROM ' . self::$_db->btick(self::$_table)
'SELECT * FROM ' . self::$_table
);

$this->assertTrue(count($result) > 0);

foreach ($result as $id => $row) {
// Compare int with string
$this->assertTrue($id == $row['id']);
Expand All @@ -123,8 +182,7 @@ public function testFetchAssoc()
public function testFetchRow()
{
$result = self::$_db->fetchRow(
'SELECT * FROM ' . self::$_db->btick(self::$_table)
. ' WHERE ' . self::$_db->btick('id') . ' = 1'
'SELECT * FROM ' . self::$_table . ' WHERE id = 1'
);

$this->assertArrayHasKey('name', $result);
Expand All @@ -135,17 +193,31 @@ public function testFetchOne()
$name = 'Steve Jobs';

$result = self::$_db->fetchOne(
'SELECT ' . self::$_db->btick('name') . ' FROM ' . self::$_db->btick(self::$_table)
. ' WHERE ' . self::$_db->btick('id') . ' = 1'
'SELECT name FROM ' . self::$_table . ' WHERE id = 1'
);

$this->assertEquals($name, $result);
}

public function testQuery()
{
// insert, update, delete
// all fetching modes
$testName = 'Steve Jobs';

// Perform tests on fetching modes
$fetchAll = self::$_db->query(
'SELECT * FROM ' . self::$_table
);
$fetchRow = self::$_db->query(
'SELECT * FROM ' . self::$_table . ' WHERE id = 1'
);
$fetchOne = self::$_db->query(
'SELECT name FROM ' . self::$_table . ' WHERE id = 1'
);

$this->assertTrue(is_array($fetchAll[0]));
$this->assertArrayHasKey('name', $fetchAll[0]);
$this->assertEquals($testName, $fetchRow['name']);
$this->assertEquals($testName, $fetchOne);
}

public function testDelete()
Expand All @@ -156,16 +228,31 @@ public function testDelete()
);

$result = self::$_db->fetchOne(
'SELECT ' . self::$_db->btick('name') . ' FROM ' . self::$_db->btick(self::$_table)
. ' WHERE ' . self::$_db->btick('id') . ' = 2'
'SELECT name FROM ' . self::$_table . ' WHERE id = 2'
);

$this->assertNull($result);
}

public function testTruncte()
{
self::$_db->truncate(self::$_table);

$result = self::$_db->fetchAll('SELECT * FROM ' . self::$_table);
$this->assertEmpty($result);
}

public function testDescribe()
{
$result = self::$_db->describe(self::$_table);

$realKeys = array('id', 'name', 'email', 'tel');
$resultKeys = array_keys($result);

$this->assertSame(
array_diff($realKeys, $resultKeys),
array_diff($resultKeys, $realKeys)
);
}

public function testDrop()
Expand Down

0 comments on commit 55f4ff1

Please sign in to comment.