TechAlpine – The Technology world

How to debug in advanced PHP?

 

Debug in advanced PHP

Debug in advanced PHP

Overview: Debugging is one of the most important and significant part of software development. Finding bugs and errors in any application is a tedious task for the developers. So, proper debugging mechanism should be adopted to make the process easier. Without following proper debugging process, software bugs and errors can not be detected easily and efficiently. As per 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 the process of using the program to trace errors and discrepancies. The out-put is gathered through the script execution and displayed at the end of the script.

Introduction: PHP stands for PHP- Hypertext Preprocessor. Normally when we work on software development process, we have to follow Software Development Life Cycle, which is also called SDLC (Software Development Life Cycle). In the SDLC process there are different types of steps present and one of them is debugging and testing process. The debugging procedure is an essential part of any software development work.

Define bug and debug: When we develop a software project, it contains different types of bugs, errors or abnormalities. These types of errors are generally called Bug. On the other hand debugging is the process of checking, detecting and correcting errors or bugs to allow software programs function properly.

General trouble shooting strategies:

Basic elements of a debugging effort are guessing out what is wrong and then fixing it. It does not matter whether we are analyzing a 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.

  1. Change one thing at a time
  2. Try to isolate the problem
  3. Simplify, then build up
  4. Check the obvious
  5. Document our solution
  6. After fixing, retest

Advanced debugging PHP through Simple Test:

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 unit testing process then also it will 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 and spread. An example will show that a user writing to a database after signing up through the web site.

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.

Listing1: Sample showing files logging class

[CODE]

<?php

require_once(‘simpletest/autorun.php’);

require_once(‘../classes/log.php’);

class Loggingtester extends TestCase {

}

?>

[/CODE]

Here the “simpletestfolder 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 class and we can have as many of these in the file as we want. Now we have a test suit with 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.

Listing2: Sample showing test case and log
[CODE]

<?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’));

}

}

?>

[/CODE]

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

The point to be remembered is the very long name like checkFirstMessage (). This is measured as a good style and makes the test output more readable. 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 their 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.

On failure the display looks like this…

Logging system testing process:

This is the part of the testing through the following way. Here we have given an example when the system fails.

If it fails then it will look like this.

1/1 test cases complete. 1 passes and 1 fails.

If it passes then it will look like this.

1/1 test cases complete. 2 passes and 0 fails.

So the GREEN and RED color also indicates the success and failure.

Definition of 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.php file that could look like this.

Listing3: Sample showing logging class

[CODE]

<?php

class logging

{

function logging($file_path)

{

……….

}

function message()

{

echo “Welcome to the world of advance PHP……”;

}

}

?>

[/CODE]

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

Building test suites:

In real life application it is not possible to run just one test case. But generally we follow 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 then and run the script.

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

Listing4: Sample showing test script

[CODE]

<?php

require_once(‘simpletest/autorun.php’);

class cei extends bcei

{

function alltest()

{

$this->TestSuite(‘All testing Process..’);

$this->addFile(‘log_test.php’);

}

}

?>

[/CODE]

Conclusion: Debugging is one of the most important processes to any software application development. It is essential to test any application before it goes for deployment. 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.

 

Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *


8 × = 24

TechAlpine Books
-----------------------------------------------------------