Yet Another TDD Newbie
Posted: Sun Nov 20, 2005 2:42 pm
I've never used (or really understood) TDD before, but since everyone says it's good, I thought I'd give it a try. I decided to write a set of classes to use when working against a MySQL database. I started with the class that will handle a connection. I've named the class DatabaseConnection, since the application I'm writing will only use MySQL. (If it's a bad name, let me know. I'm still learning to use OOP.) I've read some articles about TDD, and I think that I understand the basics. So, I used TDD (at least I think I did
) when writing this class. Here's what I ended up with:
What do you think about this? Don't be afraid to say exactly what you think about my tests - that's what I want you to do.
Code: Select all
<?php
require_once LIBRARY .
'packages/database/connection.php';
class DatabaseConnectionTestCase extends UnitTestCase
{
private $connection;
public function __construct()
{
$this->connection = new DatabaseConnection;
}
public function testFailedConnectionReturnFalse()
{
$status = $this->connection->connect(
'localhost', 'erik', 'invalid password');
$this->assertIdentical($status, false);
}
public function testSuccessfulConnectionReturnTrue()
{
$status = $this->connection->connect(
'localhost', 'erik', 'valid password');
$this->assertIdentical($status, true);
}
public function testInvalidDatabaseReturnFalse()
{
$status = $this->connection->select('invalid');
$this->assertIdentical($status, false);
}
public function testValidDatabaseReturnTrue()
{
$status = $this->connection->select('testing');
$this->assertIdentical($status, true);
}
public function testCloseConnection()
{
$this->connection->disconnect();
$this->assertNull($this->connection->getResource());
}
}
?>Code: Select all
<?php
class DatabaseConnection
{
private $resource;
public function connect($host, $username, $password)
{
return ( ($this->resource =@ mysql_connect($host, $username, $password)) !== false );
}
public function select($database)
{
return ( @ mysql_select_db($database, $this->resource) !== false );
}
public function disconnect()
{
@ mysql_close($this->resource); $this->resource = null;
}
public function getResource() { return $this->resource; }
}
?>