2015-09-04 05:49:22 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\FieldType\DBClassName;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-11-14 23:27:21 +01:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2017-07-03 02:21:27 +02:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class DBClassNameTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
DBClassNameTest\TestObject::class,
|
|
|
|
DBClassNameTest\ObjectSubClass::class,
|
|
|
|
DBClassNameTest\ObjectSubSubClass::class,
|
|
|
|
DBClassNameTest\OtherClass::class,
|
|
|
|
DBClassNameTest\CustomDefaultSubclass::class,
|
|
|
|
DBClassNameTest\CustomDefault::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that custom subclasses generate the right hierarchy
|
|
|
|
*/
|
|
|
|
public function testEnumList()
|
|
|
|
{
|
|
|
|
// Object 1 fields
|
|
|
|
$object = new DBClassNameTest\TestObject();
|
2017-09-19 06:55:39 +02:00
|
|
|
/** @var DBClassName $defaultClass */
|
2016-12-16 05:34:21 +01:00
|
|
|
$defaultClass = $object->dbObject('DefaultClass');
|
2017-09-19 06:55:39 +02:00
|
|
|
/** @var DBClassName $anyClass */
|
2016-12-16 05:34:21 +01:00
|
|
|
$anyClass = $object->dbObject('AnyClass');
|
2017-09-19 06:55:39 +02:00
|
|
|
/** @var DBClassName $childClass */
|
2016-12-16 05:34:21 +01:00
|
|
|
$childClass = $object->dbObject('ChildClass');
|
2017-09-19 06:55:39 +02:00
|
|
|
/** @var DBClassName $leafClass */
|
2016-12-16 05:34:21 +01:00
|
|
|
$leafClass = $object->dbObject('LeafClass');
|
|
|
|
|
|
|
|
// Object 2 fields
|
|
|
|
$object2 = new DBClassNameTest\ObjectSubClass();
|
2017-09-19 06:55:39 +02:00
|
|
|
/** @var DBClassName $midDefault */
|
2016-12-16 05:34:21 +01:00
|
|
|
$midDefault = $object2->dbObject('MidClassDefault');
|
2017-09-19 06:55:39 +02:00
|
|
|
/** @var DBClassName $midClass */
|
2016-12-16 05:34:21 +01:00
|
|
|
$midClass = $object2->dbObject('MidClass');
|
|
|
|
|
|
|
|
// Default fields always default to children of base class (even if put in a subclass)
|
2020-04-20 19:58:09 +02:00
|
|
|
$mainSubclasses = [
|
2017-09-19 06:55:39 +02:00
|
|
|
DBClassNameTest\TestObject::class,
|
|
|
|
DBClassNameTest\ObjectSubClass::class,
|
|
|
|
DBClassNameTest\ObjectSubSubClass::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($mainSubclasses, $defaultClass->getEnumObsolete());
|
|
|
|
$this->assertEquals($mainSubclasses, $midDefault->getEnumObsolete());
|
|
|
|
|
|
|
|
// Unbound classes detect any
|
|
|
|
$anyClasses = $anyClass->getEnumObsolete();
|
|
|
|
$this->assertContains(DBClassNameTest\OtherClass::class, $anyClasses);
|
|
|
|
$this->assertContains(DBClassNameTest\TestObject::class, $anyClasses);
|
|
|
|
$this->assertContains(DBClassNameTest\ObjectSubClass::class, $anyClasses);
|
|
|
|
$this->assertContains(DBClassNameTest\ObjectSubSubClass::class, $anyClasses);
|
|
|
|
|
|
|
|
// Classes bound to the middle of a tree
|
2020-04-20 19:58:09 +02:00
|
|
|
$midSubClasses = $mainSubclasses = [
|
2017-09-19 06:55:39 +02:00
|
|
|
DBClassNameTest\ObjectSubClass::class,
|
|
|
|
DBClassNameTest\ObjectSubSubClass::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($midSubClasses, $childClass->getEnumObsolete());
|
|
|
|
$this->assertEquals($midSubClasses, $midClass->getEnumObsolete());
|
|
|
|
|
|
|
|
// Leaf clasess contain only exactly one node
|
|
|
|
$this->assertEquals(
|
2017-09-19 06:55:39 +02:00
|
|
|
[ DBClassNameTest\ObjectSubSubClass::class ],
|
2016-12-16 05:34:21 +01:00
|
|
|
$leafClass->getEnumObsolete()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the base class can be detected under various circumstances
|
|
|
|
*/
|
|
|
|
public function testBaseClassDetection()
|
|
|
|
{
|
|
|
|
// Explicit DataObject
|
|
|
|
$field1 = new DBClassName('MyClass', DataObject::class);
|
|
|
|
$this->assertEquals(DataObject::class, $field1->getBaseClass());
|
|
|
|
$this->assertNotEquals(DataObject::class, $field1->getDefault());
|
|
|
|
|
|
|
|
// Explicit base class
|
|
|
|
$field2 = new DBClassName('MyClass', DBClassNameTest\TestObject::class);
|
|
|
|
$this->assertEquals(DBClassNameTest\TestObject::class, $field2->getBaseClass());
|
|
|
|
$this->assertEquals(DBClassNameTest\TestObject::class, $field2->getDefault());
|
|
|
|
|
|
|
|
// Explicit subclass
|
|
|
|
$field3 = new DBClassName('MyClass');
|
|
|
|
$field3->setValue(null, new DBClassNameTest\ObjectSubClass());
|
|
|
|
$this->assertEquals(DBClassNameTest\TestObject::class, $field3->getBaseClass());
|
|
|
|
$this->assertEquals(DBClassNameTest\TestObject::class, $field3->getDefault());
|
|
|
|
|
|
|
|
// Implicit table
|
|
|
|
$field4 = new DBClassName('MyClass');
|
|
|
|
$field4->setTable('DBClassNameTest_ObjectSubClass_Versions');
|
|
|
|
$this->assertEquals(DBClassNameTest\TestObject::class, $field4->getBaseClass());
|
|
|
|
$this->assertEquals(DBClassNameTest\TestObject::class, $field4->getDefault());
|
|
|
|
|
|
|
|
// Missing
|
|
|
|
$field5 = new DBClassName('MyClass');
|
|
|
|
$this->assertEquals(DataObject::class, $field5->getBaseClass());
|
|
|
|
$this->assertNotEquals(DataObject::class, $field5->getDefault());
|
|
|
|
|
|
|
|
// Invalid class
|
|
|
|
$field6 = new DBClassName('MyClass');
|
|
|
|
$field6->setTable('InvalidTable');
|
|
|
|
$this->assertEquals(DataObject::class, $field6->getBaseClass());
|
|
|
|
$this->assertNotEquals(DataObject::class, $field6->getDefault());
|
|
|
|
|
|
|
|
// Custom default_classname
|
|
|
|
$field7 = new DBClassName('MyClass');
|
|
|
|
$field7->setTable('DBClassNameTest_CustomDefault');
|
|
|
|
$this->assertEquals(DBClassNameTest\CustomDefault::class, $field7->getBaseClass());
|
|
|
|
$this->assertEquals(DBClassNameTest\CustomDefaultSubclass::class, $field7->getDefault());
|
|
|
|
}
|
2017-11-14 23:27:21 +01:00
|
|
|
|
|
|
|
public function testShortName()
|
|
|
|
{
|
|
|
|
$test1 = new DBClassNameTest\TestObject();
|
|
|
|
$test2 = new DBClassNameTest\ObjectSubSubClass();
|
|
|
|
$this->assertEquals('TestObject', $test1->obj('ClassName')->getShortName());
|
|
|
|
$this->assertEquals('ObjectSubSubClass', $test2->obj('ClassName')->getShortName());
|
|
|
|
|
|
|
|
$test3 = DBField::create_field('DBClassName', \stdClass::class);
|
|
|
|
$this->assertEquals('stdClass', $test3->getShortName());
|
|
|
|
|
|
|
|
$test4 = DBField::create_field('DBClassName', null);
|
|
|
|
$this->assertNull($test4->getShortName());
|
|
|
|
|
|
|
|
$test5 = DBField::create_field('DBClassName', 'not a class');
|
|
|
|
$this->assertNull($test5->getShortName());
|
|
|
|
}
|
2015-09-04 05:49:22 +02:00
|
|
|
}
|