February, 2010


11
Feb 10

Getting Started With PHPUnit

We all agree that testing code is better than not testing it right? So why do we tend to avoid writing unit tests to make sure that we are writing code that works? I’m looking at you PHP guys. Good thing there is PHPUnit and it is as easy as can be to get started with it.

First you have to discover the channel:

pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com

Sidenote: phpunit.de has many useful packages in it including phpcpd, phpdcd, and phploc.

Then you install it (I usually install all of the dependencies with PHPUnit, but you don’t really have to).

pear install phpunit/PHPUnit

After that you can start writing tests.

Say you have a simple class like this:

<?php
class needsTesting{
	public function divide($a, $b){
		return $a/$b;
	}
}

Your unit test file would be this (prior to adding any test cases to it):

<?php 
 
require_once 'PHPUnit/Framework.php';
 
require_once 'needsTesting.php';
 
class needsTestingTest extends PHPUnit_Framework_TestCase {
	/**
	 * @var needsTesting
	 */
	protected $object;
 
	/**
	 * This method is called before a test is executed.
	 */
	protected function setUp() {
		$this->object = new needsTesting;
	}
 
	/**
	 * This method is called after a test is executed.
	 */
	protected function tearDown() {
	}
 
	public function testDivide() {
 
	}
}

After you have added the obvious test cases, your testDivide() function should look something like this:

<?php
//[...]
	public function testDivide() {
		$array_a = array(
			10,
			20,
			30,
			40,
			50,
		);
		$array_b = array(
			2,
			4,
			6,
			8,
			0,
		);
		$array_expected = array(
			5,
			5,
			5,
			5,
			0,
		);
		foreach($array_a as $k=>$a){
			$b = $array_b[$k];
			$expected = $array_expected[$k];
			$result = $this->object->divide($a, $b);
 
			$this->assertEquals($expected, $result);
		}
	}
//[...]

Running the code is quite easy, if your ide has a PHPUnit extension (like Netbeans does) you can run it from there, or you can run it from the command line (in the folder you are writing the tests in):

 
phpunit needsTesting

Since one of the tests will give you an error, you should get this:

 
 
PHPUnit 3.4.9 by Sebastian Bergmann.
 
E
 
Time: 0 seconds, Memory: 5.25Mb
 
There was 1 error:
 
1) needsTesting::testDivide
Division by zero
 
[...]/needsTesting.php:4
[...]/needsTestingTest.php:56
 
FAILURES!
Tests: 1, Assertions: 4, Errors: 1.

Now it is up to you to decide how your application handles the Divide by 0 error that you got (or any other errors for that matter). While testing first is a good way to define exactly how your app should work, testing after is good too (as long as you are testing it is a good thing).

Check it out:

http://www.phpunit.de/manual/current/en/


9
Feb 10

New posts every Wednesday

So, it turns out that having a blog is not of much use if you don’t actually post anything on it. Unfortunately I have been neglecting mine as of late, so I am going to try and remedy that by forcing myself to post something on every Wednesday. Why Wednesday? Why not.


7
Feb 10

Daily Digest for February 7th

