Archive for the ‘Development’ Category

Backtrace in PHP

without comments

I have been developing in PHP from the last 4-5 years. Since the introduction of OOP concepts in PHP5, the language is getting matured and more ready for large applications.

While working with large projects, one can not avoid logging for error / debug to later diagnose the issues one might face in production. So beside logging some general information and simple errors which PHP throws and we could get from PHP we could also capture the whole backtrace for the code we are running in such case.

It could be easily incorporated in your application. Follow are some modified examples from PHP’s documentation debug_print_backtrace:

//This is backtrace_include.php

//This function modifies the output which PHP provides for debug_print_backtrace
//by dany dot dylan at gmail dot com
function debug_string_backtrace()
{
	ob_start();
	debug_print_backtrace();
	$trace = ob_get_contents();
	ob_end_clean();

	// Remove first item from backtrace as it's this function which
	// is redundant.
	$trace = preg_replace ('/^#0\s+' . __FUNCTION__ . "[^\n]*\n/", '', $trace, 1);

	// Renumber backtrace items.
	$trace = preg_replace ('/^#(\d+)/me', '\'#\' . ($1 - 1)', $trace);

	return $trace;
}

function a()
{
    return b();
}

function b()
{
    return c();
}

function c()
{
    return debug_string_backtrace();
}

$trace = a();
echo $trace;

Save the above file as backtrace_include.php and call in the following file:

//This is backtrace_test.php
include 'backtrace_include.php';

This will print the following:

#0  c() called at [/home/www/backtrace_include.php:29]
#1  b() called at [/home/www/backtrace_include.php:24]
#2  a() called at [/home/www/backtrace_include.php:37]
#3  include(/home/www/backtrace_include.php) called at [/home/www/backtrace_test.php:3]

Usually in production websites, we don’t print such trace on screen and log this in a file. We will also log such information only in a situation when an error occurs while executing the code.

Written by admin

July 15th, 2009 at 10:42 am

Posted in Development, Testing

Tagged with , , ,

Array comparison in PHP

without comments

While working on a recent project, I had to do some array comparison. I did array comparison by doing the following:

$firstArr = array(2,4,8);
$secondArr = array(2,8,4);

According to my requirements the above two arrays are same but if we compare it in PHP, it would not think so due to the presence of value on different indexes. To solve that, I first sorted and then compared these arrays, which gave me the required result.

//sort the arrays using php's sort function
sort($firstArr);
sort($secondArr);

//compare the arrays
if ( $firstArr === $secondArr)
{
	echo "Yes. They are equal";
}
else
{
	echo "No. They are not.";
}

Written by admin

July 9th, 2009 at 10:27 am

Posted in Development

Tagged with ,

Mysql backup and delete older files

without comments

We were taking mysql backups using a very simple shell script which was using mysqldump as below.

#!/bin/sh
#echo "Starting the script";
HOST=localhost
USER=myDBUser
PASSWD='myPassword';

# create directory if it doesn't exist
OLDBACKUP=/home/my_backup/sql_files
if [ ! -d "$OLDBACKUP" ]; then
# create direcotry
mkdir /home/my_backup/sql_files
fi

# set file names by appending date to each file
dbName=dbName-`date -I`.sql;

# take db backups
mysqldump -u $USER -p$PASSWD -n -c myDbName >  $dbName;

# move the file to sql_files directory
mv $dbName $OLDBACKUP

We were keeping old backup files but we knew that we would need to remove the backup files older than two weeks or a month. I was thinking what to do and after some studying I found the following one line, would help us remove files older than x number of days.

find /home/my_backup/sql_files -type f -mtime +15 | xargs rm

Where 15 could be changed to any number of days and it will remove all the files older than that number of days recursively, using this command. This command won’t delete any special files or sub directories.

Written by admin

May 22nd, 2009 at 1:00 pm

Posted in Development

Tagged with , , , , ,

Sahi Helped Us

without comments

Sahi helped us automate our web application testing by 60%-70%. Till now I am quite happy with it.

Written by admin

May 7th, 2009 at 11:45 pm

Posted in Development, Testing

Tagged with , ,

PHP’s empty and isset function

without comments

