Yet Another TDD Newbie

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
lilleman
Forum Newbie
Posts: 7
Joined: Thu Nov 17, 2005 4:55 pm
Location: Örebro, Sweden

Yet Another TDD Newbie

Post by lilleman »

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 :wink:) when writing this class. Here's what I ended up with:

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; }
}

?>
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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hmm... though I'm no unit testing guru, I don't think you should test this sort of stuff yet (way to many external sources)... try tdd with a more "thinker"-type class first.

And wait for McGruff to swing by and comment.
Post Reply