silverstripe-framework/tests/php/ORM/DBClassNameTest.php

112 lines
4.4 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\ORM\Tests;
use SilverStripe\ORM\FieldType\DBClassName;
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\SapphireTest;
class DBClassNameTest extends SapphireTest {
2016-03-08 21:50:18 +01:00
2016-10-14 03:30:05 +02:00
protected $extraDataObjects = [
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
2016-10-14 03:30:05 +02:00
$object = new DBClassNameTest\TestObject();
$defaultClass = $object->dbObject('DefaultClass');
$anyClass = $object->dbObject('AnyClass');
$childClass = $object->dbObject('ChildClass');
$leafClass = $object->dbObject('LeafClass');
// Object 2 fields
2016-10-14 03:30:05 +02:00
$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 (
2016-10-14 03:30:05 +02:00
DBClassNameTest\TestObject::class => DBClassNameTest\TestObject::class,
DBClassNameTest\ObjectSubClass::class => DBClassNameTest\ObjectSubClass::class,
DBClassNameTest\ObjectSubSubClass::class => DBClassNameTest\ObjectSubSubClass::class,
);
$this->assertEquals($mainSubclasses, $defaultClass->getEnumObsolete());
$this->assertEquals($mainSubclasses, $midDefault->getEnumObsolete());
// Unbound classes detect any
$anyClasses = $anyClass->getEnumObsolete();
2016-10-14 03:30:05 +02:00
$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
$midSubClasses = $mainSubclasses = array (
2016-10-14 03:30:05 +02:00
DBClassNameTest\ObjectSubClass::class => DBClassNameTest\ObjectSubClass::class,
DBClassNameTest\ObjectSubSubClass::class => DBClassNameTest\ObjectSubSubClass::class,
);
$this->assertEquals($midSubClasses, $childClass->getEnumObsolete());
$this->assertEquals($midSubClasses, $midClass->getEnumObsolete());
// Leaf clasess contain only exactly one node
$this->assertEquals(
2016-10-14 03:30:05 +02:00
array(DBClassNameTest\ObjectSubSubClass::class => DBClassNameTest\ObjectSubSubClass::class,),
$leafClass->getEnumObsolete()
);
}
/**
* Test that the base class can be detected under various circumstances
*/
public function testBaseClassDetection() {
// Explicit DataObject
2016-10-14 03:30:05 +02:00
$field1 = new DBClassName('MyClass', DataObject::class);
$this->assertEquals(DataObject::class, $field1->getBaseClass());
$this->assertNotEquals(DataObject::class, $field1->getDefault());
// Explicit base class
2016-10-14 03:30:05 +02:00
$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');
2016-10-14 03:30:05 +02:00
$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');
2016-10-14 03:30:05 +02:00
$this->assertEquals(DBClassNameTest\TestObject::class, $field4->getBaseClass());
$this->assertEquals(DBClassNameTest\TestObject::class, $field4->getDefault());
// Missing
$field5 = new DBClassName('MyClass');
2016-10-14 03:30:05 +02:00
$this->assertEquals(DataObject::class, $field5->getBaseClass());
$this->assertNotEquals(DataObject::class, $field5->getDefault());
// Invalid class
$field6 = new DBClassName('MyClass');
$field6->setTable('InvalidTable');
2016-10-14 03:30:05 +02:00
$this->assertEquals(DataObject::class, $field6->getBaseClass());
$this->assertNotEquals(DataObject::class, $field6->getDefault());
// Custom default_classname
$field7 = new DBClassName('MyClass');
$field7->setTable('DBClassNameTest_CustomDefault');
2016-10-14 03:30:05 +02:00
$this->assertEquals(DBClassNameTest\CustomDefault::class, $field7->getBaseClass());
$this->assertEquals(DBClassNameTest\CustomDefaultSubclass::class, $field7->getDefault());
}
}