join($queryparts,",")."
WHERE id='{$id}'";
return mysql_query($query, $this->connection);
}
}
?>
We need to test all the public methods in this class to ensure they are working
properly. So here come our test cases.
require_once "PHPUnit/Framework/TestCase.php";
class DBTester extends PHPUnit_Framework_TestCase
{
private $connection;
private $Db;
protected function setup()
{
$this->Db = new DB();
$this->connection = mysql_connect("localhost","root","root1234");
mysql_select_db("abcd",$this->connection);
}
protected function tearDown()
{
mysql_close($this->connection);
}
public function testValidInsert()
{
$data = array("name"=>"afif","pass"=>md5("hello world"));
mysql_query("delete from users");
$result = $this->Db->insertData($data);
$this->assertNotNull($result);
$affected_rows = mysql_affected_rows($this->connection);
$this->assertEquals(1, $affected_rows);
}
public function testInvalidInsert()
{
$data = array("names"=>"afif","passwords"=>md5("hello world"));
mysql_query("delete from users");
$result = $this->Db->insertData($data);
Reflection and Unit Testing
[ 122 ]
$this->assertNotNull($result);
$affected_rows = mysql_affected_rows($this->connection);
$this->assertEquals(-1, $affected_rows);
}
public function testUpdate()
{
$data = array("name"=>"afif","pass"=>md5("hello world"));
mysql_query("truncate table users");
$this->Db->insertData($data);
$data = array("name"=>"afif2","pass"=>md5("hello world"));
$result = $this->Db->updateData(1, $data);
$this->assertNotNull($result);
$affected_rows = mysql_affected_rows($this->connection);
$this->assertEquals(1, $affected_rows);
}
public function testDelete()
{
$data = array("name"=>"afif","pass"=>md5("hello world"));
mysql_query("truncate table users");
$this->Db->insertData($data);
$result = $this->Db->deleteData(1);
$this->assertNotNull($result);
$affected_rows = mysql_affected_rows($this->connection);
$this->assertEquals(1, $affected_rows);
}
}
?>
The test suite is like this:
require_once 'PHPUnit/TextUI/TestRunner.
Pages:
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137