Saturday, May 7, 2011

Test Driven Development with PHP

Long time I was trying to explore if there is good unit test framework in PHP and recently got opportunity to work on for one of my projects. Its Simple Test PHP Unit Test Framework.

It has support for SSL, forms, frames, proxies and basic authentication. What makes Simple Test Framework immediately useful to the PHP developer is the internal web browser. This allows tests that navigate web sites, fill in forms and test pages. Being able to write these test in PHP means that it is easy to write integrated tests. An example might be confirming that a user was written to a database after a signing up through the web site.

There are basically two types of Test Case:

1. Unit Test Cases
a. The UnitTestCase class will typically generate test events when run and these events are sent to an observing reporter using methods inherited from UnitTestCase.
b. Autorun.php file does the expected work of pulling in the definitions of UnitTestCase. It collects all test classes in the current file and runs them automagically. It does this by setting up an exit handler.

Code Snippet:


Some of the Base Functions and their description:
- assertTrue($x) Fail unless $x evaluates true
- assertFalse($x) Fail unless $x evaluates false

2. Web Test Case

a. The WebTestCase includes facilities for navigation, content and cookie checks and form handling. Usage of these test cases is similar to the UnitTestCase.
b. Inorder to run test cases as part of a group, in this scenarion we use group testing. The test cases should really be placed in files without the runner code.
c. The setUp() method is run just before each and every test method.
d. The tearDown() is run just after each and every test method.
e. The name of the test case function is always prefixed by the word test. eg. if the test case is checklogin then you will require to
define the function as testchecklogin.
Code Snippet:
# For checking the response code of a URL
File: lastcraft_test.php


File: runlastcraft_test.php


# For checking the login functionallity


Some of the Base Functions and their description:
- getUrl() => The current location
- setMaximumRedirects($max) => Stop after this many redirects
- assertResponse($codes) => Pass if HTTP response matches this list
- assertField($name, $value) => Pass if an input tag with this name has this value
- setField($name, $value) => Sets the input tag with this name with this value
- clickSubmit($label) => Click the first button with this label
- assertText($name) => Pass if matches visible and "alt" text
- assertTitle($title) => Pass if title is an exact match

One can explore more on Simple Test PHP Unit Testing Framework Its pretty clean mechnism to follow your TDD

Sunday, April 3, 2011

Using Google Earth API and KML for your business

Although its almost 6 yrs since when Google launched its Google Map API, and I built my first app around it for entrip.com, it has been evolving lot and lots of possibilities have been created.
A new application is result of new business requirement or introduction of new technology which can be used for solving old business problems.
Next step of Google Map api is now Google earth API, as processing power and bandwidth is going cheaper its becoming more feasible and attactive to build apps around google earth. A browser plugin makes google earth api more killing and pervasive.
The Google Earth Plug-in and its JavaScript API let you embed Google Earth, a true 3D digital globe, into your web pages. Using the API you can draw markers and lines, drape images over the terrain, add 3D models, or load KML files, allowing you to build sophisticated 3D map applications. The Google Earth API is a free service, available for any web site that is free to consumers.
Google Earth provides the following functionalities that adds to the advantage of using it:

a. Google Street View
b. Google Aerial View
c. Google 3D buildings
and many more...

The main new and interesting thing google introduces is KML ( Key Hole Markup Language ).
KML is a file format used to display geographic data in an Earth browser, such as Google Earth, Google Maps, and Google Maps for mobile. You can create KML files to pinpoint locations, add image overlays, and expose rich data in new ways. KML is used for the following reasons:

a. Casual users
Description: Placemark homes, plan and document journeys.
b. Scientists
Description: See detailed mappings of natural resources and trends.
c. Non-Profits
Description: Highlight problems and advocate change
d. Students and Educators
Description: Explore historic and current places, people and events.

Using Above technologies just created a simple app which is just combination of
Goggle Earth API + KML Data + PHP + MYSQL + AJAX

Steps done taken are

