Archive for the ‘Side Projects’ Category

Common PHP Test Utilities

Saturday, January 16th, 2010

Writing tests can be repetitive, even if you try to adhere to DRY principals. I’m using the PHP Simple Test library to run my tests, since it’s been written, it’s good and there’s no reason for me to write my own. However, I’m not using a heavy framework with an ORM, rather a light framework (Swiftlet) with my own DAO.

Here are my test utilities so far, they’ve been useful for general models and have helped increase test coverage while reducing code:

<?php
/**
 * TestUtils 
 * Utilities not included in test library that are commonly used in tests
 * 
 * copyright 2010 logon2 http://www.logon2.com.au
 * license http://opensource.org/licenses/gpl-3.0.html GNU Public License
 */
 
class TestUtils {
 
	function confirmSaveAndRestore($saved, $restored) {
		/*
		* Confirms the values saved to a database are
		* retreived accurately. 
		* saved can either be an array of key/value pairs OR
		* an array of an array of key/value pairs.
		*
		* The format of $restored must match that of $saved
		*/
 
		//determine if we're testing multiple values
		if (is_array($saved)) { 
			foreach ($saved as $key => $value) {
				if (!$key) {
					$array = true;
				} else {
					$array = false;
					break;
				}
			}
		} else {
			//the input MUST be an array or an array of an array
			throw new Exception("Input must be at least a 1D array");
		}
 
		if ($array = false) {
			$responses = array($saved);
		} else {
			$responses = $saved;
		}
 
		foreach ($responses as $response) {
			if (is_array($saved) && is_array($restored)) {
 
				foreach ($saved as $key => $value) {
					if ($restored[$key] != $value) {
						return false;
					}
				}
				return true;
			} else {
				throw new Exception("Inconsistent input");
			}
		}
	}
 
	function overwriteAttributes($attributes, $updates) {
		/*
		* Will overwrite attributes from $attributes with all attributes 
		* found in $updates, but leave all others
		*
		* This can be useful for testing edits
		*/
 
		if (is_array($updates)) {
			foreach ($updates as $key => $value) {
				$attributes[$key] = $value;
			}
		}
		return $attributes;
	}
 
	function generateBadValSet($badVals, $goodVals) {
		/*
		* Will generate a complete set of values, 
		* each with exactly one bad value from $badVals
		*
		* I was thinking if I should make it generate
		* all possibilities (n^m) rather than just
		* a set of values each with exactly one bad value.
		*/
		$badValSet = array();
 
		foreach($badVals as $attribute => $badVal) {
			if (!is_array($badVal)) {
				//more than one bad value for attribute
				$badVal = array($badVal);
			}
 
			foreach ($badVal as $thisBadVal) {
				$theseBadVals = $goodVals;
				$theseBadVals[$attribute] = $thisBadVal;
 
				$badValSet[] = $theseBadVals;
			}
		}
 
		return $badValSet;
 
	}
 
}
 
?>

Ironically, I have no test for these Test Utilities so beware ;)

Pretty Smarties – Pretty URLs through Smarty made Simple

Tuesday, February 24th, 2009

Pretty SmartiesDeciding what template system to use when designing and developing a website can be difficult. For beginners and the lazy, Dreamweaver’s DWT files are fine but they are difficult and clumsy to maintain and update. Besides, they are messy.

Being a logical and rational person, I want to separate content from layout and with a preference for PHP I decided it was time to build a simple template system.

A few weeks later and after using it to drive a couple of my websites, here’s what Pretty Smarties has to offer:

  • Uses Smarty template engine  – Easy to learn (That is, if you don’t already know it)
  • Pretty URLs – Great for search engines and humans ( http://www.site.com/urls/like/this )
  • SEO Optimisation – Reduced duplication by redirecting to correct host, and applying consistent “/” suffix.
  • Easy to implement optional page components – Simply add or remove files to add or remove components
  • Template updates are instantly applied
  • Swap template by changing 1 line

Source and documentation will be posted on the logon2 blog this week.

Lovely Captions – Transparent Image Overlays using CSS

Thursday, June 19th, 2008
Heath Ledger Jokes

Heath Ledger Jokes

They might get old, but he never will. LOL!

A new revolutionary (LOL!) tool is available over at logon2, it’s called ‘Lovely Caption Maker‘ – if the title didn’t give that away. Lovely Caption Maker was inspired by a beautiful effect that can be found at some of the more tech-savvy Australian Media websites such as The Age and Fox FM. I think ABC was the first place I saw this effect. (more…)

Multi-Comments on myLook

Saturday, March 15th, 2008

Secretly, the myLook team has been working away at the second attempt at myLook (hopefully this generation is a little bit better). Once nifty feature I’ve just added is Javascript-driven comments.

Here’s a sample – try to ignore the bland look, Alex hasn’t finished the designs. You can have a look at it yourself, if you like at the new myLook website.

myLook’s New Comment System

 myLook.cc’s new comment system lets users make comments exactly what they’re talking about, and lets users see the comments they want to see (or not see the ones they don’t)

Keep your eyes open for a how-to guide for the Javascript Show/Hide business on the logon2 blog…

Knock knock?
Who’s there?
Interrupting cow.
Interuppti… Moo!
Int.. Moooooo!