PHP Classes

File: tests/AliasedArrayTest.php

Recommend this page to a friend!
  Classes of Richard Williams  >  Aliased Array  >  tests/AliasedArrayTest.php  >  Download  
File: tests/AliasedArrayTest.php
Role: Unit test script
Content type: text/plain
Description: PHPUnit tests
Class: Aliased Array
Assign array entries that point to other entries
Author: By
Last change: New
Date: 6 years ago
Size: 2,844 bytes


Class file image Download
// Tests for AliasedArray class

require __DIR__ . '/../AliasedArray.class.php';

AliasedArrayTest extends PHPUnit_Framework_TestCase {

test_1() {

$a = new AliasedArray();

$this->assertEquals(NULL, $a->get(0));
$this->assertEquals(NULL, $a['abc']);

// default array assignment

$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');
$a->alias(0, 'zerokey');
$this->fail('Failed to throw exception for duplicate alias');
        } catch (
Exception $e) {}

$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');

$a[400] = 1;


$a[400] = 1;
$a->alias(400, 'b');

// null returned for element never set.


// 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()));

testParent() {
$aa = new AliasedArray(array('a'=>1,'b'=>2,'c'=>3));
$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']);



For more information send a message to info at phpclasses dot org.