2008-08-12 05:34:19 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\View\Tests;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\ArrayLib;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBVarchar;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\View\ArrayData;
|
2016-10-14 03:30:05 +02:00
|
|
|
use stdClass;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class ArrayDataTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
public function testViewabledataItemsInsideArraydataArePreserved()
|
|
|
|
{
|
|
|
|
/* ViewableData objects will be preserved, but other objects will be converted */
|
|
|
|
$arrayData = new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
"A" => new DBVarchar("A"),
|
|
|
|
"B" => new stdClass(),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(DBVarchar::class, get_class($arrayData->A));
|
|
|
|
$this->assertEquals(ArrayData::class, get_class($arrayData->B));
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWrappingAnAssociativeArrayWorks()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$array = ["A" => "Alpha", "B" => "Beta"];
|
2016-12-16 05:34:21 +01:00
|
|
|
$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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRefusesToWrapAnIndexedArray()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$array = [0 => "One", 1 => "Two"];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse(ArrayLib::is_associative($array));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetField()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$arrayData = new ArrayData([]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$arrayData->setField('d', 'Delta');
|
|
|
|
|
|
|
|
$this->assertTrue($arrayData->hasField('d'));
|
|
|
|
$this->assertEquals('Delta', $arrayData->getField('d'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetArray()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$array = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'Foo' => 'Foo',
|
|
|
|
'Bar' => 'Bar',
|
|
|
|
'Baz' => 'Baz'
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$arrayData = new ArrayData($array);
|
|
|
|
|
|
|
|
$this->assertEquals($arrayData->toMap(), $array);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayToObject()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$arr = ["test1" => "result1","test2"=>"result2"];
|
2016-12-16 05:34:21 +01:00
|
|
|
$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
|
|
|
}
|