PHP
  Home arrow PHP arrow The Active Record Pattern, concluded
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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, concluded
By: php|architect
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2005-12-29

    Table of Contents:
  • The Active Record Pattern, concluded
  • Active Record Instance ID
  • Searching for Records
  • Updating Records
  • Issues

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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, concluded


    (Page 1 of 5 )

    This article, the second 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).

    Testing Database Failure

    Databases usually just work, but failure is not unheard of. To make sure your code operates correctly under failure conditions, let’s simulate a failure using a Mock Object (see Chapter 6 — The Mock Object Pattern), which stands in for the connection object. 

     


    Mock::generate(‘ADOConnection’);
    class ActiveRecordTestCase extends UnitTestCase {
      //...
      function testDbFailure() {
        $conn = new MockADOConnection($this);
        $conn->expectOnce(‘execute’, array(‘*’,’*’));
        $conn->setReturnValue(‘execute’,false);
        $conn->expectOnce(‘errorMsg’);
        $conn->setReturnValue(‘errorMsg’,
          ‘The database has exploded!!!!’);
      }
    }

    This code calls Mock::generate() to create a MockADOConnection class, creates an instance of the mock connection, sets up some basic return values to indicate failure, and defines some expectations about what’s to be called in these circumstances.

    However, because the Bookmark constructor makes a call to the static DB:conn() method to retrieve the database connection, it’s difficult to inject the mock connection into that code. There are several possible workarounds: add a method to change $this->conn, add an optional parameter to each method, or add a parameter to the constructor. Let’s opt for the latter: add an optional connection class parameter to the Bookmark constructor:

    class Bookmark {
      // ...
      public function __construct($id=false, $conn=false) {
        $this->conn = ($conn) ? $conn : DB::conn();
        // ...
      }
    }

    Now new Bookmark works as normal, but new Bookmark(1, $connection) uses the $connection object instead of the normal ADOConnection object.

    With that code in place, you can now easily replace the “normal” database connection object with a MockADOconnection and verify the results of a “database failure.”

    class ActiveRecordTestCase extends UnitTestCase { 
      // ...
      function testDbFailure() {
        $conn = new MockADOConnection($this);
        $conn->expectOnce(‘execute’, array(‘*’,’*’));
        $conn->setReturnValue(‘execute’,false);
        $conn->expectOnce(‘errorMsg’);
        $conn->setReturnValue(‘errorMsg’,
          ‘The database has exploded!!!!’);    
        $link = new Bookmark(1,$conn);
        $this->assertErrorPattern(‘/exploded/i’);
        $conn->tally();
    }

    More PHP Articles
    More By php|architect


       · This article is an excerpt from the book "php|architect's Guide to PHP Design...
     

    Buy this book now. This article 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). Check it out at your favorite bookstore today. Buy this book now.

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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