Preview to PHP 5.3

Posted: May 19th, 2008 | Author: | Tags: | Comments Off

One of the mayor updates since PHP 5.0 is upcoming with the 5.3 version, this is also a major step forward to PHP 6 – I love to have namespaces and appreciate the split of E_NOTICE and E_DEPRECATED. Also other features like the native MySQL support ( I hope this really raises the performance), the array_replace/array_replace_recursive functions and also dynamic calls to static functions “$classname::$functionname($parameter);” sound very cool. So I really look forward to have it on the server (or at least on my laptop) :P

Regarding the namespaces I’ve one idea: maybe someday someone will invent a common namespace “php::clean” which fixes all the small inconsistencies within function-names and parameter-styles … that would be really great ;)

A nice article with a more detailed look into namespaces and the other inventions in 5.3 can be found on sitepoint.com


Using Mocks within Tests

Posted: May 17th, 2008 | Author: | Tags: , , | Comments Off

Let’s say you have some kind of process which creates cookies and which requires the cookies to bake before you deliver them to the customers. This process somehow touches two kinds of objects the cookie itself and a object which performs the process – the cookie oven.
When you start to develop the oven how do you check whether the cookies are really baked or not? Since you can’t get rid of the dependency between the oven and the cookie, you have to simulate a real object and do the checks by hand – but wait there’s already a way to resolve it:
PHPUnit ships with a very nice and comfortable function to create and check mock-objects and that’s exactly what we need in this situation where we somehow need to find out if our cookie really gets baked.
The testcase for this scenario could look like this:

class TestCookieOven extends PHPUnit_Framework_TestCase {
	public function testIfCookieIsBakenWithinFinishing() {
		/* create the mock and expect the
		bakeIt method to be called at least once */
		$cookiemock = $this->getMock(‘Cookie’, array(‘bakeIt’));
		$cookiemock->expects($this->once())->method(‘bakeIt’);

		/* create the observed object and perform the required steps */
		$cookieoven = new CookieOven();
		$cookieoven->finishCookie($cookiemock);
	}
}

This first creates a mock and assigns the expectation that the “bakeIt”-function is called exactly once. [ beside the ->once()-call there are some alternatives: ->any(), ->never(), ->atLeastOnce(), ->exactly(int $count) and ->at(int $index) ]. Then it passes the mock to the oven and performs the method we want to check. To make this test green you need at least this:

CookieOven {
	/**
	* Takes a cookie and prepares it to be ready
	* for a customer
	*
	* @param Cookie $cookie
	*/
	public function finishCookie(Cookie $cookie) {
		$cookie->bakeIt();
	}
}

So as soon you have these lines you have a green test and that’s great because there is no need to have a real “cookie” class – someone else can take care of the cookies – oh wait … I’d better do this myself :P