silverstripe-framework/tests/model/DBClassNameTest.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

151 lines
5.1 KiB
PHP

<?php
use SilverStripe\Model\FieldType\DBClassName;
class DBClassNameTest extends SapphireTest {
protected $extraDataObjects = array(
'DBClassNameTest_Object',
'DBClassNameTest_ObjectSubClass',
'DBClassNameTest_ObjectSubSubClass',
'DBClassNameTest_OtherClass'
);
/**
* Test that custom subclasses generate the right hierarchy
*/
public function testEnumList() {
// Object 1 fields
$object = new DBClassNameTest_Object();
$defaultClass = $object->dbObject('DefaultClass');
$anyClass = $object->dbObject('AnyClass');
$childClass = $object->dbObject('ChildClass');
$leafClass = $object->dbObject('LeafClass');
// Object 2 fields
$object2 = new DBClassNameTest_ObjectSubClass();
$midDefault = $object2->dbObject('MidClassDefault');
$midClass = $object2->dbObject('MidClass');
// Default fields always default to children of base class (even if put in a subclass)
$mainSubclasses = array (
'DBClassNameTest_Object' => 'DBClassNameTest_Object',
'DBClassNameTest_ObjectSubClass' => 'DBClassNameTest_ObjectSubClass',
'DBClassNameTest_ObjectSubSubClass' => 'DBClassNameTest_ObjectSubSubClass',
);
$this->assertEquals($mainSubclasses, $defaultClass->getEnumObsolete());
$this->assertEquals($mainSubclasses, $midDefault->getEnumObsolete());
// Unbound classes detect any
$anyClasses = $anyClass->getEnumObsolete();
$this->assertContains('DBClassNameTest_OtherClass', $anyClasses);
$this->assertContains('DBClassNameTest_Object', $anyClasses);
$this->assertContains('DBClassNameTest_ObjectSubClass', $anyClasses);
$this->assertContains('DBClassNameTest_ObjectSubSubClass', $anyClasses);
// Classes bound to the middle of a tree
$midSubClasses = $mainSubclasses = array (
'DBClassNameTest_ObjectSubClass' => 'DBClassNameTest_ObjectSubClass',
'DBClassNameTest_ObjectSubSubClass' => 'DBClassNameTest_ObjectSubSubClass',
);
$this->assertEquals($midSubClasses, $childClass->getEnumObsolete());
$this->assertEquals($midSubClasses, $midClass->getEnumObsolete());
// Leaf clasess contain only exactly one node
$this->assertEquals(
array('DBClassNameTest_ObjectSubSubClass' => 'DBClassNameTest_ObjectSubSubClass',),
$leafClass->getEnumObsolete()
);
}
/**
* Test that the base class can be detected under various circumstances
*/
public function testBaseClassDetection() {
// Explicit DataObject
$field1 = new DBClassName('MyClass', 'DataObject');
$this->assertEquals('DataObject', $field1->getBaseClass());
$this->assertNotEquals('DataObject', $field1->getDefault());
// Explicit base class
$field2 = new DBClassName('MyClass', 'DBClassNameTest_Object');
$this->assertEquals('DBClassNameTest_Object', $field2->getBaseClass());
$this->assertEquals('DBClassNameTest_Object', $field2->getDefault());
// Explicit subclass
$field3 = new DBClassName('MyClass');
$field3->setValue(null, new DBClassNameTest_ObjectSubClass());
$this->assertEquals('DBClassNameTest_Object', $field3->getBaseClass());
$this->assertEquals('DBClassNameTest_Object', $field3->getDefault());
// Implicit table
$field4 = new DBClassName('MyClass');
$field4->setTable('DBClassNameTest_ObjectSubClass_versions');
$this->assertEquals('DBClassNameTest_Object', $field4->getBaseClass());
$this->assertEquals('DBClassNameTest_Object', $field4->getDefault());
// Missing
$field5 = new DBClassName('MyClass');
$this->assertEquals('DataObject', $field5->getBaseClass());
$this->assertNotEquals('DataObject', $field5->getDefault());
// Invalid class
$field6 = new DBClassName('MyClass');
$field6->setTable('InvalidTable');
$this->assertEquals('DataObject', $field6->getBaseClass());
$this->assertNotEquals('DataObject', $field6->getDefault());
// Custom default_classname
$field7 = new DBClassName('MyClass');
$field7->setTable('DBClassNameTest_CustomDefault');
$this->assertEquals('DBClassNameTest_CustomDefault', $field7->getBaseClass());
$this->assertEquals('DBClassNameTest_CustomDefaultSubclass', $field7->getDefault());
}
}
class DBClassNameTest_Object extends DataObject implements TestOnly {
private static $extensions = array(
'Versioned'
);
private static $db = array(
'DefaultClass' => 'DBClassName',
'AnyClass' => 'DBClassName("DataObject")',
'ChildClass' => 'DBClassName("DBClassNameTest_ObjectSubClass")',
'LeafClass' => 'DBClassName("DBClassNameTest_ObjectSubSubClass")'
);
}
class DBClassNameTest_ObjectSubClass extends DBClassNameTest_Object {
private static $db = array(
'MidClassDefault' => 'DBClassName',
'MidClass' => 'DBClassName("DBClassNameTest_ObjectSubclass")'
);
}
class DBClassNameTest_ObjectSubSubClass extends DBClassNameTest_ObjectSubclass {
}
class DBClassNameTest_OtherClass extends DataObject implements TestOnly {
private static $db = array(
'Title' => 'Varchar'
);
}
class DBClassNameTest_CustomDefault extends DataObject implements TestOnly {
private static $default_classname = 'DBClassNameTest_CustomDefaultSubclass';
private static $db = array(
'Title' => 'Varchar'
);
}
class DBClassNameTest_CustomDefaultSubclass extends DBClassNameTest_CustomDefault implements TestOnly {
private static $db = array(
'Content' => 'HTMLText'
);
}