silverstripe-framework/tests/core/ArrayDataTest.php
Sam Minnee aeccb8b8e0 API: Move DBField subclasses into SilverStripe\Model\FieldType namespace
API: Deprecate SS_Datetime.

The DBField subclasses are have all been renamed to start with “DB” and
be in the SilverStripe\Model\FieldType namespace. To keep DataObject
definitions concise, the original short variations of their names are
preserved as service definitions. Most of the field generation code
doesn’t need to change, but where field classes are referenced directly,
changes will be needed.

SS_Datetime, which is commonly referenced outside the model system
itself, has been preserved as a subclass of DBDatetime. This has been
marked as deprecated and can be removed in SilverStripe 5.

A few places that referred to $db and $casting values weren’t using
the Injector to instantiate the relevant classes. This meant that the
remapping we have created as part of moving classes into a namespace
didn’t work.
2016-03-22 18:09:30 +13:00

101 lines
2.6 KiB
PHP

<?php
use SilverStripe\Model\FieldType\DBVarchar;
class ArrayDataTest extends SapphireTest {
public function testViewabledataItemsInsideArraydataArePreserved() {
/* ViewableData objects will be preserved, but other objects will be converted */
$arrayData = new ArrayData(array(
"A" => new DBVarchar("A"),
"B" => new stdClass(),
));
$this->assertEquals('SilverStripe\Model\FieldType\DBVarchar', get_class($arrayData->A));
$this->assertEquals("ArrayData", 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() {
$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"));
}
public 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);
}
public function testSetField() {
$arrayData = new ArrayData(array());
$arrayData->setField('d', 'Delta');
$this->assertTrue($arrayData->hasField('d'));
$this->assertEquals('Delta', $arrayData->getField('d'));
}
public function testGetArray() {
$originalDeprecation = Deprecation::dump_settings();
Deprecation::notification_version('2.4');
$array = array(
'Foo' => 'Foo',
'Bar' => 'Bar',
'Baz' => 'Baz'
);
$arrayData = new ArrayData($array);
$this->assertEquals($arrayData->toMap(), $array);
Deprecation::restore_settings($originalDeprecation);
}
public function testArrayToObject() {
$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");
}
}
class ArrayDataTest_NonEmptyObject {
static $c = "Cucumber";
public function __construct() {
$this->a = "Apple";
$this->b = "Banana";
}
}