O'Reilly logo

PHP Hacks by Jack D. Herrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

The robot testing the shopping cart application through the web server

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

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"; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required