2009-02-02 00:49:53 +01:00
|
|
|
<?php
|
2014-08-18 05:39:42 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\ArrayLib;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
|
|
|
|
|
|
|
|
2016-06-15 06:03:16 +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',
|
2016-05-25 07:09:29 +02:00
|
|
|
'ClassInfoTest_BaseDataClass',
|
2015-02-12 14:15:32 +01:00
|
|
|
'ClassInfoTest_ChildClass',
|
|
|
|
'ClassInfoTest_GrandChildClass',
|
2016-05-25 07:09:29 +02:00
|
|
|
'ClassInfoTest_HasFields',
|
2015-02-12 14:15:32 +01:00
|
|
|
'ClassInfoTest_NoFields',
|
2016-05-25 07:09:29 +02:00
|
|
|
'ClassInfoTest_WithCustomTable',
|
|
|
|
'ClassInfoTest_WithRelation',
|
2015-02-12 14:15:32 +01:00
|
|
|
);
|
|
|
|
|
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() {
|
2016-08-19 00:51:35 +02:00
|
|
|
$this->assertTrue(ClassInfo::exists('SilverStripe\\Core\\Object'));
|
|
|
|
$this->assertTrue(ClassInfo::exists('SilverStripe\\Core\\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'));
|
2016-05-25 07:09:29 +02:00
|
|
|
$this->assertFalse(ClassInfo::exists('SomeNonExistantClass'));
|
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
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
public function testClassName()
|
|
|
|
{
|
2015-08-04 15:05:15 +02:00
|
|
|
$this->assertEquals('ClassInfoTest', ClassInfo::class_name($this));
|
|
|
|
$this->assertEquals('ClassInfoTest', ClassInfo::class_name('ClassInfoTest'));
|
|
|
|
$this->assertEquals('ClassInfoTest', ClassInfo::class_name('CLaSsInfOTEsT'));
|
2016-05-25 07:09:29 +02:00
|
|
|
}
|
2015-08-04 15:05:15 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
public function testNonClassName() {
|
|
|
|
$this->setExpectedException('ReflectionException', 'Class IAmAClassThatDoesNotExist does not exist');
|
2015-08-04 15:05:15 +02:00
|
|
|
$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
|
|
|
|
2016-09-12 07:25:08 +02:00
|
|
|
$classes = ClassInfo::classes_for_folder(ltrim(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
|
|
|
|
2011-02-19 04:18:26 +01:00
|
|
|
/**
|
2016-09-26 11:54:28 +02:00
|
|
|
* @covers SilverStripe\Core\ClassInfo::ancestry()
|
2011-02-19 04:18:26 +01:00
|
|
|
*/
|
|
|
|
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(
|
2016-08-19 00:51:35 +02:00
|
|
|
'SilverStripe\\Core\\Object',
|
|
|
|
'SilverStripe\\View\\ViewableData',
|
2016-06-15 06:03:16 +02:00
|
|
|
'SilverStripe\\ORM\\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
|
|
|
/**
|
2016-09-26 11:54:28 +02:00
|
|
|
* @covers SilverStripe\Core\ClassInfo::dataClassesFor()
|
2011-02-19 05:35:59 +01:00
|
|
|
*/
|
|
|
|
public function testDataClassesFor() {
|
|
|
|
$expect = array(
|
|
|
|
'ClassInfoTest_BaseDataClass' => 'ClassInfoTest_BaseDataClass',
|
2014-08-18 05:39:42 +02:00
|
|
|
'ClassInfoTest_HasFields' => 'ClassInfoTest_HasFields',
|
2016-05-25 07:09:29 +02:00
|
|
|
'ClassInfoTest_WithRelation' => 'ClassInfoTest_WithRelation',
|
|
|
|
'ClassInfoTest_WithCustomTable' => 'ClassInfoTest_WithCustomTable',
|
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
|
|
|
}
|
|
|
|
|
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'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
class ClassInfoTest_WithCustomTable extends ClassInfoTest_NoFields {
|
|
|
|
private static $table_name = 'CITWithCustomTable';
|
|
|
|
private static $db = array(
|
|
|
|
'Description' => 'Text'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-08-18 05:39:42 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|