PHPBuilder - Advanced Debugging in PHP



RSS Twitter
Articles Application Architecture

Advanced Debugging in PHP

by: Kaushik Pal
|
February 27, 2014

Overview

Debugging is one of the most important and significant parts of software development. Finding bugs and errors in any application is a tedious task, so a proper debugging mechanism should be adopted to make the process easier. Without following proper debugging procedures, software bugs and errors can be difficult to locate. As per the PHP programming model, we should be aware of all the tools available for debugging and eliminating malfunctioning components from our software system. In this article, we will concentrate on the core PHP debugging process. The core mechanism consists of using the program to trace errors and discrepancies. The output is gathered through the script execution and displayed at the end of the script.

 

Introduction

Normally when we work on software development process, we have to follow Software Development Life Cycle (SDLC) guidelines. The SDLC process consists of different types of steps and one of them is the debugging and testing process. Debugging is an essential part of any software development work.

 

When we develop a software project, it invariably contains different types of bugs, errors or abnormalities. Debugging is the process of checking, detecting and correcting errors or bugs to allow software programs to function properly.

 

General Trouble Shooting Strategies

Basic elements of a debugging effort consist of guessing the problem and then fixing it. It does not matter whether we are analyzing an PHP program, a telephone switch, an electronics circuit, or a software program -- certain principles apply, regardless. So we should bear these ideas in mind as we try to figure out what ails our software.

 

 

The following assumes that you are familiar with the concept of unit testing, as well as the PHP web development language. It is a guide for a new user with the Simple Test process. If you are new to the unit testing process then it will also help you follow the steps.

 

Quick Testing Procedure

A unit testing tool is one of the most popular software testing tools in the development community. In the context of Agile development, the test code (which is required for testing) exists right next to the application source code as both are written simultaneously. In this article the simple test aims to be a complete PHP developer test solution and it is called "Simple" because it should be easy to practice.

 

Now we are testing a simple file logging class called Log in classes/logging.php. We can start by creating a test script which we will call tests/logging_test.php and colonize it as follows.

 

Listing 1: Sample showing files logging class

<?php
require_once('simpletest/autorun.php');
require_once('../classes/log.php');
class Loggingtester extends TestCase {
}
?>

 

Here the "simpletest" folder is either confined or in the path. We would have to edit these locations depending on where we chose to unpack the tool. Here the autorun.php file includes the SimpleTest files and also runs our test. The TestOfLogging is our first test case and it is currently empty. Here, each test case is a class that extends one of the SimpleTest base classes and we can have as many of these in the file as we want. Now we have a test suit with a Log class included.

 

For our first testing process, we will accept the Log class which takes the file name to write into the constructor, and place it in a temporary folder.

 

Listing 2: Sample showing test case and log

<?php
require_once('simpletest/autorun.php');
require_once('../classes/logging.php');

class Loggingtester extends TestCase {
    function checkFirstMessage() {
        @unlink('/temp/test.log');
        $logging = new Log('/temp/test.log');
        //This is a pointer that located incurrent object
        $this->assertFalse (file_exists('/temp/test.log')); 
        $logging->message ("should write this to a file");
        $this->assertTrue (file_exists('/temp/test.log'));
    }
}
?>

 

When we run a test case, it will search for any method that starts with the string "test" and execute that method.

 

A very long name such as "checkFirstMessage ()" follows good style and makes the test output more easy to read. We would normally add one test method in a test case, but that is for later. Assertions within the test methods trigger messages to the test framework which displays the result instantaneously. This instantaneous response is very important. The print statements can display debugging content right next to the assertion concerned.

 

To see these results we have to actually run the tests. No other code is necessary -- we can just open the page with our browser.

 

Logging System Testing Process

Here we have given an example when the system fails.

 

 

Definition of a Fatal Error

When an error occurs, it causes unexpected termination of the program. The program may be dismissed either by itself or by the operating system as a fatal exception. In the prior case, the program contains code which catches the error and returns it back to the operating system or calls an operating system service to terminate the program. Basically that procedure is generally called fatal error.

 

An example of fatal error is shown below.

 

Fatal error: Failed opening required '../classes/logging.php' (include_path='') in /home/bcei/projects/lastcraft/article_tests/Log/tests/logging_test.php on line 7

 

It means we are missing the classes/Logging.phpfile that could look like this.

 

Listing 3: Sample showing logging class

<?php
class logging 
{
    	function logging($file_path)
	{
		……….
    	}
    	function message() 
	{
		echo "Welcome to the world of advance PHP……";
    	}
}
?>

 

The process is called Test Driven Development (TDD) and it is a proven development methodology.

 

Building Test Suites

In a real life application it is not possible to run just one test case. But generally we find a way to group the required test cases into a test script. Now we can run the test script to check the application. The advantage is that we do not need to run individual test cases which are very tedious and time consuming. And at the same time, if we do not need all the test cases, we can simple comment some of them out and run the script.

 

Our first step is to create a new file called tests/cei.php and then insert the following code.

 

Listing 4: Sample showing test script

<?php
require_once('simpletest/autorun.php');
class cei extends bcei 
{
    	function alltest() 
	{
        	$this->TestSuite('All testing Process..');
        	$this->addFile('log_test.php');
    	}
}
?>

 

Conclusion

Debugging is one of the most important processes in any software application development. It is essential to test any application before it goes out because deployment is the final part of any software development life cycle. However, it is also true that debugging is a must during development and testing of any project. In this article, we have discussed various types of tools and procedures followed in advanced PHP debugging and testing process.

 

 

About the Author

Kaushik Pal is a technical architect with 15 years of experience in enterprise application and product development. He has expertise in web technologies, architecture/design, java/j2ee, Open source and big data technologies. You can find more of his work at www.techalpine.com and you can email him here.

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Kaushik Pal

Comment:



Comment:

(Maximum characters: 1200). You have characters left.