twitter (feed #2)
twitter (feed #2)
@chriscoyier That seems to be moving way too far in FF 3.6 [SeanJA]
twitter (feed #2)
@late2game Aren’t there enough monopoly games already? [SeanJA]
twitter (feed #2)
RT @donttrythis: This: http://www.youtube.com/watch?v=h-8PBx7isoM Is one of the loveliest PSAs I’ve ever seen. [SeanJA]
twitter (feed #2)
Little known fact about Christopher Walken: He can fly [SeanJA]
twitter (feed #2)
twitter (feed #2)
"I am infinitely more intelligent than you can ever hope to be." http://vogons.zetafleet.com/files/orly.jpg [SeanJA]
twitter (feed #2)
I am going to start at infinity and count backwards until I have enumerated all of the values between it and 0 [SeanJA]
twitter (feed #2)
Infinity…. uhhh…. infinity minus one…. infinity minus two…. infinity minus three… #ThisCouldTakeAWhile [SeanJA]
twitter (feed #2)
People people people…. Representable != Enumerable [SeanJA]
twitter (feed #2)
Ok… I mean representable is not necessarily enumerable [SeanJA]
twitter (feed #2)
Time to kill some zombies [SeanJA]
googlereader (feed #5)
twitter (feed #2)
Think of me as your parole officer with a mace. [SeanJA]
twitter (feed #2)
rt @mike_acton: Me: "Being a teenager is like being a newbie person." Daughter: "No, I *am* a person!" Me: "Whatever, noob." [SeanJA]
twitter (feed #2)
Remember when movie websites were not all flash? http://www2.warnerbros.com/spacejam/movie/jam.htm [SeanJA]
twitter (feed #2)
… and @levarburton as Black Lightning… awesome [SeanJA]
twitter (feed #2)
Screw you, I don’t want to log in to vote up an answer… I shouldn’t have to [SeanJA]
twitter (feed #2)
Ghost Hunters is a rather silly show… Just people walking around in the dark with cameras [SeanJA]
twitter (feed #2)
Oh my god! Fake heartbeat sound! Intense! #OhWaitGhostsArentReal [SeanJA]
twitter (feed #2)
100 episodes of this show?!?! [SeanJA]
twitter (feed #2)
@late2game When the Me generation grew up and earned a lot of money by being greedy? [SeanJA]
twitter (feed #2)
For professional ghost hunters… they really scare easily [SeanJA]

6
Feb 10

Daily Digest for February 6th

twitter (feed #2)
twitter (feed #2)
@late2game Ya, I saw that a while ago, this is a much better quality version of it though [SeanJA]
twitter (feed #2)
Apparently there is some sort of football thing this weekend [SeanJA]
twitter (feed #2)
@late2game He gives checkmate? [SeanJA]
twitter (feed #2)
Want to see a magic trick? [SeanJA]
twitter (feed #2)
@feliciaday Damn you… I want a lightsaber [SeanJA]
twitter (feed #2)
@feliciaday Pft you held it upside down… [SeanJA]
twitter (feed #2)
$%#$%# you bell, not letting me download legal torrents at any kind of speed… [SeanJA]
twitter (feed #2)
@neilhimself That is what you get for trusting Steve [SeanJA]
twitter (feed #2)
twitter (feed #2)
They originally named it "Scrotum humanum"? Really? Come on scientists… http://bit.ly/bFoO8v [SeanJA]
twitter (feed #2)
Who’s playing the super-bowl? [SeanJA]
twitter (feed #2)
Reward: 3000 Mega credits [SeanJA]
twitter (feed #2)
There is always a surplus of dry ice machines running on space ships [SeanJA]

5
Feb 10

Daily Digest for February 5th

googlereader (feed #5)
Shared Wall Street.
twitter (feed #2)
@paul_houle I hate xml… [SeanJA]
twitter (feed #2)
#ProTip RT @cssquirrel Pro-tip: <meat> is not a valid HTML tag. It should be, though. [SeanJA]
twitter (feed #2)
@cssquirrel It is for html tips like that that I follow you on twitter [SeanJA]
twitter (feed #2)
@paul_houle I think I might prefer that, though YAML would be even better (I think…?) [SeanJA]
twitter (feed #2)
@paul_houle Fair enough, but I can’t stand it when people don’t properly indent their code (or at least don’t do it consistently) [SeanJA]
twitter (feed #2)
@paul_houle Personally I hate overly verbose syntax [SeanJA]
googlereader (feed #5)
googlereader (feed #5)
googlereader (feed #5)
twitter (feed #2)
I agree with @TimCAD the AvP demo should not have been total freeforall deathmatch [SeanJA]
twitter (feed #2)
This is a really niche website… http://selleckwaterfallsandwich.tumblr.com/ [SeanJA]
twitter (feed #2)