PHP
  Home arrow PHP arrow The Active Record Pattern, concluded
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, concluded
By: php|architect
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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, 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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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