getting started with CodeIgniter on Ubuntu Lucid Lynx (10.04)

As I am currently playing around with CodeIgniter (an open source PHP web application framework), I thought I could write down the few software installation steps one needs to go through in order to get started on a fresh Ubuntu box. And all you really need is a LAMP (Linux Apache MySQL PHP) setup that can be up and running in less than 10 minutes (assuming the OS is already installed of course).

Install Apache

sudo apt-get install apache2

Install MySQL

sudo apt-get install mysql-server

Install PHP (with MySQL support)

sudo apt-get install php5 php5-mysql

Enable the rewrite mod in Apache

This is only needed if you plan to get rid off “index.php” in your CodeIgniter application’s URLs.

sudo a2enmod rewrite
sudo gedit /etc/apache2/sites-available/default
<!-- Edit the configuration and change AllowOverride None to AllowOverride All -->
<Directory /var/www/>
   Options Indexes FollowSymLinks MultiViews
   AllowOverride All
   Order allow,deny
   allow from all
</Directory>

Add your site to your local web server

You could probably just extract the CodeIgniter archive into /var/www/ but you also could have your application in your home folder (/home/scandinabox/www.mysite.com/ for instance). In that case, all you need to do is create a symbolic link in the Apache web directory pointing to your application folder.

cd /var/www
sudo ln -s /home/scandinabox/www.mysite.com/ www.mysite.com

Restart Apache

sudo /etc/init.d/apache2 restart

Here you go, just point your web browser to http://localhost/www.mysite.com/ and you should see the CodeIgniter example pages.

concept series – Mozilla Seabird

behaviour driven unit test design in Java

Writing unit tests is not that hard but writing good unit tests which, when they fail, tell you exactly and verbosely why and where it did not go right is not as trivial as it seems.

Let’s say we have a class that represents a square. The only operations we can perform on a square are: getting its surface and doubling the side length.

public class Square {

  private int sideLength;

  public Square(int sideLength) {
    this.sideLength = sideLength;
  }

  public int getSurface() {

    return this.sideLength * this.sideLength;

  }

  public void doubleSideLength() {

    this.sideLength = this.sideLength * 2;

  }

}

To test that piece of code, we could probably write a unit test that will look like:

public class SquareTest {

  public void testSurfaceWhenSideLengthIsDoubled() {

    Square a = new Square(5);
    Square b = a; b.doubleSideLength();

    assertEquals(a.getSurface(), b.getSurface() / 4);

  }

}

From a programmer’s point of view the code above perfectly makes sense but – even if the example is rather simple – it does not really tells us what scenario we want to test here. If this piece code would be read by a product manager, she would probably not understand it (oh well, in that case she probably will but unit tests can be a lot more complex than that).

What about making the code a little bit clearer and telling us a story? There are frameworks for that you could argue (like JBehave) but do we need a framework – and all the configuration, XML files that come with – for everything really? What if we just rewrite the test class so it is more narrative?

public class SquareTestEnhanced {

  private Square square;

  public void test_side_length_x_2_gives_surface_x_4() {

    when_doubling_is_done_on(new Square(5));
    then_surface_must_be_multiplied_by_4();

  }

  private void when_doubling_is_done_on(Square s) {

    this.square = s;

  }

  private void then_surface_must_be_multiplied_by_4() {

    Square s = this.square; s.doubleSideLength();
    assertEquals(this.square.getSurface() * 4, s.getSurface());

  }

}

Isn’t it enough? Can my product manager understand what we are testing between lines 5 and 10?