Programming


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/


1
Dec 09

Simple Tool Tip with jQuery

I saw a post that talked about how to make a simple tool tip with jQuery, but that post did not make a plugin of it. It just added the function to the global scape, meaning that it was not chainable and it would only be applied to the a element. So I took the code for the first example as a starting point and turned the first example into a plugin. So, based on the first example, here we have yet another tooltip plugin, it uses the title attribute so it is perfectly accessible to people without javascript enabled. It can be applied to any element, and it is chainable and it will also be applied to new elements on the page instead of you having to re attach it to every item that you add to the dom.

Here is the javascript:

// starting the script on page load
$().ready(function(){
	$('a').simple_tip();
});

The html:

 
<a href="#" title="This is a tooltip">I have a tool tip!</a>
 
<a href="#">I don't have a tool tip!</a>

The demo.

The code is available here:
http://code.google.com/p/jsimpletip/downloads/list

The code is licensed:
Mit and GPL v2


27
Aug 09

When is 4 – 1 = 4?

When c like languages say so

$a = 5;
$b = 1;
 
$result = $a---$b;
echo $a; #=>4
echo $b; #=>1
echo $result; #=>4
public class FunMaths{
	public static void main(String[] args) {
		int result;
		int a = 5, b = 1;
		result = a---b;
		System.out.println(a);
		System.out.println(b);
		System.out.println(result);
	}
}

9
Aug 09

A couple useful jQuery Snippets

For popups, just add class=”popup”

jQuery('a.popup').live('click', function(){
	newwindow=window.open($(this).attr('href'),'','height=200,width=150');
	if (window.focus) {newwindow.focus()}
	return false;
});

To open in a new tab, add class=”newTab”

jQuery('a.newTab').live('click', function(){
	newwindow=window.open($(this).href);
	jQuery(this).target = "_blank";
	return false;
});

Check if something is in view

function in_view(elem){
	var docViewTop = jQuery(window).scrollTop();
	var docViewBottom = docViewTop + jQuery(window).height();
 
	var elemTop = jQuery(elem).offset().top;
	var elemBottom = elemTop + jQuery(elem).height();
 
	return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom) && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop));
}

23
Jul 09

PHP Pop Quiz Hotshot

Without running it, what does this do?

function hmmm(){
	$i = 'a';
	$array = array();
	while($i != 'z'){
		$array[$i] = $i;
		$i++;
	}
	return $array;
}