Encouraging best practices within the PHP community through the adoption of powerful tools such as Phing, PHPUnit and FirePHP was a major focus of my 2010 contributions to Developer.com and PHPBuilder.com. Much to my delight, interest in these articles was so strong that I'll be regularly returning to more productivity-boosting utilities throughout 2011, starting with the SimpleTest unit and Web testing framework.
Although not as well known as PHPUnit, SimpleTest is an immensely capable framework, having been adopted by popular PHP projects including CakePHP and Drupal. In addition to offer a complete suite of unit testing capabilities, SimpleTest is bundled with an internal Web browser which you can use to automate the navigation of your Web site and perform tests such as completing and submitting Web forms much in the same way a typical user would. In this tutorial, I'll show you how to use SimpleTest to automate these otherwise tedious tasks.

Installing SimpleTest

Download SimpleTest by heading over to its SourceForge page and downloading the latest stable version. Once downloaded, unzip the package to a convenient location and add its path to PHP's include_path. After restarting your Web server, you can make sure SimpleTest is accessible from a PHP script by creating a PHP script which contains the following code:
<?php
require_once('simpletest/autorun.php');
require_once('simpletest/web_tester.php');

?>
Save this file (using a name such as simpletest.php) to a convenient location within your Web server document root path and call it from within your Web browser. You should see output similar to that presented in Figure 1.

 

The SimpleTest Framework
Figure 1. Making Sure the SimpleTest Framework Is Accessible

 

If you receive a warning about not being able to find one of the SimpleTest files, the problem is almost certainly due to an incorrect include_path setting.

Confirming That a Web Page Exists

With SimpleTest installed, let's work through testing the contact form shown in Figure 2. This is a pretty standard form, consisting of three input fields, including two text fields named name and email, respectively, and a text area field named message.

 

SimpleTest Framework
Figure 2. A Typical Contact Form (contact.php)

 

You can write a test which confirms the existence of the contact form page by first accessing the page using SimpleTest's get() method, and then asserting that a 200 response code is returned. The code used to perform this task is presented next:
<?php
require_once('simpletest/autorun.php');
require_once('simpletest/web_tester.php');

class SimpleFormTests extends WebTestCase {

  function testDoesContactPageExist() {
    $this->get('http://www.example.com/contact.php');
    $this->assertResponse(200); 
  }

}

?>
Try changing the URL to a non-existent file and reload the test. Because the file doesn't exist, you'll be greeted with a failing message.

Testing a Valid Form Submission

The purpose of SimpleTest's Web testing feature is to test your PHP logic from the perspective of a typical user. Because the user will presumably receive different responses based upon whether the form submission was successful, you can use SimpleTest to determine whether the appropriate response has been provided to the user.
For instance, if all form fields have been properly completed when the form is submitted, suppose the user will be presented with the statement We will be in touch within 24 hours. You can easily test this outcome using SimpleTest. Add the following test to your test file:
function testIsProperFormSubmissionSuccessful() {

  $this->get('http://www.example.com/contact.php');
  $this->assertResponse(200);

  $this->setField("name", "Jason");
  $this->setField("email", "wj@example.com");
  $this->setField("message", "I look forward to hearing from you!");

  $this->clickSubmit("Contact us!");

  $this->assertResponse(200);
  $this->assertText("We will be in touch within 24 hours.");

}

Testing Invalid Form Submissions

Web development would be pretty easy if we were concerned only with testing valid form data. Unfortunately, users can be expected to submit all manner of improper, invalid, and even malicious data and so you'll need to write numerous tests that ensure your PHP code is properly detecting and reporting errant form input such as an invalid email address or missing name. Again when using the Web testing framework you'll be concerned with ensuring that the appropriate error messages are displayed. Once the form validation code has been added to the page you can use the following tests to ensure valid email addresses and names consisting of at least one character are included with the form submission:
function testInvalidEmailAddress() {
  $this->get('http://www.example.com/contact.php');
  $this->assertResponse(200);

  $this->setField("name", "Jason");
  $this->setField("email", "wjexample.com");
  $this->setField("message", "I look forward to hearing from you!");

  $this->clickSubmit("Contact us!");

  $this->assertResponse(200);
  $this->assertText("Please provide a valid email address.");
}

function testInvalidName() {
  $this->get('http://www.example.com/contact.php');
  $this->assertResponse(200);

  $this->setField("name", "");
  $this->setField("email", "wj@example.com");
  $this->setField("message", "I look forward to hearing from you!");

  $this->clickSubmit("Contact us!");

  $this->assertResponse(200);
  $this->assertText("Please provide your name.");
}

 

Run all of the tests and you should see output similar to that found in Figure 3.
The SimpleTest Framework
Figure 3. Comprehensive Form Testing Is Easy with SimpleTest

 

Conclusion

SimpleTest's native Web testing framework makes it ridiculously easy to automate the testing of your PHP logic from the end user's perspective. This functionality can greatly reduce the time you'll otherwise need to spend performing tedious tasks such as completing and submitting Web forms. Be sure to check out the following links for more information about Web testing!
What other Web testing tools do you recommend? Tell us about them in the comments!

About the Author

Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.