Home arrow PHP arrow Page 3 - The Active Record Pattern

Test Independence - PHP

This article, the first of two parts, helps you use design patterns to better organize how your web application interacts with a database. It is excerpted from chapter 14 of the book php|architect's Guide to PHP Design Patterns, written by Jason E. Sweat (php|architect, 2005; ISBN: 0973589825).

TABLE OF CONTENTS:
  1. The Active Record Pattern
  2. Sample Code
  3. Test Independence
  4. Record Creation
By: php|architect
Rating: starstarstarstarstar / 18
December 22, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Tests should be independent of each other; otherwise, the mere running of a certain test could interfere with the results of latter tests.

To avoid interference between tests that rely on a database, it's best to drop and recreate the database (or just specific tables) between each test method. SimpleTest provides the standard xUnit setup() method to prepare for each test.

Here's how you might "reset" the database between each test:

class ActiveRecordTestCase extends UnitTestCase {  
  protected $conn;
  function __construct($name='') {
    $this->UnitTestCase($name);
    $this->conn = DB::conn();
  }
  function setup() {
    $this->conn->execute('drop table bookmark');
    $this->conn->execute(BOOKMARK_TABLE_DDL);
  }
}

The code populates the $conn attribute with a standard ADOConnection object and then uses the connection's execute() method to perform SQL statements dropping and recreating the table. Because this is in the setup() method, each test method starts out with a fresh copy of the database table to work with.

Going a little further, you can do some basic sanity checks of the setup() method (and learn a little bit about the ADOConnection  API along the way):

class ActiveRecordTestCase extends UnitTestCase { 
  // ...
  function testSetupLeavesTableEmptyWithCorrectStructure() {
    $rs = $this->conn->execute('select * from bookmark');
    $this->assertIsA($rs, 'ADORecordSet');
    $this->assertEqual(0,$rs->recordCount());
    foreach(array(
      'id',
      'url',
      'name',
      'description',
      'tag',
      'created',
      'updated') as $i => $name) {
      $this->assertEqual($name, $rs->fetchField($i)->name);
    }
  }
}

Even if you're unfamiliar with ADOdb, you can probably still discern that the execute() method returns an ADORecordSet object if successful. The object has a recordCount() method, which is used here to verify the table is empty. The record set object also has some methods to explore result set metadata and the fetchField() is used to verify the structure of the table.



 
 
>>> More PHP Articles          >>> More By php|architect
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: