2011-03-18 07:35:20 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
namespace SilverStripe\Core\Tests\Manifest;
|
|
|
|
|
2017-03-14 03:20:51 +01:00
|
|
|
use Exception;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Core\Manifest\ClassManifest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2011-03-18 07:35:20 +01:00
|
|
|
/**
|
2016-09-09 08:43:05 +02:00
|
|
|
* Tests for the {@link ClassManifest} class.
|
2011-03-18 07:35:20 +01:00
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class ClassManifestTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
2017-03-14 03:20:51 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $base;
|
2017-03-14 03:20:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ClassManifest
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $manifest;
|
2017-03-14 03:20:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ClassManifest
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $manifestTests;
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->base = dirname(__FILE__) . '/fixtures/classmanifest';
|
2017-09-21 07:54:06 +02:00
|
|
|
$this->manifest = new ClassManifest($this->base);
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->manifest->init(false);
|
|
|
|
$this->manifestTests = new ClassManifest($this->base);
|
|
|
|
$this->manifestTests->init(true);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-09-21 07:54:06 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerTestGetItemPath()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-09-21 07:54:06 +02:00
|
|
|
return [
|
|
|
|
['CLASSA', 'module/classes/ClassA.php'],
|
|
|
|
['ClassA', 'module/classes/ClassA.php'],
|
|
|
|
['classa', 'module/classes/ClassA.php'],
|
|
|
|
['INTERFACEA', 'module/interfaces/InterfaceA.php'],
|
|
|
|
['InterfaceA', 'module/interfaces/InterfaceA.php'],
|
|
|
|
['interfacea', 'module/interfaces/InterfaceA.php'],
|
|
|
|
['TestTraitA', 'module/traits/TestTraitA.php'],
|
|
|
|
['TestNamespace\\Testing\\TestTraitB', 'module/traits/TestTraitB.php'],
|
|
|
|
['VendorClassA', 'vendor/silverstripe/modulec/code/VendorClassA.php'],
|
|
|
|
['VendorTraitA', 'vendor/silverstripe/modulec/code/VendorTraitA.php'],
|
|
|
|
];
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-09-21 07:54:06 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider providerTestGetItemPath
|
|
|
|
* @param string $name
|
|
|
|
* @param string $path
|
|
|
|
*/
|
|
|
|
public function testGetItemPath($name, $path)
|
|
|
|
{
|
|
|
|
$this->assertEquals("{$this->base}/$path", $this->manifest->getItemPath($name));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetClasses()
|
|
|
|
{
|
2017-09-21 07:54:06 +02:00
|
|
|
$expect = [
|
|
|
|
'classa' => "{$this->base}/module/classes/ClassA.php",
|
|
|
|
'classb' => "{$this->base}/module/classes/ClassB.php",
|
|
|
|
'classc' => "{$this->base}/module/classes/ClassC.php",
|
|
|
|
'classd' => "{$this->base}/module/classes/ClassD.php",
|
|
|
|
'classe' => "{$this->base}/module/classes/ClassE.php",
|
|
|
|
'vendorclassa' => "{$this->base}/vendor/silverstripe/modulec/code/VendorClassA.php",
|
2020-09-09 03:47:36 +02:00
|
|
|
'vendorclassx' => "{$this->base}/vendor/silverstripe/modulecbetter/code/VendorClassX.php",
|
2017-09-21 07:54:06 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expect, $this->manifest->getClasses());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetClassNames()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-09-19 06:55:39 +02:00
|
|
|
[
|
|
|
|
'classa' => 'ClassA',
|
|
|
|
'classb' => 'ClassB',
|
|
|
|
'classc' => 'ClassC',
|
|
|
|
'classd' => 'ClassD',
|
|
|
|
'classe' => 'ClassE',
|
2017-09-21 07:54:06 +02:00
|
|
|
'vendorclassa' => 'VendorClassA',
|
2020-09-09 03:47:36 +02:00
|
|
|
'vendorclassx' => 'VendorClassX',
|
2017-09-19 06:55:39 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->manifest->getClassNames()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetTraitNames()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-09-21 07:54:06 +02:00
|
|
|
[
|
2017-09-19 06:55:39 +02:00
|
|
|
'testtraita' => 'TestTraitA',
|
2017-09-21 07:54:06 +02:00
|
|
|
'testnamespace\\testing\\testtraitb' => 'TestNamespace\\Testing\\TestTraitB',
|
|
|
|
'vendortraita' => 'VendorTraitA',
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->manifest->getTraitNames()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDescendants()
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$expect = [
|
|
|
|
'classa' => [
|
|
|
|
'classc' => 'ClassC',
|
|
|
|
'classd' => 'ClassD',
|
|
|
|
],
|
|
|
|
'classc' => [
|
|
|
|
'classd' => 'ClassD',
|
|
|
|
],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expect, $this->manifest->getDescendants());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDescendantsOf()
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$expect = [
|
|
|
|
'CLASSA' => ['classc' => 'ClassC', 'classd' => 'ClassD'],
|
|
|
|
'classa' => ['classc' => 'ClassC', 'classd' => 'ClassD'],
|
|
|
|
'CLASSC' => ['classd' => 'ClassD'],
|
|
|
|
'classc' => ['classd' => 'ClassD'],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
foreach ($expect as $class => $desc) {
|
|
|
|
$this->assertEquals($desc, $this->manifest->getDescendantsOf($class));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInterfaces()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$expect = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'interfacea' => "{$this->base}/module/interfaces/InterfaceA.php",
|
|
|
|
'interfaceb' => "{$this->base}/module/interfaces/InterfaceB.php"
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expect, $this->manifest->getInterfaces());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetImplementors()
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$expect = [
|
|
|
|
'interfacea' => ['classb' => 'ClassB'],
|
|
|
|
'interfaceb' => ['classc' => 'ClassC'],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expect, $this->manifest->getImplementors());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetImplementorsOf()
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$expect = [
|
|
|
|
'INTERFACEA' => ['classb' => 'ClassB'],
|
|
|
|
'interfacea' => ['classb' => 'ClassB'],
|
|
|
|
'INTERFACEB' => ['classc' => 'ClassC'],
|
|
|
|
'interfaceb' => ['classc' => 'ClassC'],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
foreach ($expect as $interface => $impl) {
|
|
|
|
$this->assertEquals($impl, $this->manifest->getImplementorsOf($interface));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTestManifestIncludesTestClasses()
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$this->assertArrayNotHasKey('testclassa', $this->manifest->getClasses());
|
|
|
|
$this->assertArrayHasKey('testclassa', $this->manifestTests->getClasses());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testManifestExcludeFilesPrefixedWithUnderscore()
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$this->assertArrayNotHasKey('ignore', $this->manifest->getClasses());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that ClassManifest throws an exception when it encounters two files
|
|
|
|
* which contain classes with the same name
|
|
|
|
*/
|
|
|
|
public function testManifestWarnsAboutDuplicateClasses()
|
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
$this->expectException(Exception::class);
|
2017-06-22 12:50:45 +02:00
|
|
|
$manifest = new ClassManifest(dirname(__FILE__) . '/fixtures/classmanifest_duplicates');
|
|
|
|
$manifest->init();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|