Showing posts with label TDD. Show all posts
Showing posts with label TDD. Show all posts

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

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)