#native_company# #native_desc#
#native_cta#

Testing Forms in PHP with SimpleTest

By W. Jason Gilmore
on March 2, 2011

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", "[email protected]");
  $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.");

}