<?php
require_once dirname(__FILE__).'/DB.php';
class DBTest extends UnitTestCase{
private $db;
private $createDbQuery =
'
CREATE DATABASE /*!32312 IF NOT EXISTS*/`pdo_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `pdo_db`;
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(20) default NULL,
`password` varchar(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
';
public function DBTest(){
$this->UnitTestCase('DB Test');
$this->db = new DB();
}
public function testCreateDb(){
$result = $this->db->query($this->createDbQuery);
$this->db->selectDB('pdo_db');
$this->assertEqual(1,$result);
}
public function testInsert(){
$data = array('name'=>'testName','password'=>'testPassword');
$table = 'user';
$this->assertEqual(1,$this->db->insert($table,$data));
}
public function testUpdate(){
$data = array('name'=>'testName1', 'password'=>'testPassword1');
$where = array("id= ".$this->db->getLastInsertId(),"AND name='testName'");
$table = 'user';
$this->assertEqual(1,$this->db->update($table,$data,$where));
}
public function testInsertQuery(){
$query = "INSERT INTO `user` SET `name`='testName2', `password`='testPassword2'";
$this->assertTrue($this->db->query($query));
}
public function testUpdateQuery(){
$query = "UPDATE `user` SET `name`='testName3', `password`='testPassword3' WHERE `id`=2";
$this->assertTrue($this->db->query($query));
}
public function testSelectQuery(){
$query = "SELECT * FROM `user` WHERE `id`='1'";
$this->db->query($query);
$this->assertEqual(1,$this->db->getRowCount());
}
public function testGetRow(){
$query = "SELECT * FROM `user`";
$result = $this->db->getRow($query);
$testObject = new stdClass();
$testObject->id = '1';
$testObject->name = 'testName1';
$testObject->password = 'testPassword1';
$this->assertIdentical($result,$testObject);
}
public function testGetResults(){
$query = "SELECT * FROM `user`";
$this->db->getResults($query);
$this->assertEqual(2,$this->db->getRowCount());
}
public function testDelete(){
$where = array('id = 1');
$table = 'user';
$this->assertTrue($this->db->delete($table,$where));
}
public function testDeleteQuery(){
$query = "DELETE FROM `user` WHERE `id`='2'";
$this->assertEqual(1,$this->db->query($query));
}
/**
* @property DB $db
*/
public function testDeleteDB(){
$query = "DROP DATABASE IF EXISTS `pdo_db`";
$this->assertTrue($this->db->query($query));
}
}
?>
|