2009-02-02 00:49:53 +01:00
|
|
|
<?php
|
2014-08-18 05:39:42 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-02-02 00:49:53 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class ClassInfoTest extends SapphireTest {
|
2011-02-27 05:20:28 +01:00
|
|
|
|
2015-02-12 14:15:32 +01:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'ClassInfoTest_BaseClass',
|
|
|
|
'ClassInfoTest_ChildClass',
|
|
|
|
'ClassInfoTest_GrandChildClass',
|
|
|
|
'ClassInfoTest_BaseDataClass',
|
|
|
|
'ClassInfoTest_NoFields',
|
|
|
|
);
|
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
}
|
|
|
|
|
2011-02-27 05:20:28 +01:00
|
|
|
public function testExists() {
|
2017-11-30 18:49:46 +01:00
|
|
|
$this->assertTrue(ClassInfo::exists('SS_Object'));
|
|
|
|
$this->assertTrue(ClassInfo::exists('SS_Object'));
|
2011-02-27 05:20:28 +01:00
|
|
|
$this->assertTrue(ClassInfo::exists('ClassInfoTest'));
|
2015-02-27 01:10:32 +01:00
|
|
|
$this->assertTrue(ClassInfo::exists('CLASSINFOTEST'));
|
2011-02-27 05:20:28 +01:00
|
|
|
$this->assertTrue(ClassInfo::exists('stdClass'));
|
2015-02-27 01:10:32 +01:00
|
|
|
$this->assertTrue(ClassInfo::exists('stdCLASS'));
|
2011-02-27 05:20:28 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSubclassesFor() {
|
2009-02-02 00:49:53 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
ClassInfo::subclassesFor('ClassInfoTest_BaseClass'),
|
|
|
|
array(
|
2011-02-21 06:16:52 +01:00
|
|
|
'ClassInfoTest_BaseClass' => 'ClassInfoTest_BaseClass',
|
2009-02-02 00:49:53 +01:00
|
|
|
'ClassInfoTest_ChildClass' => 'ClassInfoTest_ChildClass',
|
|
|
|
'ClassInfoTest_GrandChildClass' => 'ClassInfoTest_GrandChildClass'
|
|
|
|
),
|
|
|
|
'ClassInfo::subclassesFor() returns only direct subclasses and doesnt include base class'
|
|
|
|
);
|
2015-02-27 01:10:32 +01:00
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals(
|
|
|
|
ClassInfo::subclassesFor('classinfotest_baseclass'),
|
|
|
|
array(
|
|
|
|
'ClassInfoTest_BaseClass' => 'ClassInfoTest_BaseClass',
|
|
|
|
'ClassInfoTest_ChildClass' => 'ClassInfoTest_ChildClass',
|
|
|
|
'ClassInfoTest_GrandChildClass' => 'ClassInfoTest_GrandChildClass'
|
|
|
|
),
|
|
|
|
'ClassInfo::subclassesFor() is acting in a case sensitive way when it should not'
|
|
|
|
);
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-04 15:05:15 +02:00
|
|
|
public function testClassName() {
|
|
|
|
$this->assertEquals('ClassInfoTest', ClassInfo::class_name($this));
|
|
|
|
$this->assertEquals('ClassInfoTest', ClassInfo::class_name('ClassInfoTest'));
|
|
|
|
$this->assertEquals('ClassInfoTest', ClassInfo::class_name('CLaSsInfOTEsT'));
|
|
|
|
|
|
|
|
// This is for backwards compatiblity and will be removed in 4.0
|
|
|
|
$this->assertEquals('IAmAClassThatDoesNotExist', ClassInfo::class_name('IAmAClassThatDoesNotExist'));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testClassesForFolder() {
|
2012-03-24 04:38:57 +01:00
|
|
|
//$baseFolder = Director::baseFolder() . '/' . FRAMEWORK_DIR . '/tests/_ClassInfoTest';
|
2009-07-01 00:08:59 +02:00
|
|
|
//$manifestInfo = ManifestBuilder::get_manifest_info($baseFolder);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
$classes = ClassInfo::classes_for_folder(FRAMEWORK_DIR . '/tests');
|
2009-07-01 00:08:59 +02:00
|
|
|
$this->assertContains(
|
2010-10-12 23:42:25 +02:00
|
|
|
'classinfotest',
|
2009-07-01 00:08:59 +02:00
|
|
|
$classes,
|
|
|
|
'ClassInfo::classes_for_folder() returns classes matching the filename'
|
|
|
|
);
|
2015-02-27 01:10:32 +01:00
|
|
|
$this->assertContains(
|
|
|
|
'classinfotest_baseclass',
|
|
|
|
$classes,
|
|
|
|
'ClassInfo::classes_for_folder() returns additional classes not matching the filename'
|
|
|
|
);
|
2009-07-01 00:08:59 +02:00
|
|
|
}
|
2011-03-24 10:14:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ClassInfo::baseDataClass()
|
|
|
|
*/
|
|
|
|
public function testBaseDataClass() {
|
|
|
|
$this->assertEquals('ClassInfoTest_BaseClass', ClassInfo::baseDataClass('ClassInfoTest_BaseClass'));
|
2015-02-27 01:10:32 +01:00
|
|
|
$this->assertEquals('ClassInfoTest_BaseClass', ClassInfo::baseDataClass('classinfotest_baseclass'));
|
2011-03-24 10:14:38 +01:00
|
|
|
$this->assertEquals('ClassInfoTest_BaseClass', ClassInfo::baseDataClass('ClassInfoTest_ChildClass'));
|
2015-02-27 01:10:32 +01:00
|
|
|
$this->assertEquals('ClassInfoTest_BaseClass', ClassInfo::baseDataClass('CLASSINFOTEST_CHILDCLASS'));
|
2011-03-24 10:14:38 +01:00
|
|
|
$this->assertEquals('ClassInfoTest_BaseClass', ClassInfo::baseDataClass('ClassInfoTest_GrandChildClass'));
|
2015-02-27 01:10:32 +01:00
|
|
|
$this->assertEquals('ClassInfoTest_BaseClass', ClassInfo::baseDataClass('ClassInfoTest_GRANDChildClass'));
|
2011-03-24 10:14:38 +01:00
|
|
|
|
2011-12-16 23:51:40 +01:00
|
|
|
$this->setExpectedException('InvalidArgumentException');
|
2011-03-24 10:14:38 +01:00
|
|
|
ClassInfo::baseDataClass('DataObject');
|
|
|
|
}
|
|
|
|
|
2011-02-19 04:18:26 +01:00
|
|
|
/**
|
|
|
|
* @covers ClassInfo::ancestry()
|
|
|
|
*/
|
|
|
|
public function testAncestry() {
|
2011-03-29 06:54:08 +02:00
|
|
|
$ancestry = ClassInfo::ancestry('ClassInfoTest_ChildClass');
|
2011-02-19 04:18:26 +01:00
|
|
|
$expect = ArrayLib::valuekey(array(
|
2017-11-30 18:49:46 +01:00
|
|
|
'SS_Object',
|
2011-02-19 04:18:26 +01:00
|
|
|
'ViewableData',
|
|
|
|
'DataObject',
|
2011-03-29 06:54:08 +02:00
|
|
|
'ClassInfoTest_BaseClass',
|
|
|
|
'ClassInfoTest_ChildClass',
|
2011-02-19 04:18:26 +01:00
|
|
|
));
|
|
|
|
$this->assertEquals($expect, $ancestry);
|
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals($expect, ClassInfo::ancestry('classINFOTest_Childclass'));
|
|
|
|
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals($expect, ClassInfo::ancestry('classINFOTest_Childclass'));
|
|
|
|
|
|
|
|
ClassInfo::reset_db_cache();
|
2011-03-29 06:54:08 +02:00
|
|
|
$ancestry = ClassInfo::ancestry('ClassInfoTest_ChildClass', true);
|
|
|
|
$this->assertEquals(array('ClassInfoTest_BaseClass' => 'ClassInfoTest_BaseClass'), $ancestry,
|
|
|
|
'$tablesOnly option excludes memory-only inheritance classes'
|
|
|
|
);
|
2011-02-19 04:18:26 +01:00
|
|
|
}
|
|
|
|
|
2011-02-19 05:35:59 +01:00
|
|
|
/**
|
|
|
|
* @covers ClassInfo::dataClassesFor()
|
|
|
|
*/
|
|
|
|
public function testDataClassesFor() {
|
|
|
|
$expect = array(
|
|
|
|
'ClassInfoTest_BaseDataClass' => 'ClassInfoTest_BaseDataClass',
|
2014-08-18 05:39:42 +02:00
|
|
|
'ClassInfoTest_HasFields' => 'ClassInfoTest_HasFields',
|
|
|
|
'ClassInfoTest_WithRelation' => 'ClassInfoTest_WithRelation'
|
2011-02-19 05:35:59 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$classes = array(
|
|
|
|
'ClassInfoTest_BaseDataClass',
|
|
|
|
'ClassInfoTest_NoFields',
|
2014-08-18 05:39:42 +02:00
|
|
|
'ClassInfoTest_HasFields',
|
2011-02-19 05:35:59 +01:00
|
|
|
);
|
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
ClassInfo::reset_db_cache();
|
2014-08-18 05:39:42 +02:00
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor($classes[0]));
|
2015-02-27 01:10:32 +01:00
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor(strtoupper($classes[0])));
|
|
|
|
ClassInfo::reset_db_cache();
|
2014-08-18 05:39:42 +02:00
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor($classes[1]));
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2014-08-18 05:39:42 +02:00
|
|
|
$expect = array(
|
|
|
|
'ClassInfoTest_BaseDataClass' => 'ClassInfoTest_BaseDataClass',
|
|
|
|
'ClassInfoTest_HasFields' => 'ClassInfoTest_HasFields',
|
|
|
|
);
|
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
ClassInfo::reset_db_cache();
|
2014-08-18 05:39:42 +02:00
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor($classes[2]));
|
2015-02-27 01:10:32 +01:00
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor(strtolower($classes[2])));
|
2011-02-19 05:35:59 +01:00
|
|
|
}
|
|
|
|
|
2014-08-18 05:39:42 +02:00
|
|
|
public function testTableForObjectField() {
|
|
|
|
$this->assertEquals('ClassInfoTest_WithRelation',
|
|
|
|
ClassInfo::table_for_object_field('ClassInfoTest_WithRelation', 'RelationID')
|
|
|
|
);
|
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
$this->assertEquals('ClassInfoTest_WithRelation',
|
|
|
|
ClassInfo::table_for_object_field('ClassInfoTest_withrelation', 'RelationID')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('ClassInfoTest_BaseDataClass',
|
2014-08-18 05:39:42 +02:00
|
|
|
ClassInfo::table_for_object_field('ClassInfoTest_BaseDataClass', 'Title')
|
|
|
|
);
|
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
$this->assertEquals('ClassInfoTest_BaseDataClass',
|
2014-08-18 05:39:42 +02:00
|
|
|
ClassInfo::table_for_object_field('ClassInfoTest_HasFields', 'Title')
|
|
|
|
);
|
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
$this->assertEquals('ClassInfoTest_BaseDataClass',
|
2014-08-18 05:39:42 +02:00
|
|
|
ClassInfo::table_for_object_field('ClassInfoTest_NoFields', 'Title')
|
|
|
|
);
|
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
$this->assertEquals('ClassInfoTest_BaseDataClass',
|
|
|
|
ClassInfo::table_for_object_field('classinfotest_nofields', 'Title')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('ClassInfoTest_HasFields',
|
2014-08-18 05:39:42 +02:00
|
|
|
ClassInfo::table_for_object_field('ClassInfoTest_HasFields', 'Description')
|
|
|
|
);
|
|
|
|
|
|
|
|
// existing behaviour fallback to DataObject? Should be null.
|
|
|
|
$this->assertEquals('DataObject',
|
|
|
|
ClassInfo::table_for_object_field('ClassInfoTest_BaseClass', 'Nonexist')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNull(
|
|
|
|
ClassInfo::table_for_object_field('SomeFakeClassHere', 'Title')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNull(
|
2017-11-30 18:49:46 +01:00
|
|
|
ClassInfo::table_for_object_field('SS_Object', 'Title')
|
2014-08-18 05:39:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNull(
|
|
|
|
ClassInfo::table_for_object_field(null, null)
|
|
|
|
);
|
2015-10-30 04:26:14 +01:00
|
|
|
|
|
|
|
// Test fixed fields
|
|
|
|
$this->assertEquals(
|
|
|
|
'ClassInfoTest_BaseDataClass',
|
|
|
|
ClassInfo::table_for_object_field('ClassInfoTest_HasFields', 'ID')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'ClassInfoTest_BaseDataClass',
|
|
|
|
ClassInfo::table_for_object_field('ClassInfoTest_NoFields', 'Created')
|
|
|
|
);
|
2014-08-18 05:39:42 +02:00
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
|
|
|
|
2014-08-18 05:39:42 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
2015-02-12 14:15:32 +01:00
|
|
|
class ClassInfoTest_BaseClass extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
|
|
|
|
2014-08-18 05:39:42 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
class ClassInfoTest_ChildClass extends ClassInfoTest_BaseClass {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
|
|
|
|
2014-08-18 05:39:42 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
class ClassInfoTest_GrandChildClass extends ClassInfoTest_ChildClass {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-02-19 04:18:26 +01:00
|
|
|
}
|
2011-02-19 05:35:59 +01:00
|
|
|
|
2014-08-18 05:39:42 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
2015-02-12 14:15:32 +01:00
|
|
|
class ClassInfoTest_BaseDataClass extends DataObject implements TestOnly {
|
2014-08-18 05:39:42 +02:00
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Title' => 'Varchar'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ClassInfoTest_NoFields extends ClassInfoTest_BaseDataClass {
|
|
|
|
|
2011-02-19 05:35:59 +01:00
|
|
|
}
|
2014-08-18 05:39:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
2011-02-19 05:35:59 +01:00
|
|
|
class ClassInfoTest_HasFields extends ClassInfoTest_NoFields {
|
2014-08-18 05:39:42 +02:00
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Description' => 'Varchar'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ClassInfoTest_WithRelation extends ClassInfoTest_NoFields {
|
|
|
|
|
|
|
|
private static $has_one = array(
|
|
|
|
'Relation' => 'ClassInfoTest_HasFields'
|
|
|
|
);
|
2011-02-19 05:35:59 +01:00
|
|
|
}
|