PHP
  Home arrow PHP arrow Page 3 - The Active Record Pattern
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The Active Record Pattern
By: php|architect
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    2005-12-22


    Table of Contents:
  • The Active Record Pattern
  • Sample Code
  • Test Independence
  • Record Creation

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The Active Record Pattern - Test Independence
    ( Page 3 of 4 )

    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
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT