PHP|Tek 2011 TDD Code
Thank you to everyone who attended my May 25th PHP|Tek talk on Test Driven Development. If you have not had a chance, please do leave feedback on joind.in.
Per the audiance request, below are the requirements, test file and source code we developed during the live tutorial.
Requirements:
Hash object
get and set function
respond to array access
Tests:
<?php
require 'simpletest/autorun.php';
require 'hash.php';
class TekTDDUnitTestCase extends UnitTestCase {
protected $subject;
function setup() {
$this->subject = new Hash;
}
function testClassExists() {
$this->assertTrue(class_exists('Hash'),'Hash Class does not exist');
}
function testSetMethod() {
$this->assertNull($this->subject->set('key','value'));
}
function testGetMethod() {
$key = 'foo';
$value = 'bar'.rand(0,100);
$this->subject->set($key,$value);
$this->assertEqual ($value, $this->subject->get($key));
}
function testAccessAsAnArray() {
$key = 'foo';
$value = 'bar'.rand(0,100);
$this->subject[$key] =$value;
$this->assertEqual($value, $this->subject[$key]);
}
}
?>
hash.php
<?php
class Hash implements ArrayAccess {
protected $store = array();
function offsetExists($key) {
}
function offsetGet($key) {
return $this->get($key);
}
function offsetSet($key,$value) {
$this->set($key, $value);
}
function offsetUnset($key) {
}
function set($key, $value) {
$this->store[$key] = $value;
}
function get($key) {
return $this->store[$key];
}
}
?>