2009-02-02 00:49:53 +01:00
|
|
|
<?php
|
2014-08-18 05:39:42 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Core\Tests;
|
|
|
|
|
2017-05-17 07:40:13 +02:00
|
|
|
use ReflectionException;
|
2017-09-19 06:55:39 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\BaseClass;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\BaseDataClass;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\ChildClass;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\GrandChildClass;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\HasFields;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\NoFields;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\WithCustomTable;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\WithRelation;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\View\ViewableData;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class ClassInfoTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_dataobjects = array(
|
2016-12-16 05:34:21 +01:00
|
|
|
BaseClass::class,
|
|
|
|
BaseDataClass::class,
|
|
|
|
ChildClass::class,
|
|
|
|
GrandChildClass::class,
|
|
|
|
HasFields::class,
|
|
|
|
NoFields::class,
|
|
|
|
WithCustomTable::class,
|
|
|
|
WithRelation::class,
|
|
|
|
);
|
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExists()
|
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
$this->assertTrue(ClassInfo::exists(ClassInfo::class));
|
|
|
|
$this->assertTrue(ClassInfo::exists('SilverStripe\\Core\\classinfo'));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(ClassInfo::exists('SilverStripe\\Core\\Tests\\ClassInfoTest'));
|
|
|
|
$this->assertTrue(ClassInfo::exists('SilverStripe\\Core\\Tests\\CLASSINFOTEST'));
|
|
|
|
$this->assertTrue(ClassInfo::exists('stdClass'));
|
|
|
|
$this->assertTrue(ClassInfo::exists('stdCLASS'));
|
|
|
|
$this->assertFalse(ClassInfo::exists('SomeNonExistantClass'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubclassesFor()
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$subclasses = [
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\baseclass' => BaseClass::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\childclass' => ChildClass::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\grandchildclass' => GrandChildClass::class,
|
|
|
|
];
|
2019-04-30 00:43:14 +02:00
|
|
|
$subclassesWithoutBase = [
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\childclass' => ChildClass::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\grandchildclass' => GrandChildClass::class,
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-09-19 06:55:39 +02:00
|
|
|
$subclasses,
|
2016-12-16 05:34:21 +01:00
|
|
|
ClassInfo::subclassesFor(BaseClass::class),
|
|
|
|
'ClassInfo::subclassesFor() returns only direct subclasses and doesnt include base class'
|
|
|
|
);
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals(
|
2017-09-19 06:55:39 +02:00
|
|
|
$subclasses,
|
2016-12-16 05:34:21 +01:00
|
|
|
ClassInfo::subclassesFor('silverstripe\\core\\tests\\classinfotest\\baseclass'),
|
|
|
|
'ClassInfo::subclassesFor() is acting in a case sensitive way when it should not'
|
|
|
|
);
|
2019-04-30 00:43:14 +02:00
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals(
|
|
|
|
$subclassesWithoutBase,
|
|
|
|
ClassInfo::subclassesFor('silverstripe\\core\\tests\\classinfotest\\baseclass', false)
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClassName()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
ClassInfoTest::class,
|
|
|
|
ClassInfo::class_name($this)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
ClassInfoTest::class,
|
|
|
|
ClassInfo::class_name('SilverStripe\\Core\\Tests\\ClassInfoTest')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
ClassInfoTest::class,
|
|
|
|
ClassInfo::class_name('SilverStripe\\Core\\TESTS\\CLaSsInfOTEsT')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonClassName()
|
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
$this->expectException(ReflectionException::class);
|
|
|
|
$this->expectExceptionMessage('Class IAmAClassThatDoesNotExist does not exist');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('IAmAClassThatDoesNotExist', ClassInfo::class_name('IAmAClassThatDoesNotExist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClassesForFolder()
|
|
|
|
{
|
|
|
|
$classes = ClassInfo::classes_for_folder(ltrim(FRAMEWORK_DIR . '/tests', '/'));
|
2017-09-19 06:55:39 +02:00
|
|
|
$this->assertArrayHasKey(
|
2016-12-16 05:34:21 +01:00
|
|
|
'silverstripe\\core\\tests\\classinfotest',
|
|
|
|
$classes,
|
|
|
|
'ClassInfo::classes_for_folder() returns classes matching the filename'
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
2017-09-19 06:55:39 +02:00
|
|
|
ClassInfoTest::class,
|
|
|
|
$classes,
|
|
|
|
'ClassInfo::classes_for_folder() returns classes matching the filename'
|
|
|
|
);
|
|
|
|
$this->assertArrayHasKey(
|
2016-12-16 05:34:21 +01:00
|
|
|
'silverstripe\\core\\tests\\classinfotest\\baseclass',
|
|
|
|
$classes,
|
|
|
|
'ClassInfo::classes_for_folder() returns additional classes not matching the filename'
|
|
|
|
);
|
2017-09-19 06:55:39 +02:00
|
|
|
$this->assertContains(
|
|
|
|
BaseClass::class,
|
|
|
|
$classes,
|
|
|
|
'ClassInfo::classes_for_folder() returns additional classes not matching the filename'
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \SilverStripe\Core\ClassInfo::ancestry()
|
|
|
|
*/
|
|
|
|
public function testAncestry()
|
|
|
|
{
|
|
|
|
$ancestry = ClassInfo::ancestry(ChildClass::class);
|
2017-09-19 06:55:39 +02:00
|
|
|
$expect = [
|
|
|
|
'silverstripe\\view\\viewabledata' => ViewableData::class,
|
|
|
|
'silverstripe\\orm\\dataobject' => DataObject::class,
|
|
|
|
'silverstripe\\core\tests\classinfotest\\baseclass' => BaseClass::class,
|
|
|
|
'silverstripe\\core\tests\classinfotest\\childclass' => ChildClass::class,
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expect, $ancestry);
|
|
|
|
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals(
|
|
|
|
$expect,
|
|
|
|
ClassInfo::ancestry('silverstripe\\core\\tests\\classINFOtest\\Childclass')
|
|
|
|
);
|
|
|
|
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$ancestry = ClassInfo::ancestry(ChildClass::class, true);
|
|
|
|
$this->assertEquals(
|
2017-09-19 06:55:39 +02:00
|
|
|
[
|
|
|
|
'silverstripe\\core\tests\classinfotest\\baseclass' => BaseClass::class
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$ancestry,
|
|
|
|
'$tablesOnly option excludes memory-only inheritance classes'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \SilverStripe\Core\ClassInfo::dataClassesFor()
|
|
|
|
*/
|
|
|
|
public function testDataClassesFor()
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$expect = [
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\basedataclass' => BaseDataClass::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\hasfields' => HasFields::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\withrelation' => WithRelation::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\withcustomtable' => WithCustomTable::class,
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$classes = array(
|
|
|
|
BaseDataClass::class,
|
|
|
|
NoFields::class,
|
|
|
|
HasFields::class,
|
|
|
|
);
|
|
|
|
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor($classes[0]));
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor(strtoupper($classes[0])));
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor($classes[1]));
|
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
$expect = [
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\basedataclass' => BaseDataClass::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\hasfields' => HasFields::class,
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor($classes[2]));
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor(strtolower($classes[2])));
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|