2008-08-12 05:34:19 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ArrayDataTest extends SapphireTest {
|
2010-05-25 04:58:06 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testViewabledataItemsInsideArraydataArePreserved() {
|
2008-08-12 05:34:19 +02:00
|
|
|
/* 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));
|
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWrappingANonEmptyObjectWorks() {
|
2010-05-25 04:58:06 +02:00
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWrappingAnAssociativeArrayWorks() {
|
2010-05-25 04:58:06 +02:00
|
|
|
$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"));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRefusesToWrapAnIndexedArray() {
|
2010-05-25 04:58:06 +02:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetField() {
|
2010-10-15 05:47:27 +02:00
|
|
|
$arrayData = new ArrayData(array());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:47:27 +02:00
|
|
|
$arrayData->setField('d', 'Delta');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:47:27 +02:00
|
|
|
$this->assertTrue($arrayData->hasField('d'));
|
|
|
|
$this->assertEquals('Delta', $arrayData->getField('d'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetArray() {
|
2011-10-29 06:04:45 +02:00
|
|
|
$originalDeprecation = Deprecation::dump_settings();
|
|
|
|
Deprecation::notification_version('2.4');
|
|
|
|
|
2010-10-19 00:39:19 +02:00
|
|
|
$array = array(
|
|
|
|
'Foo' => 'Foo',
|
|
|
|
'Bar' => 'Bar',
|
|
|
|
'Baz' => 'Baz'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 00:39:19 +02:00
|
|
|
$arrayData = new ArrayData($array);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-11 01:27:36 +02:00
|
|
|
$this->assertEquals($arrayData->toMap(), $array);
|
2011-10-29 06:04:45 +02:00
|
|
|
|
|
|
|
Deprecation::restore_settings($originalDeprecation);
|
2010-10-19 00:39:19 +02:00
|
|
|
}
|
2010-05-25 04:58:06 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testArrayToObject() {
|
2011-03-22 22:23:57 +01:00
|
|
|
$arr = array("test1" => "result1","test2"=>"result2");
|
|
|
|
$obj = ArrayData::array_to_object($arr);
|
|
|
|
$objExpected = new stdClass();
|
|
|
|
$objExpected->test1 = "result1";
|
|
|
|
$objExpected->test2 = "result2";
|
|
|
|
$this->assertEquals($obj,$objExpected, "Two objects match");
|
|
|
|
}
|
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
}
|
2010-05-25 04:58:06 +02:00
|
|
|
|
|
|
|
class ArrayDataTest_NonEmptyObject {
|
|
|
|
|
|
|
|
static $c = "Cucumber";
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->a = "Apple";
|
|
|
|
$this->b = "Banana";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|