<?php
// Tests for AliasedArray class
require __DIR__ . '/../AliasedArray.class.php';
class AliasedArrayTest extends PHPUnit_Framework_TestCase {
function test_1() {
$a = new AliasedArray();
$this->assertEquals(NULL, $a->get(0));
$this->assertEquals(NULL, $a['abc']);
// default array assignment
$a->set('zero');
$this->assertEquals('zero', $a->get(0));
$a[] = 'one';
$this->assertEquals('one', $a->get(1));
$this->assertEquals(2, count($a));
// overwrite
$a->set('newzero', 0);
$this->assertEquals('newzero', $a->get(0));
// set an alias of zerokey to element 0.
$a->alias(0, 'zerokey');
try{
$a->alias(0, 'zerokey');
$this->fail('Failed to throw exception for duplicate alias');
} catch (Exception $e) {}
$this->assertTrue(isset($a[0]));
$this->assertEquals('newzero', $a[0]);
$this->assertEquals('newzero', $a->get('zerokey'));
$this->assertEquals('newzero', $a['zerokey']);
// Try to add a alias pointing to a non-existant index
try {
$a->alias(400, 'a2');
$this->fail('Failed to throw exception when no key for alias exists');
} catch (Exception $exc) { }
$a[400] = null;
$a->alias(400, 'a2');
$a->alias(400, 'a3');
$this->assertNull($a->get(400));
$this->assertNull($a->get('a2'));
$this->assertNull($a->get('a3'));
$a[400] = 1;
unset($a[400]);
$this->assertNull($a[400]);
$this->assertNull($a['a2']);
$this->assertNull($a['a3']);
$a[400] = 1;
$a->alias(400, 'b');
unset($a['b']);
$this->assertNull($a[400]);
$this->assertNull($a['b']);
// null returned for element never set.
$this->assertNull($a[1000]);
// make sure iterator works.
for($i=0; $i<count($a); $i++) {
if ($i == 0) $this->assertEquals('newzero', $a[0]);
if ($i == 1) $this->assertEquals('one', $a[1]);
}
$a->append('hello'); // adds element 401
$this->assertTrue(count($a) == 3);
$a['abc'] = 'abc';
$this->assertTrue(count($a) == 4);
$copy = $a->getArrayCopy();
$this->assertTrue(count($copy) == count($a));
$this->assertEquals(1, count($a->getAliases()));
}
function testParent() {
$aa = new AliasedArray(array('a'=>1,'b'=>2,'c'=>3));
$this->assertEquals(3,$aa->count());
$aa->alias('a', 'one');
$aa->alias('b', 'two');
$aa->alias('c', 'three');
$this->assertEquals(1, $aa['one']);
$this->assertEquals(2, $aa['two']);
$this->assertEquals(3, $aa['three']);
}
}
?>
|