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;
|
|
|
|
|
2020-10-23 05:33:56 +02:00
|
|
|
use DateTime;
|
2021-10-27 04:39:47 +02:00
|
|
|
use Exception;
|
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;
|
2020-02-14 03:44:28 +01:00
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\BaseObject;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\ChildClass;
|
2020-02-14 03:44:28 +01:00
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\ExtendTest;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\ExtendTest2;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\ExtendTest3;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\ExtensionTest1;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\ExtensionTest2;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\GrandChildClass;
|
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\HasFields;
|
2020-10-23 05:33:56 +02:00
|
|
|
use SilverStripe\Core\Tests\ClassInfoTest\HasMethod;
|
2016-10-14 03:30:05 +02:00
|
|
|
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
|
|
|
|
{
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
BaseClass::class,
|
|
|
|
BaseDataClass::class,
|
|
|
|
ChildClass::class,
|
|
|
|
GrandChildClass::class,
|
|
|
|
HasFields::class,
|
|
|
|
NoFields::class,
|
|
|
|
WithCustomTable::class,
|
|
|
|
WithRelation::class,
|
2020-02-14 03:44:28 +01:00
|
|
|
BaseObject::class,
|
|
|
|
ExtendTest::class,
|
|
|
|
ExtendTest2::class,
|
|
|
|
ExtendTest3::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
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)
|
|
|
|
);
|
2023-07-29 08:13:45 +02:00
|
|
|
|
|
|
|
// Check that core classes are present (eg: Email subclasses)
|
|
|
|
$emailClasses = ClassInfo::subclassesFor(\SilverStripe\Control\Email\Email::class);
|
|
|
|
$this->assertArrayHasKey(
|
|
|
|
'silverstripe\\control\\tests\\email\\emailtest\\emailsubclass',
|
|
|
|
$emailClasses,
|
|
|
|
'It contains : ' . json_encode($emailClasses)
|
|
|
|
);
|
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()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionMessageMatches('/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,
|
|
|
|
];
|
2020-04-20 19:58:09 +02:00
|
|
|
$classes = [
|
2016-12-16 05:34:21 +01:00
|
|
|
BaseDataClass::class,
|
|
|
|
NoFields::class,
|
|
|
|
HasFields::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor($classes[0]));
|
|
|
|
ClassInfo::reset_db_cache();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor(strtoupper($classes[0] ?? '')));
|
2016-12-16 05:34:21 +01:00
|
|
|
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();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals($expect, ClassInfo::dataClassesFor(strtolower($classes[2] ?? '')));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2020-02-14 03:44:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \SilverStripe\Core\ClassInfo::classesWithExtension()
|
|
|
|
*/
|
|
|
|
public function testClassesWithExtensionUsingConfiguredExtensions()
|
|
|
|
{
|
|
|
|
$expect = [
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\extendtest' => ExtendTest::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\extendtest2' => ExtendTest2::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\extendtest3' => ExtendTest3::class,
|
|
|
|
];
|
|
|
|
$this->assertEquals(
|
|
|
|
$expect,
|
|
|
|
ClassInfo::classesWithExtension(ExtensionTest1::class, BaseObject::class),
|
|
|
|
'ClassInfo::testClassesWithExtension() returns class with extensions applied via class config'
|
|
|
|
);
|
|
|
|
|
|
|
|
$expect = [
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\extendtest' => ExtendTest::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\extendtest2' => ExtendTest2::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\extendtest3' => ExtendTest3::class,
|
|
|
|
];
|
|
|
|
$this->assertEquals(
|
|
|
|
$expect,
|
|
|
|
ClassInfo::classesWithExtension(ExtensionTest1::class, ExtendTest::class, true),
|
|
|
|
'ClassInfo::testClassesWithExtension() returns class with extensions applied via class config, including the base class'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \SilverStripe\Core\ClassInfo::classesWithExtension()
|
|
|
|
*/
|
|
|
|
public function testClassesWithExtensionUsingDynamicallyAddedExtensions()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
[],
|
|
|
|
ClassInfo::classesWithExtension(ExtensionTest2::class, BaseObject::class),
|
|
|
|
'ClassInfo::testClassesWithExtension() returns no classes for extension that hasn\'t been applied yet.'
|
|
|
|
);
|
|
|
|
|
|
|
|
ExtendTest::add_extension(ExtensionTest2::class);
|
|
|
|
|
|
|
|
$expect = [
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\extendtest2' => ExtendTest2::class,
|
|
|
|
'silverstripe\\core\\tests\\classinfotest\\extendtest3' => ExtendTest3::class,
|
|
|
|
];
|
|
|
|
$this->assertEquals(
|
|
|
|
$expect,
|
|
|
|
ClassInfo::classesWithExtension(ExtensionTest2::class, ExtendTest::class),
|
|
|
|
'ClassInfo::testClassesWithExtension() returns class with extra extension dynamically added'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \SilverStripe\Core\ClassInfo::classesWithExtension()
|
|
|
|
*/
|
|
|
|
public function testClassesWithExtensionWithDynamicallyRemovedExtensions()
|
|
|
|
{
|
|
|
|
ExtendTest::remove_extension(ExtensionTest1::class);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
[],
|
|
|
|
ClassInfo::classesWithExtension(ExtensionTest1::class, BaseObject::class),
|
|
|
|
'ClassInfo::testClassesWithExtension() returns no classes after an extension being removed'
|
|
|
|
);
|
|
|
|
}
|
2020-09-30 02:05:15 +02:00
|
|
|
|
2020-10-23 05:33:56 +02:00
|
|
|
/** @dataProvider provideHasMethodCases */
|
|
|
|
public function testHasMethod($object, $method, $output)
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
$output,
|
|
|
|
ClassInfo::hasMethod($object, $method)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideHasMethodCases()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'Basic object' => [
|
|
|
|
new DateTime(),
|
|
|
|
'format',
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'CustomMethod object' => [
|
|
|
|
new HasMethod(),
|
|
|
|
'example',
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'Class Name' => [
|
|
|
|
'DateTime',
|
|
|
|
'format',
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'FQCN' => [
|
|
|
|
'\DateTime',
|
|
|
|
'format',
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'Invalid FQCN' => [
|
|
|
|
'--GreatTime',
|
|
|
|
'format',
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
'Integer' => [
|
|
|
|
1,
|
|
|
|
'format',
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
'Array' => [
|
|
|
|
['\DateTime'],
|
|
|
|
'format',
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideClassSpecCases */
|
2020-09-30 02:05:15 +02:00
|
|
|
public function testParseClassSpec($input, $output)
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
$output,
|
|
|
|
ClassInfo::parse_class_spec($input)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideClassSpecCases()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'Standard class' => [
|
|
|
|
'SimpleClass',
|
|
|
|
['SimpleClass', []],
|
|
|
|
],
|
|
|
|
'Namespaced class' => [
|
|
|
|
'Foo\\Bar\\NamespacedClass',
|
|
|
|
['Foo\\Bar\\NamespacedClass', []],
|
|
|
|
],
|
|
|
|
'Namespaced class with service name' => [
|
|
|
|
'Foo\\Bar\\NamespacedClass.withservicename',
|
|
|
|
['Foo\\Bar\\NamespacedClass.withservicename', []],
|
|
|
|
],
|
|
|
|
'Namespaced class with argument' => [
|
|
|
|
'Foo\\Bar\\NamespacedClass(["with-arg" => true])',
|
|
|
|
['Foo\\Bar\\NamespacedClass', [["with-arg" => true]]],
|
|
|
|
],
|
|
|
|
'Namespaced class with service name and argument' => [
|
|
|
|
'Foo\\Bar\\NamespacedClass.withmodifier(["and-arg" => true])',
|
|
|
|
['Foo\\Bar\\NamespacedClass.withmodifier', [["and-arg" => true]]],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|