However, as specific you go
with your unit tests, the better outcome you can expect. Do also remember that of the
many unit tests you write, only few of them are actually useful.
Now we will discuss how to test routines that works with a database. Let us create a
small class which inserts, finds and updates the record, which we will write unit tests
for. Here comes our small class, which directly interacts with a table named users in
our database.
class DB
{
private $connection;
public function __construct()
{
$this->connection = mysql_connect("localhost","root","root1234");
mysql_select_db("test",$this->connection);
}
public function insertData($data)
{
$fields = join(array_keys($data),",");
$values = "'".join(array_values($data),",")."'";
$query = "INSERT INTO users({$fields}) values({$values})";
return mysql_query($query, $this->connection);
}
public function deleteData($id)
{
$query = "delete from users where id={$id}";
return mysql_query($query, $this->connection);
}
public function updateData($id, $data)
{
$queryparts = array();
foreach ($data as $key=>$value)
Chapter 5
[ 121 ]
{
$queryparts[] = "{$key} = '{$value}'";
}
$query = "UPDATE users SET ".
Pages:
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136