18 - Testing and debugging in PHP applications

Importance of testing and debugging

Testing and debugging phase in development process of any application is very important, because in this phase it is ensured that the code written will function as per expectation. Now testing phase is a part of programming also and in software development process, it begins from the first phase of software development life cycle. It is good approach since it avoids complexities by debugging errors at the last stage. Testing phase is used in SDLC to enhance productivity and consistency in application that will result in great customer satisfaction.

How to debug PHP applications

To debug PHP applications, various strategies can be used for example we can use turning on error reporting option in Apache and PHP, use print statements in PHP code to locate the source more difficult bug through PHP script. We can use PHP eclipse plug in also for the debugging of real time PHP applications. PHP eclipse is an eclipse plug-in that is capable to highlight all syntactical errors, we can also use breakpoints in eclipse.

Error messages can be used to debug application in development mode. Developers can set error reporting to turn on so that error messages can be displayed. Error messages display exact lines where the error is occurred, and it is helpful for debugging the application.

Error reporting should be turned off in production mode, because error messages should not be visible to end users.

Configuration settings in PHP

In PHP.ini file, we can configure settings for error reporting purposes:

Following are the default values of these settings:

    Display_errors=off
    Error_reporting=E_ALL

 

    Display_errors=on

This setting can be used to display errors in development mode of application.

Error_reporting=E_ALL setting will display all types of errors. We must configure this setting as:

    Error_reporting=E_ALL & ~E_NOTICE

Following is the configuration settings in PHP.ini file:

Fig - The configuration settings in PHP.ini file

 

Common error level constants:

These directive controls are very useful during development mode, but in production mode these directives could be very critical, it depends on the code which will be trigger at the time of error, sensitive information could not be displayed to end users, so it is recommended that errors be logged on production server rather than sending errors to STDOUT.

Possible values for this are:

Off: it do not display any error

Stderr: it display error to STDERR

On or stdout: it display errors to STDOUT

PHP start up error

These types of errors should be handled separately from display errors. These errors if turn on can be useful in debugging environment. It is recommended that we should off this directive on production server.

Default value of this directive: off

Development value: On

Production value: off

Apart of displaying errors PHP can also log errors to specific locations such as server specific log, STDERR or any other location specified by error_log directive.

Normally we don’t display errors in production environment, even then errors can be monitored and can be logged by setting log_error directive as:

       Log_error=on

Apart of these configuration settings, in PHP user defined error handler function can also be used. For this we should use set_error_handler() function. This function can be used to define user defined function to handle error at run time.

Trigger_error: This function is used to generate user level error, warning or notice message in PHP applications.

Error reporting configuration in Web server

After the configuration settings in PHP. We must configure web server apache also so that the error levels can be corrected. For the same we should configure http.conf file of apache as:

      php_flagdisplay_errors on
      php_valueerror_reporting 2039

2039 stands for E_ALL  & ~E_NOTICE

2037 will stands for E_ALL

Following is the configuration setting in httpd.conf file of apache regarding log_errors in PHP application:

Fig - The configuration setting in httpd.conf file

Debugging and testing tools in PHP

To debug PHP applications, various tools are available that a developer can choose to debug:

  • Cucumber
  • PHPUnit
  • SimpleTest
  • XDebug
  • Firebug
  • FirePHP
  • Watir
  • Selenium
  • Human-powered testing with UserTesting.com
  • Bug tracking with Bugzilla

 

Like us on Facebook