1) Develope KML to locate particular co-ordinate and mark it using graphical shapes such are ring, polygon

2) Initialise the Google earth API

google.earth.createInstance('map3d', initCB, failureCB);

3) In-order to send the request to the PHP script Ajax has been implemented.

function addcityajax()
{
var data = "load=citydropdown";

$.ajax({
url: "get_kml_data.php",
type: 'GET',
data: data,
success: function(data){
//code to process kml data response
}
});
}



Some useful links
Using PHP and Mysql to generate KML
test your KML online tool

Monday, March 28, 2011

Weekly Diary of a Rails Developer

Day 1
Ruby on Rails Installtion

Rails installation and configuration
Installing rvm for ruby versions on same system.
Installing dependencies for rails,rvm and gems like iconv,mysql,hpricot,sqlite,nokogiri etc by using apt-get.
Solving issues found during installation.


Day 2
Model (ActiveRecord )

Using validations like validates_presence_of, validates_uniqueness_of, validates_size_of,validates_format_of etc.
Using association for specifying relationships in the tables using has_one,has_many, belongs_to etc.
Using named_scope to classify and access records. Include and joins are used to access data from tables and query data.

Day 3
View ( ActionView )

Using views in html and haml files.
Using haml with right indentation are important as the ending tag depends on this.
Including javascript/stylesheets in views by using the include and link tag respectively.
Using different types of ajax tags like link_to_function,form_remote_tag etc which is used to harness the power of javascript library.
Using Partials we can have the same view has to retained on multiple pages. The layout file is used to specify the background of the page which is made to be used for multiple pages or for just single pages by mentioning it on the controller as layout "filename".
Using Yield to insert contents on a particular layout.
Using Render to show the same page again with the parameters already present in the response while redirect is used to tranfer the response to a different function and then to a different view.

Day 4
Controller ( ActionController )

Using facility within the application that directs traffic, on the one hand querying the models for specific data, and on the other hand organizing that data (searching, sorting, massaging it) into a form that fits the needs of a given view.
Managing the layout in application that is to be used for the views from that controller.
Using access specifiers like public,private for methods on the controller.
Using filters on controller like before_filter and callbacks which are like after_create, before_save, after_save etc.

Day 5
Migration Scripts

Creating Rails migrations for creating tables , manipulating columns in it.
For specifying its data structure.
Creating up and down blocks for it.
Specific migration can be run and it can also be reverted using different migration command.
Migration files are created when a model is created also additional migration files can be created for inserting columns to table later on.

All Days
Working on git, gems

Using git for version management.
Creating account on git, uploading private keys on it,
Using git for cloning code from origin, making git repository,creating new branches, merging branches with master, pulling and pushing code from origin and to origin.
Using gems for additional features e.g.
seed_fu,
paperclip,
s3,
authologic,
facebooker,
twitteroauth,
fckeditor,
act_as_ferret,
will_paginate,
rmagick,
etc..
Using bundler and Maintaining differnt section in gemfile for development,production and testing.

On Weekend :)
Deploying App on Cloud (Heroku)

Using heroku for deploying application.
Creating application according to the heroku requiremeent as on heroku we have time limit for request and response.
Using amazon s3 for storing files as Heroku does not file storage on it. Creating rake tasks for that.
Using gems which supports heroku.
Using heroku commands,
e.g heroku logs, heroku rake etc.


Philosphy that Developer lives everyday
TDD(Test Driven Developement), DRY ( Do Not Repeat Yourself)

Wednesday, March 23, 2011

Rails 2 to Rails 3 Migration

Recently I got engaged with assignment to help one of my client in migrating his Rails 2 app to Rails 3. The engagement was interesting and quite demanding. I am sharing this experience which may of interest to some of you guys.
Steps taken

1) First made sure that I have rvm installed so that i can switch between ruby versions easily.

$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
Then put the following line in .bashrc. This line loads rvm on system.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
$ rvm install 1.9.2 $ rvm use 1.9.2
$ gem install rails --version=3.0.3


