The following unit tests are used to test the PHP key-value map with associative array. They use SimpleTest as a unit testing framework.
require_once(dirname(__FILE__) . '/../../config.inc.php'); require_once('simpletest/autorun.php'); require_once('ig/util/AssocMap.class.php'); /** * Associative Map unit tests. * @author Ivan Georgiev */ class ig_test_ig_util_AssocMap extends UnitTestCase { function testBasic() { $v1 = (object) array(); $v2 = (object) array(); $v2replace = (object) array(); $map =& ig_util_AssocMap::Create(); $this->assertIsA($map, 'ig_util_AbstractMap', 'Create() returns AbstractMap instance'); $this->assertTrue($map->isEmpty(), 'isEmpty() returns true empty map'); $this->assertEqual(0, $map->size(), 'size() returns 0 empty map %s'); $this->assertEqual(0, count($map->keyArray()), 'keyArray() returns empty array empty map %s'); $this->assertFalse($map->containsKey('a'), 'containsKey() returns FALSE key not set'); $this->assertFalse($map->containsValue($v1), 'containsValue() returns FALSE not exist'); $this->assertTrue(false===$map->get('a'), 'get() returns FALSE key not set'); $map->put('a', $v1); $map->put('b', $v2); $map->put('c', $v1); $this->assertFalse($map->isEmpty(), 'isEmpty() returns false some keys put.'); $this->assertEqual(3, $map->size(), 'size() returns number of keys'); $this->assertTrue($map->containsKey('a'), 'containsKey() returns TRUE key set'); $this->assertTrue($map->containsValue($v1), 'containsValue() returns TRUE value set'); $this->assertReference($v1, $map->get('a'), 'get() returns REFERENCE to value'); $unknownKeys = array_diff(array('a', 'b', 'c'), $map->keyArray()); $this->assertTrue(empty($unknownKeys), 'keyArray() returns array of keys'); $map->put('b', $v2replace); $this->assertFalse($map->containsValue($v2), 'put() removes value for existing key'); $this->assertReference($v2replace, $map->get('b'), 'put() associates existing key with new value'); $map->remove('b'); $this->assertEqual(2, $map->size(), 'remove() decreases the map size %s'); $this->assertFalse($map->containsKey('b'), 'remove() removes key from map'); $this->assertFalse($map->containsValue($v2replace), 'remove() removes value from map'); $this->assertReference($v1, $map->get('a'), 'remove() retains other key-value mappings'); $mapClone =& $map->clone(); $this->assertEqual($map->size(), $mapClone->size(), 'clone() returns map with same size'); $unknownKeys = array_diff($mapClone->keyArray(), $map->keyArray()); $this->assertTrue(empty($unknownKeys), 'clone() copies keys'); foreach($mapClone->keyArray() as $key) { $this->assertReference($mapClone->get($key), $map->get($key), 'clone() copies each key-value association'); } $map->clear(); $this->assertEqual(0, $map->size(), 'clear() sets map size to 0 %s'); $this->assertTrue($map->isEmpty(), 'clear() makes map empty'); } }