Hack83.Test Your Application with Robots


Hack 83. Test Your Application with Robots

Use the HTTP_Client PEAR module to test your PHP application through the Web.

How do you know that your application is running properly? It's a lot like that little light in your refrigerator; if you can't see it, can you really be sure it's off when you close the door? One way to keep an eye on your application is to build a robot that tests your site. You can run this robot periodically, ensuring the server is always responding properly (and notifying you when it's not).

This hack shows how to use the HTTP_Client PEAR module [Hack #2] to test the shopping cart application [Hack #66]. Figure 8-5 illustrates the robot.php script driving the shopping cart application, all through requests to the web server. The robot checks the contents of each return page in the application, making sure that the process for adding and removing items from the shopping cart works properly.

Figure 8-5. The robot testing the shopping cart application through the web server


8.6.1. The Code

Save the code in Example 8-10 as robot.php.

Example 8-10. The test robot
 <?php require_once 'HTTP/Client.php'; function check_html( $testname, $client, $values ) {   $resp = $client->currentResponse();   $body = $resp['body'];   preg_match( "/\<\!\-\- CART \: (.*?) \-\-\>/", $body, $found );   print "$testname: ";   print ( $found[1] == join(",", $values ) ) ? "passed" : "failed";   print "\n"; } $client = new HTTP_Client(); $client->get( "http://localhost/phphacks/shopcart/index.php" ); $client->post( "http://localhost/phphacks/shopcart/add.php", array( 'prod_id' => 1 ) ); $client->get( "http://localhost/phphacks/shopcart/index.php" ); check_html( "Add one", $client, array( 1 ) ); $client->post( "http://localhost/phphacks/shopcart/add.php", array( 'prod_id' => 2 ) ); $client->get( "http://localhost/phphacks/shopcart/index.php" ); check_html( "Add two", $client, array( 1, 2 ) ); $client->post( "http://localhost/phphacks/shopcart/add.php", array( 'prod_id' => 3 ) ); $client->get( "http://localhost/phphacks/shopcart/index.php" ); check_html( "Add three", $client, array( 1, 2, 3 ) ); $client->get( "http://localhost/phphacks/shopcart/checkout.php" ); check_html( "Checkout", $client, array( 1, 2, 3 ) ); $client->post( "http://localhost/phphacks/shopcart/delete.php", array( 'ids[]' => 2 ) ); $client->get( "http://localhost/phphacks/shopcart/checkout.php" ); check_html( "Remove two", $client, array( 1, 3 ) ); ?> 

The code starts by creating a new HTTP_Client PEAR object. From there it does a series of GET and POST requests that simulate a user's transaction with the system as if there were a browser, but there is no browser. The check_html function is used to check the return page to see the contents of the current shopping cart. The contents of the cart are encoded in a special HTML comment that is embedded in both the index.php page and the checkout.php page. This comment has a comma-separated list of the IDs of the items in the cart. The robot expects that when it POSTs the ID of a product to the add.php page, it will see that ID when it comes back to the index.php page, and so on. If the robot doesn't see the cart items it expects, it will signal an error in the test.

8.6.2. Running the Hack

This hack starts with installing and running the shopping cart application [Hack #66]. With the shopping cart installed, you can test it automatically with this robot. Run the robot with the command-line PHP interpreter:

 % php robot.php Add one: passed Add two: passed Add three: passed Checkout: passed Remove two: passed 

This shows that each step the robot took returned responses that matched what the robot was looking for. It's also worth noting that the pages of the shopping cart are designed to be robot tested; each page has a comment embedded in it that is invisible to the end user, but is picked up by the robot. The robot reads the comment, which contains the current contents of the cart, and compares it to the value it's programmed to expect.

8.6.3. Hacking the Hack

Robots can also be used as unit tests [Hack #79]. Start by installing the PHPUnit2 test framework, available from PEAR [Hack #2]. Then use the code in Example 8-11 for the unit test.

Example 8-11. A PHPUnit version of the robot code
 <?php require_once 'HTTP/Client.php'; require_once 'PHPUnit2/Framework/TestCase.php'; class RobotUnit extends PHPUnit2_Framework_TestCase {   var $client = null;   private function check_html( $testname, $values )   { $resp = $this->client->currentResponse(); $body = $resp['body']; preg_match( "/\<\!\-\- CART \: (.*?) \-\-\>/", $body, $found ); return ( $found[1] == join(",", $values ) ); } function test1() { $this->client = new HTTP_Client(); $this->client->get( "http://localhost/phphacks/shopcart/index.php" ); $this->client->post( "http://localhost/phphacks/shopcart/add.php", array( 'prod_id' => 1 ) ); $this->client->get( "http://localhost/phphacks/shopcart/index.php" ); $this->assertTrue( $this->check_html( "Add one", array( 1 ) ) ); } function test2() { $this->client = new HTTP_Client(); $this->client->get( "http://localhost/phphacks/shopcart/index.php" ); $this->client->post( "http://localhost/phphacks/shopcart/add.php", array( 'prod_id' => 1 ) ); $this->client->get( "http://localhost/phphacks/shopcart/index.php" ); $this->assertTrue( $this->check_html( "Add one", array( 1 ) ) ); $this->client->post( "http://localhost/phphacks/shopcart/add.php", array( 'prod_id' => 2 ) ); $this->client->get( "http://localhost/phphacks/shopcart/index.php" ); $this->assertTrue( $this->check_html( "Add two", array( 1, 2 ) ) ); } function test3() { $this->client = new HTTP_Client(); $this->client->get( "http://localhost/phphacks/shopcart/index.php" ); $this->client->post( "http://localhost/phphacks/shopcart/add.php", array( 'prod_id' => 1 ) ); $this->client->get( "http://localhost/phphacks/shopcart/index.php" ); $this->assertTrue( $this->check_html( "Add one", array( 1 ) ) ); $this->client->post( "http://localhost/phphacks/shopcart/add.php", array( 'prod_id' => 2 ) ); $this->client->get( "http://localhost/phphacks/shopcart/index.php" ); $this->assertTrue( $this->check_html( "Add two", array( 1, 2 ) ) ); $this->client->post( "http://localhost/phphacks/shopcart/add.php", array( 'prod_id' => 3 ) ); $this->client->get( "http://localhost/phphacks/shopcart/index.php" ); $this->assertTrue( $this->check_html( "Add three", array( 1, 2, 3 ) ) ); $this->client->get( "http://localhost/phphacks/shopcart/checkout.php" ); $this- >assertTrue( $this->check_html( "Checkout", array( 1, 2, 3 ) ) ); $this->client->post( "http://localhost/phphacks/shopcart/delete.php", array( 'ids[]' => 2 ) ); $this->client->get( "http://localhost/phphacks/shopcart/checkout.php" ); $this->assertTrue( $this->check_html( "Remove two", array( 1, 3 ) ) );   }  }  ?> 

This new version of the code encapsulates the robot tests into methods within a PHPUnit class. The tests are exactly the same as before, with each test running more and more of the tests until the third method runs the entire test. The reason for the repeats is to check each section of the functionality individually. For example, if the first and second tests pass and the third fails, you know it has something to do with the checkout.php page, since that page is checked in the third test.

Run the unit test using the phpunit test runner application:

 % phpunit RobotUnit.php PHPUnit 2.2.1 by Sebastian Bergmann. … Time: 1.5706360340118 OK (3 tests) 

The report shows that three tests were run, and they all turned out OK. The three testsnamed test1, test2, and test3each run the robot with an increasing number of requests.

This kind of unit test is ideal for testing a complete system in operation (as opposed to a very small portion of an application). When a system is initializedafter the database is loaded and the pages are in placeyou could run this unit test to confirm that all of the pages are responding properly before the system is moved into deployment.

8.6.4. See Also

  • "Create a Shopping Cart" [Hack #66]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net