The best way to check if a variable consists any value or is empty is to use empty function from PHP itself.

$myVar = 0;
if (empty($myVar))
{
echo '$myVar is either 0, empty, or not set at all';
}

Though, while coding in PHP it shouldn’t be confused with isset which checks if a variable is set or not set, whether that variable is empty.

$myVar = 0;
if (isset($myVar))
{
echo '$myVar is set even though it is empty';
}

Written by admin

January 16th, 2009 at 11:11 am

Posted in Development

Tagged with ,

Domain Specific Language QA

without comments

An interesting question and answers about DSL (Domain Specific Language) with Martin Fowler.

What is a Domain Specific Language?
A Domain Specific Language (DSL) is a computer programming language of limited expressiveness focused on a particular domain. Most languages you hear of are General Purpose Languages, which can handle most things you run into during a software project. Each DSL can only handle one specific aspect of a system.

Read complete question and answers here.

Written by admin

September 10th, 2008 at 9:38 am

Posted in Development

Tagged with , , ,

Final Project For CS201

without comments

Our final term for this semester is going to start in the last week of August. I am also working on the final project for my course CS201. The project I am working on is a reservation system for a bus company. It is required to have:

  • New Reservation
  • Update Reservation
  • Delete Reservation
  • Search Reservation

The deadline for this project is 9th August. So I will be busy in this and will try to post the source code, once I get the marking on this project.

I am writing this project in C/C++ and using notepad++ for writing C/C++ code.

Written by admin

August 7th, 2008 at 12:24 pm

Posted in Development, Personal

Tagged with , ,

Testing Automation Setup

without comments

On a continuation to my previous post regarding Testing Automation Tools, I will try to write the process through which I went while writing the testing scripts.

The application for which I wanted to do the automated testing is a web based application written in PHP on top of Zend Framework. As Zend Framework follows the MVC pattern, we followed the same pattern. For database, we wrote our own layer and didn’t use the layer which Zend Framework provided.

For testing automation I followed the same design rules, which I followed while designing and writing this application. I wrote testing scripts for each entity in their own files. Then I called them in one main file, which I run when I want to run the regression test. Following is an example configuration file:

//include all files, which are required to run the test cases
_include("webadmin_login.sah");
_include("webadmin_company.sah");
_include("webadmin_user.sah");
_include("webadmin_group.sah");
_include("webadmin_homepage.sah")
_include("webadmin_sysconfig.sah")
//global variables
_setGlobal("companyname", "Test_1");

// login
login("admin", "admin");

// create company info
createCompany("Test_1");
createUser("test1_user1");
createUser("test1_user2");
createUser("test1_user3");
createUser("test1_user4");
createGroup("test1_group");

// homepage
startAll();
stopAll();
refresh();

// logout
logout();

I treat this main script as my main configuration file. Where I declare some global variables, call those scripts which I need to run during my test run. I can include the scripts, which I need to run and can exclude those scripts which I don’t need.

I will continue with this series with further updates on how I wrote automated tests for every individual entity.

Written by admin

July 31st, 2008 at 11:20 am

Testing Automation Tools

with 3 comments

I recently started using Sahi. Sahi is an automation and testing tool for web applications, with the facility to record and playback scripts.

When I was trying to find any tool for testing automation, I evaluated many tools as the following:

  • AutomatedQA’s TestComplete – It is one of the other product which I would love to use. It is an excellent product and could be used very nicely for GUI applications. I had to drop this from the list due to its price :( and availability of free / open source automation tools for web.
  • Watir – I couldn’t try this much but it was dropped from the list due to the learning curve involved in learning Ruby and then using this whole suite efficiently. Though, it seems to be a nice product and offers many add-ons.
  • Sahi – I chose this due to the ease of use. Another positive aspect of this application is that its scripting is done in Javascript. So I didn’t have to learn another language. I was able to start actually automating my test cases the next day, when I installed Sahi on my system. Till date, I am able to automate about 50% of my application. That application testing, I am going to automate has been written in Zend framework.

In the next posts, I will try to post some more details about my testing experience.

Written by admin

July 24th, 2008 at 12:45 pm

Posted in Development, Testing

Tagged with , ,