PHP
  Home arrow PHP arrow Page 4 - Unit Testing in Detail
Dev Shed Forums 
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

Unit Testing in Detail
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2006-10-26

    Table of Contents:
  • Unit Testing in Detail
  • Additional Features in PHPUnit
  • Adding More Test Conditions
  • Adding Listeners

  • 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


    Unit Testing in Detail - Adding Listeners


    (Page 4 of 4 )

    When you execute PHPUnit_TextUI_TestRunner::run(), that function creates a PHPUnit_Framework_TestResult object in which the results of the tests will be stored, and it attaches to it a listener, which implements the interface PHPUnit_Framework_TestListener. This listener handles generating any output or performing any notifications based on the test results.

    To help you make sense of this, here is a simplified version of PHPUnit_TextUI_TestRunner::run(), myTestRunner(). MyTestRunner() executes the tests identically to TextUI, but it lacks the timing support you may have noticed in the earlier output examples:

    require_once "PHPUnit/TextUI/ResultPrinter.php";
    require_once "PHPUnit/Framework/TestResult.php"; 
    function myTestRunner($suite) 
    {
    $result = new PHPUnit_Framework_TestResult;
    $textPrinter = new PHPUnit_TextUI_ResultPrinter;
    $result->addListener($textPrinter);
    $suite->run($result);
    $textPrinter->printResult($result);
    }

    PHPUnit_TextUI_ResultPrinter is a listener that handles generating all the output we've seen before. You can add additional listeners to your tests as well. This is useful if you want to bundle in additional reporting other than simply displaying text. In a large API, you might want to alert a developer by email if a component belonging to that developer starts failing its unit tests (because that developer might not be the one running the test). You can write a listener that provides this service:

    <?php
    require_once "PHPUnit/Framework/TestListener.php";
    class EmailAddressListener implements
    PHPUnit_Framework_TestListener { public $owner = "develepors@example.foo"; public $message = ''; public function addError(PHPUnit_Framework_Test
    $test, Exception $e) { $this->message .= "Error in
    ".$test->getName()."\n"; $this->message .= "Error message:
    ".$e->getMessage()."\n"; } public function addFailure(PHPUnit_Framework_Test
    $test, PHPUnit_Framework_AssertionFailedError
    $e) { $this->message .= "Failure in
    ".$test->getName()."\n"; $this->message .= "Error message:
    ".$e->getMessage()."\n"; } public function startTest(PHPUnit_Framework_Test
    $test) { $this->message .= "Beginning of test
    ".$test->getName()."\n"; } public function endTest(PHPUnit_Framework_Test
    $test) { if($this->message) { $owner =
    isset($test->owner)?$test->owner:$this->owner; $date = strftime("%D %H:%M:%S"); mail($owner, "Test Failed at $date",
    $this->message); } } } ?>

    Remember that because EmailAddressListener implements PHPUnit_Framework_TestListener (and does not extend it), EmailAddressListener must implement all the methods defined in PHPUnit_Framework_TestListener, with the same prototypes.

    This listener works by accumulating all the error messages that occur in a test. Then, when the test ends, endTest() is called and the message is dispatched. If the test in question has an owner attribute, that address is used; otherwise, it falls back to developers@example.foo.

    To enable support for this listener in myTestRunner(), all you need to do is add it with addListener():

    function myTestRunner($suite)
    {
    $result = new PHPUnit_Framework_TestResult;
    $textPrinter = new PHPUnit_TextUI_ResultPrinter;
    $result->addListener($textPrinter);
    $result->addListener(new EmailAddressListener);
    $suite->run($result);
    $textPrinter->printResult($result);
    }

    Please check back next week for the conclusion of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Advanced PHP Programming," published by...
       · Not bad article if you need to test your PHP *classes* but what about approaches on...
     

    Buy this book now. This article is excerpted from chapter 6 of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616). Check it out today at your favorite bookstore. Buy this book now.

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...





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