2) Switch to your existing system ruby verion(in my case it was ruby 1.8.7) and start migrating your existing rails 2 project. I have taken help of rails_upgrade plugin for the same

$ rvm system
$ cd myproject
$ script/plugin install git://github.com/rails/rails_upgrade.git


3) Once plugin installed then run rake tasks

# Check your app for required upgrades
rake rails:upgrade:check

# Backup your likely modified files that might be overwritten by the generator
rake rails:upgrade:backup

# Generate a new route file
rake rails:upgrade:routes

# Generate a Gemfile from your config.gem directives
rake rails:upgrade:gems

# Generate code for a new config/application.rb from your environment.rb
rake rails:upgrade:configuration

4) Now its time to go back to your backed up files and pick up your application specific code and put it in upgraded files
5) Now switch to rails 3 using rvm and start your app


$ rvm use 1.9.2
$ rails server

6) If you do not get any error then that will be surprise as you will definitely get tons of errors and its time to attack one by one

frequest type of error you will be sseing are
a) gem not installed -> please make sure that you have added entry in .Gemfile and used bundle install command
b) gem version mismatch -> find out verion that supports rails 3(ruby 1.9.2)
c) some modifications in application.rb
d) html corruption -> please use html safe functions .html_safe

7) now you need to check each page one by one and sort out any issues.

Overall its a great experience and offcourse I could do it with help of ample onlie help available on opensource communities.
I will like you not to miss following links
rails upgrate plugin
rails cast on rails 3 migration
rails cast on rails 3 routing

Sunday, June 28, 2009

Engaging web2.0 to go greener

LAMP Technology + a simple logic for radius search + thinking in web2.0 way for greener life = thevillagegreener.com
I am really happy the way people behind it made it a platform to fuel a greener lifestyle. I am proud to be part of the team and could help to realize the thoughts on technology front.
The Village Greener aims to help you find and fuel your greener lifestyle by equipping you with intelligence and opportunities for action. The Village Greener encourages those interested in making ecologically sound daily lifestyle choices to gather here — and then to share, learn and do things to live a little greener. Everyday. In small ways that add up to big change.
One may see features like rating a listing, calendar events, a website blog, having teasers for people through media like books, videos. But blending the same to give a very simple and intuitive platform for users who want to propogate their green lifestyle is unique.
The most important thing in the site was navigability and search.
Enabling users to search their product nearest to them, "local" to them. Knowing more about a service provider who is also providing service in other locations.
Technologies chosen to shape up the things
1) mod_rewrite for enabling SEF urls
2) Joomla 1.5 to act as backend CMS
3) SOBI Radius search plugin with many customisations for ZIP search
4) Rating component
5) Events and Calendar component to publish event
6) Javascript gadgets for video sharing

The beta to be released soon and will write more once its open.

Wednesday, June 3, 2009

New release of RDR

As came new rails 2.3 with more n more new stuff and making your app more DRY ( Do not Repeat Yourself) I have gone ahead and released new RDR Rails Dynamic Routes for rails 2.3.
The new release RDR2_3_2 is implemented in Rails 2.3.2 and compatible with Rails 2.2.x and above. I made some changes in new release like create actions with web-service support. for this I have used "respond_to" feature of Action Controller which determines the desired response format(html/xml) from the HTTP Accept header submitted by the client.

Hopefully you will like it..

Friday, May 29, 2009

RDR : Rails Dynamic Routes

While working on a CMS project we came across with requirement to build SEF urls dynamically in rails. rails have out-of-the-box support for SEF urls having routes.rb.
But what if I need to add new SEF urls without restarting server.. dynamically.
This need gave birth to RDR Rails Dynamic Routes. This is pretty simple utility that I have share on rubyforge. This will enable you to define customised(user friendly) url mapping for your selected controller/action making rails' routes.rb dynamic. No need to have server restart new url mapping is loaded dynamically. No additional dependency of any gems. You can also keep track of new updates on Rubyforge

Enjoy!!