PHPBuilder - 10 Productive PHP Tools for Testing and Debugging



RSS Twitter
Articles Site Operation

10 Productive PHP Tools for Testing and Debugging

by: W. Jason Gilmore
|
November 17, 2010

I've been perched atop the testing and debugging soapbox lately, having written several tutorials introducing great utilities such as PHPUnit and FirePHP. Perhaps more accurately, I've been focusing on productivity enhancement, because the true measure of productivity is based not on the amount of code written in any given day, but rather on the amount of code written that we know will function as expected. Tools such as PHPUnit and FirePHP can go a very long way towards helping you make this determination.
Yet despite the general notion that programming is a profession steeped in discipline and rigor, members of the programming community tend to be rather finicky folks who can be very particular when it comes to tooling. Thankfully PHP developers have a great deal to choose from when it comes to testing and debugging tools, and in this article I'll highlight 10 of my favorites.

1. Cucumber

I recently attended a local Ruby usergroup meeting, and was treated to an introduction to the Cucumber behavior-driven development tool. Cucumber allows you to write tests in "plain English" (or any of 30 other spoken languages) and then execute them in conjunction with a third-party tool such as Watir, Selenium, or Mechanize to execute the tests within a browser.
For instance, check out this Cucumber test (part of the Cucumber example suite), which is intended to confirm the results of a search executed through the Google search engine:
Feature: Search
  In order to learn more
  As an information seeker
  I want to find more information

  Scenario: Find what I'm looking for
    Given I am on the Google search page
    When I search for "cucumber github"
    Then I should see
      """
      GitHub
      """
Although originally written for testing Ruby on Rails applications, Cucumber can be used with many mainstream programming languages, including Java, Python and PHP. Learn more about how to configure Cucumber to test your PHP-driven websites here.

2. PHPUnit

Test-driven development (TDD) is all the rage these days, and for good reason. By elevating the role of testing -- insomuch that tests are developed alongside the application code rather than after much of the application code has already been written -- developers are constantly encouraged to think about the different ways in which errors or unexpected outcomes can occur. As a result, they accordingly write tests to ensure their application doesn't succumb to those errors.
The PHPUnit testing framework offers PHP developers a fantastic solution for doing TDD, offering a rigorous solution for writing, organizing and automating tests. Incorporating such a tool into your daily development routine can result in enormous productivity increases by saving you the hassle of having to test features such as the user login process manually. For instance, the Zend Framework offers support for PHPUnit, allowing me to write tests that can automate testing of the login process, as demonstrated here:
public function testValidLoginShouldAuthenticateSessionAndRedirectToHomePage()
{

  $this->request->setMethod('POST')
       ->setPost(array(
           'email' => 'wj@example.com',
           'pswd'  => 'secret',
           'public' => 0
         ));

  $this->dispatch('/account/login');
       
  $this->assertController('account');
  $this->assertAction('login');

  $this->assertRedirectTo('/');

  $this->assertTrue(Zend_Auth::getInstance()->hasIdentity());

}
This test will confirm that the account controller's login action can accept a valid email address and password, authenticate the account, redirect the user to the site's homepage, and finally confirm that a valid user session exists.
To learn more about PHPUnit, check out the official website, and read the PHPBuilder article, Use PHPUnit to Implement Unit Testing in Your PHP Development .

3. SimpleTest

SimpleTest is another great automated testing solution, which like PHPUnit allows developers to write unit tests using familiar PHP syntax. Additionally, SimpleTest offers several features not natively available to PHPUnit (although these features can be used with PHPUnit in conjunction with third-party tools) such as Web interface testing, including the ability to test authentication and Web forms.
For instance, the following SimpleTest test will navigate to the Google home page, insert the query "PHP" into the search field, and submit the form by clicking the Google Search button:
class SimpleFormTests extends WebTestCase {
  function testDefaultValue() {
    $this->get('http://www.google.com/');
    $this->setField('q', 'PHP');
    $this->click('Google Search');
  }
}
Although not as well known as PHPUnit, SimpleTest's user-friendly approach to testing continues to earn the framework accolades among many members of the PHP community. For this reason, if you're a relative novice to programming and feel particularly intimidated by the various testing frameworks, SimpleTest may be well worth a look.

4. XDebug

Much of the time you spend testing and debugging will involve grunt work that requires you to inspect the state of your application at various stages of execution. Typically accomplished using native PHP statements such as echo and var_dump, reviewing the contents of an object can quickly become a messy affair due to the lack of coherent formatting when the object contents are output to the browser.
The XDebug PHP extension can greatly enhance the readability of this data by changing how array and object contents are output to the browser. Consider the improvements when reviewing even a simple array such as this:
<?php

$sports = array();
$sports[] = "Football";
$sports[] = "Baseball";
$sports[] = "Basketball";

?>
For output using var_dump, the array contents will look like this:
array(3) { [0]=> string(8) "Football" [1]=> string(8) "Baseball" [2]=> string(10) "Basketball"}
However, with XDebug enabled, the array contents will appear like this:
array
  0 => string 'Football' (length=8)
  1 => string 'Baseball' (length=8)
  2 => string 'Basketball' (length=10)
Installed in mere minutes, XDebug is one of those tools that quickly will make you wonder how you ever got along without it. For more information about XDebug, be sure to visit the official website and read the article A Sanity-saving Debugging Solution for Your PHP Development.

5. Firebug

These days, creating great websites isn't simply a function of understanding a server-side language, with even the simplest of websites are driven by a symphony of HTML, CSS, JavaScript and a language such as PHP. Therefore, your ability to test and debug all facets of the website effectively is going to weigh heavily on your productivity. And for such purposes, there are few tools more capable than Firebug, a Firefox extension that you can use to inspect and manipulate every conceivable characteristic of a Web page.
Whether you want to see what your website looks like with all images disabled, experiment with CSS-driven font sizes, or easily measure the dimensions of a DIV using a built-in ruler, Firebug contains a seemingly endless array of useful features capable of answering any question you may have regarding your website's operation.
For more information about Firebug, see the official home page, and be sure to check out the Developer.com article Firebug: Add Browser-based Debugging to Your Ajax Development.

1
|
2
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
W. Jason Gilmore

Comment:



Comment:

(Maximum characters: 1200). You have characters left.