2008-08-12 05:34:19 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ArrayDataTest extends SapphireTest {
|
2010-05-25 04:58:06 +02:00
|
|
|
|
2008-08-12 05:34:19 +02:00
|
|
|
function testViewabledataItemsInsideArraydataArePreserved() {
|
|
|
|
/* ViewableData objects will be preserved, but other objects will be converted */
|
|
|
|
$arrayData = new ArrayData(array(
|
|
|
|
"A" => new Varchar("A"),
|
2009-03-14 01:16:32 +01:00
|
|
|
"B" => new stdClass(),
|
2008-08-12 05:34:19 +02:00
|
|
|
));
|
|
|
|
$this->assertEquals("Varchar", get_class($arrayData->A));
|
|
|
|
$this->assertEquals("ArrayData", get_class($arrayData->B));
|
|
|
|
}
|
2010-05-25 04:58:06 +02:00
|
|
|
|
|
|
|
function testWrappingANonEmptyObjectWorks() {
|
|
|
|
$object = new ArrayDataTest_NonEmptyObject();
|
|
|
|
$this->assertTrue(is_object($object));
|
|
|
|
|
|
|
|
$arrayData = new ArrayData($object);
|
|
|
|
|
|
|
|
$this->assertEquals("Apple", $arrayData->getField('a'));
|
|
|
|
$this->assertEquals("Banana", $arrayData->getField('b'));
|
|
|
|
$this->assertFalse($arrayData->hasField('c'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function testWrappingAnEmptyObjectWorks() {
|
|
|
|
$object = (object) array();
|
|
|
|
$this->assertTrue(is_object($object));
|
|
|
|
|
|
|
|
$arrayData = new ArrayData($object);
|
|
|
|
|
|
|
|
$this->assertEquals(null, $arrayData->TotalItems()); // (tobych) Shouldn't we get 0?
|
|
|
|
}
|
|
|
|
|
|
|
|
function testWrappingAnAssociativeArrayWorks() {
|
|
|
|
$array = array("A" => "Alpha", "B" => "Beta");
|
|
|
|
$this->assertTrue(ArrayLib::is_associative($array));
|
|
|
|
|
|
|
|
$arrayData = new ArrayData($array);
|
|
|
|
|
|
|
|
$this->assertTrue($arrayData->hasField("A"));
|
|
|
|
$this->assertEquals("Alpha", $arrayData->getField("A"));
|
|
|
|
$this->assertEquals("Beta", $arrayData->getField("B"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function testWrappingAnEmptyArrayWorks() {
|
|
|
|
$arrayData = new ArrayData(array());
|
|
|
|
|
|
|
|
$this->assertEquals(null, $arrayData->TotalItems()); // (tobych) Shouldn't we get 0?
|
|
|
|
}
|
|
|
|
|
|
|
|
function testRefusesToWrapAnIndexedArray() {
|
|
|
|
$array = array(0 => "One", 1 => "Two");
|
|
|
|
$this->assertFalse(ArrayLib::is_associative($array));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expect user_error() to be called below, if enabled
|
|
|
|
* (tobych) That should be an exception. Something like:
|
|
|
|
* $this->setExpectedException('InvalidArgumentException');
|
|
|
|
*/
|
|
|
|
|
|
|
|
// $arrayData = new ArrayData($array);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testForTemplateWorks() {
|
|
|
|
$arrayData = new ArrayData(array('b' => 'bunny'));
|
|
|
|
$this->assertEquals("array (\n 'b' => 'bunny',\n)", $arrayData->forTemplate());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testObjectToArrayWorks() {
|
|
|
|
$object = new ArrayDataTest_NonEmptyObject();
|
|
|
|
|
|
|
|
$array = ArrayDataTest_ArrayData_Exposed::object_to_array($object);
|
|
|
|
|
|
|
|
$this->assertEquals("Apple", $array['a']);
|
|
|
|
$this->assertEquals("Banana", $array['b']);
|
|
|
|
}
|
2010-10-15 05:47:27 +02:00
|
|
|
|
|
|
|
function testSetField() {
|
|
|
|
$arrayData = new ArrayData(array());
|
|
|
|
|
|
|
|
$arrayData->setField('d', 'Delta');
|
|
|
|
|
|
|
|
$this->assertTrue($arrayData->hasField('d'));
|
|
|
|
$this->assertEquals('Delta', $arrayData->getField('d'));
|
|
|
|
}
|
2010-10-19 00:39:19 +02:00
|
|
|
|
|
|
|
function testGetArray() {
|
|
|
|
$array = array(
|
|
|
|
'Foo' => 'Foo',
|
|
|
|
'Bar' => 'Bar',
|
|
|
|
'Baz' => 'Baz'
|
|
|
|
);
|
|
|
|
|
|
|
|
$arrayData = new ArrayData($array);
|
|
|
|
|
|
|
|
$this->assertEquals($arrayData->getArray(), $array);
|
|
|
|
}
|
2010-05-25 04:58:06 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
}
|
2010-05-25 04:58:06 +02:00
|
|
|
|
|
|
|
class ArrayDataTest_ArrayData_Exposed extends ArrayData {
|
|
|
|
|
|
|
|
public static function object_to_array($obj) {
|
|
|
|
return parent::object_to_array($obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ArrayDataTest_NonEmptyObject {
|
|
|
|
|
|
|
|
static $c = "Cucumber";
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->a = "Apple";
|
|
|
|
$this->b = "Banana";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|