So I wrote the tests for the DB connection class and wrote the class itself. Here's how it looks like (test names should be self-explanatory):
Code: Select all
<?php
define('SIMPLE_TEST', '../simpletest/');
require_once(SIMPLE_TEST . 'unit_tester.php');
require_once(SIMPLE_TEST . 'reporter.php');
require_once('../classes/class.MySQLDatabase.php');
define('db_host', 'localhost');
define('db_name', 'test');
define('db_user', 'root');
define('db_pass', '');
class TestOfMySQLDatabase extends UnitTestCase
{
var $connection;
function TestOfMySQLDatabase()
{
$this->UnitTestCase('Test of MySQL Database');
}
function setUp()
{
$this->connection = mysql_connect(db_host, db_user, db_pass, true);
$this->query("CREATE DATABASE " . db_name);
mysql_select_db(db_name, $this->connection);
$this->query("CREATE TABLE test_table (test_field TEXT)");
$this->query("INSERT INTO test_table VALUES ('test_value')");
$this->db = &new MySQLDatabase(db_host, db_name, db_user, db_pass);
}
function tearDown()
{
mysql_query("DROP DATABASE " . db_name, $this->connection);
mysql_close($this->connection);
}
function testSuccessfullConnection()
{
$this->assertIdentical($this->db->error(), false);
$this->assertIdentical($this->db->getError(), '');
}
function testGoodQuery()
{
$result = $this->db->query("SELECT test_field FROM test_table");
$this->assertTrue(is_object($result));
$this->assertIdentical($result->fetch(), array('test_field' => 'test_value'));
$this->assertIdentical($this->db->error(), false);
$this->assertIdentical($this->db->getError(), '');
}
function testBadQuery()
{
$result = $this->db->query("foo");
$this->assertIdentical($this->db->error(), true);
$this->assertIdentical($this->db->getError(), 'SQLError');
}
function testGoodManipulate()
{
$this->db->manipulate("INSERT INTO test_table VALUES ('another_value')");
$this->assertIdentical($this->db->error(), false);
$this->assertIdentical($this->db->getError(), '');
$result = $this->db->query("SELECT test_field FROM test_table WHERE test_field='another_value'");
$this->assertTrue(is_object($result));
$this->assertIdentical($result->fetch(), array('test_field' => 'another_value'));
$this->assertIdentical($this->db->error(), false);
$this->assertIdentical($this->db->getError(), '');
}
function testBadManipulate()
{
$this->db->manipulate("foo");
$this->assertIdentical($this->db->error(), true);
$this->assertIdentical($this->db->getError(), 'SQLError');
}
function testErrorsAreClearedAfterEachQuery()
{
$this->db->manipulate("foo");
$this->assertIdentical($this->db->error(), true);
$this->assertIdentical($this->db->getError(), 'SQLError');
$result = $this->db->query("SELECT test_field FROM test_table");
$this->assertIdentical($this->db->error(), false);
$this->assertIdentical($this->db->getError(), '');
$result = $this->db->query("foo");
$this->assertIdentical($this->db->error(), true);
$this->assertIdentical($this->db->getError(), 'SQLError');
$this->db->manipulate("INSERT INTO test_table VALUES ('booh')");
$this->assertIdentical($this->db->error(), false);
$this->assertIdentical($this->db->getError(), '');
}
function testSuccessfullConnectionDisconnect()
{
$this->db->disconnect();
$this->assertIdentical($this->db->error(), false);
$this->assertIdentical($this->db->getError(), '');
}
//Connection problem tests (separate db object is created)
function testSuccessfullConnectionWrongDB()
{
$db = &new MySQLDatabase(db_host, 'booh', db_user, db_pass);
$this->assertIdentical($db->error(), true);
$this->assertIdentical($db->getError(), 'NoDB');
$result = $db->query("SELECT test_field FROM test_table");
$this->assertIdentical($db->error(), true);
$this->assertIdentical($db->getError(), 'NoDB');
$db->manipulate("INSERT INTO test_table VALUES ('booh')");
$this->assertIdentical($db->error(), true);
$this->assertIdentical($db->getError(), 'NoDB');
}
function testUnsuccessfullConnection()
{
$db = &new MySQLDatabase('booh', db_name, db_user, db_pass);
$this->assertIdentical($db->error(), true);
$this->assertIdentical($db->getError(), 'UnableToConnect');
$result = $db->query("SELECT test_field FROM test_table");
$this->assertIdentical($db->error(), true);
$this->assertIdentical($db->getError(), 'UnableToConnect');
$db->manipulate("INSERT INTO test_table VALUES ('booh')");
$this->assertIdentical($db->error(), true);
$this->assertIdentical($db->getError(), 'UnableToConnect');
}
function testSuccessfullConnectionWrongDBDisconnect()
{
$db = &new MySQLDatabase(db_host, 'booh', db_user, db_pass);
$db->disconnect();
$this->assertIdentical($db->error(), false);
$this->assertIdentical($db->getError(), '');
}
function testUnsuccessfullConnectionDisconnect()
{
$db = &new MySQLDatabase('booh', db_name, db_user, db_pass);
$db->disconnect();
$this->assertIdentical($db->error(), true);
$this->assertIdentical($db->getError(), 'UnableToDisconnect');
}
function query($sql)
{
mysql_query($sql, $this->connection);
}
}
$test = &new TestOfMySQLDatabase();
$test->run(new HtmlReporter());
?>
And this is the class:
Code: Select all
<?php
require_once('class.MySQLResultIterator.php');
class MySQLDatabase
{
var $connection;
var $select;
var $error;
function MySQLDatabase($host, $name, $username, $password)
{
$this->connection = @mysql_connect($host, $username, $password, true);
if ($this->connection)
{
$this->select = mysql_select_db($name, $this->connection);
if ($this->select)
{
$this->error = '';
return;
}
$this->error = 'NoDB';
return;
}
$this->error = 'UnableToConnect';
}
function query($sql)
{
if ($this->select)
{
$result = mysql_query($sql, $this->connection);
if (!$result)
{
$this->error = 'SQLError';
return;
}
$this->error = '';
return $iterator = &new MySQLResultIterator($result);
}
}
function manipulate($sql)
{
if ($this->select)
{
$result = mysql_query($sql, $this->connection);
if (!$result)
{
$this->error = 'SQLError';
return;
}
$this->error = '';
}
}
function disconnect()
{
if ($this->connection)
{
$this->error = '';
mysql_close($this->connection);
return;
}
$this->error = 'UnableToDisconnect';
}
function getError()
{
return $this->error;
}
function error()
{
return (bool)$this->error;
}
}
?>
When I did all this, now I wonder again: aren't I doing too much testing? There's quite a lot of code in the tests... Do you think all the tests are proper or are some of them unneccessary at all? You can easily notice, that tests pretty much cover all possible variations of method usage. Now since I pretty much finished it, any comments on this would be greatly appreciated.