2011-12-17 02:03:53 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Manifest\SS_ClassManifest;
|
|
|
|
use SilverStripe\Core\Manifest\SS_ClassLoader;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
/**
|
|
|
|
* Tests for the {@link SS_ClassManifest} class.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-12-17 02:03:53 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class NamespacedClassManifestTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $base;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SS_ClassManifest
|
|
|
|
*/
|
2011-12-17 02:03:53 +01:00
|
|
|
protected $manifest;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2013-07-08 10:41:46 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->base = dirname(__FILE__) . '/fixtures/namespaced_classmanifest';
|
|
|
|
$this->manifest = new SS_ClassManifest($this->base, false, true, false);
|
2015-02-13 15:34:41 +01:00
|
|
|
SS_ClassLoader::instance()->pushManifest($this->manifest, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
SS_ClassLoader::instance()->popManifest();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetImportedNamespaceParser() {
|
|
|
|
$file = file_get_contents($this->base . DIRECTORY_SEPARATOR . 'module/classes/ClassI.php');
|
|
|
|
$tokens = token_get_all($file);
|
|
|
|
$parsedTokens = SS_ClassManifest::get_imported_namespace_parser()->findAll($tokens);
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
/** @skipUpgrade */
|
2015-02-13 15:34:41 +01:00
|
|
|
$expectedItems = array(
|
2016-08-11 01:40:23 +02:00
|
|
|
array('SilverStripe', '\\', 'Admin', '\\', 'ModelAdmin'),
|
2016-08-19 00:51:35 +02:00
|
|
|
array('SilverStripe', '\\', 'Control', '\\', 'Controller', ' ', 'as', ' ', 'Cont'),
|
2015-02-13 15:34:41 +01:00
|
|
|
array(
|
2016-08-19 00:51:35 +02:00
|
|
|
'SilverStripe', '\\', 'Control', '\\', 'SS_HTTPRequest', ' ', 'as', ' ', 'Request', ',',
|
|
|
|
'SilverStripe', '\\', 'Control', '\\', 'SS_HTTPResponse', ' ', 'as', ' ', 'Response', ',',
|
2016-06-23 01:37:22 +02:00
|
|
|
'SilverStripe', '\\', 'Security', '\\', 'PermissionProvider', ' ', 'as', ' ', 'P',
|
2015-02-13 15:34:41 +01:00
|
|
|
),
|
|
|
|
array('silverstripe', '\\', 'test', '\\', 'ClassA'),
|
2016-08-19 00:51:35 +02:00
|
|
|
array('\\', 'SilverStripe', '\\', 'Core', '\\', 'Object'),
|
2015-02-13 15:34:41 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(count($expectedItems), count($parsedTokens));
|
|
|
|
|
|
|
|
foreach ($expectedItems as $i => $item) {
|
|
|
|
$this->assertEquals($item, $parsedTokens[$i]['importString']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetImportsFromTokens() {
|
|
|
|
$file = file_get_contents($this->base . DIRECTORY_SEPARATOR . 'module/classes/ClassI.php');
|
|
|
|
$tokens = token_get_all($file);
|
|
|
|
|
|
|
|
$method = new ReflectionMethod($this->manifest, 'getImportsFromTokens');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$expectedImports = array(
|
2016-08-11 01:14:02 +02:00
|
|
|
'SilverStripe\\Admin\\ModelAdmin',
|
2016-08-19 00:51:35 +02:00
|
|
|
'Cont' => 'SilverStripe\\Control\\Controller',
|
|
|
|
'Request' => 'SilverStripe\\Control\\SS_HTTPRequest',
|
|
|
|
'Response' => 'SilverStripe\\Control\\SS_HTTPResponse',
|
2016-06-23 01:37:22 +02:00
|
|
|
'P' => 'SilverStripe\\Security\\PermissionProvider',
|
2016-08-19 00:51:35 +02:00
|
|
|
'silverstripe\\test\\ClassA',
|
|
|
|
'\\SilverStripe\\Core\\Object',
|
2015-02-13 15:34:41 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$imports = $method->invoke($this->manifest, $tokens);
|
|
|
|
|
|
|
|
$this->assertEquals($expectedImports, $imports);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClassInfoIsCorrect() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$this->assertContains('SilverStripe\Framework\Tests\ClassI', ClassInfo::implementorsOf('SilverStripe\\Security\\PermissionProvider'));
|
2015-02-13 15:34:41 +01:00
|
|
|
|
|
|
|
//because we're using a nested manifest we have to "coalesce" the descendants again to correctly populate the
|
|
|
|
// descendants of the core classes we want to test against - this is a limitation of the test manifest not
|
|
|
|
// including all core classes
|
|
|
|
$method = new ReflectionMethod($this->manifest, 'coalesceDescendants');
|
|
|
|
$method->setAccessible(true);
|
2016-08-11 01:14:02 +02:00
|
|
|
$method->invoke($this->manifest, 'SilverStripe\\Admin\\ModelAdmin');
|
2015-02-13 15:34:41 +01:00
|
|
|
|
2016-08-11 01:14:02 +02:00
|
|
|
$this->assertContains('SilverStripe\Framework\Tests\ClassI', ClassInfo::subclassesFor('SilverStripe\\Admin\\ModelAdmin'));
|
2015-02-13 15:34:41 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2015-02-13 15:34:41 +01:00
|
|
|
public function testFindClassOrInterfaceFromCandidateImports() {
|
|
|
|
$method = new ReflectionMethod($this->manifest, 'findClassOrInterfaceFromCandidateImports');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$this->assertTrue(ClassInfo::exists('silverstripe\test\ClassA'));
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'PermissionProvider',
|
|
|
|
$method->invokeArgs($this->manifest, [
|
|
|
|
'\PermissionProvider',
|
|
|
|
'Test\Namespace',
|
|
|
|
array(
|
|
|
|
'TestOnly',
|
|
|
|
'Controller',
|
|
|
|
),
|
|
|
|
])
|
|
|
|
);
|
2015-02-13 15:34:41 +01:00
|
|
|
|
|
|
|
$this->assertEquals('PermissionProvider', $method->invokeArgs($this->manifest, array(
|
|
|
|
'PermissionProvider',
|
|
|
|
'Test\NAmespace',
|
|
|
|
array(
|
|
|
|
'PermissionProvider',
|
|
|
|
)
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertEmpty($method->invokeArgs($this->manifest, array(
|
|
|
|
'',
|
|
|
|
'TextNamespace',
|
|
|
|
array(
|
|
|
|
'PermissionProvider',
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertEmpty($method->invokeArgs($this->manifest, array(
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
array()
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertEquals('silverstripe\test\ClassA', $method->invokeArgs($this->manifest, array(
|
|
|
|
'ClassA',
|
|
|
|
'Test\Namespace',
|
|
|
|
array(
|
|
|
|
'silverstripe\test\ClassA',
|
|
|
|
'PermissionProvider',
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertEquals('ClassA', $method->invokeArgs($this->manifest, array(
|
|
|
|
'\ClassA',
|
|
|
|
'Test\Namespace',
|
|
|
|
array(
|
|
|
|
'silverstripe\test',
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertEquals('ClassA', $method->invokeArgs($this->manifest, array(
|
|
|
|
'ClassA',
|
|
|
|
'silverstripe\test',
|
|
|
|
array(
|
|
|
|
'\ClassA',
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertEquals('ClassA', $method->invokeArgs($this->manifest, array(
|
|
|
|
'Alias',
|
|
|
|
'silverstripe\test',
|
|
|
|
array(
|
|
|
|
'Alias' => '\ClassA',
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertEquals('silverstripe\test\ClassA', $method->invokeArgs($this->manifest, array(
|
|
|
|
'ClassA',
|
|
|
|
'silverstripe\test',
|
|
|
|
array(
|
|
|
|
'silverstripe\test\ClassB',
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetItemPath() {
|
|
|
|
$expect = array(
|
2012-03-24 04:38:57 +01:00
|
|
|
'SILVERSTRIPE\TEST\CLASSA' => 'module/classes/ClassA.php',
|
|
|
|
'Silverstripe\Test\ClassA' => 'module/classes/ClassA.php',
|
|
|
|
'silverstripe\test\classa' => 'module/classes/ClassA.php',
|
|
|
|
'SILVERSTRIPE\TEST\INTERFACEA' => 'module/interfaces/InterfaceA.php',
|
|
|
|
'Silverstripe\Test\InterfaceA' => 'module/interfaces/InterfaceA.php',
|
|
|
|
'silverstripe\test\interfacea' => 'module/interfaces/InterfaceA.php'
|
2011-12-17 02:03:53 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($expect as $name => $path) {
|
|
|
|
$this->assertEquals("{$this->base}/$path", $this->manifest->getItemPath($name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetClasses() {
|
|
|
|
$expect = array(
|
2012-03-24 04:38:57 +01:00
|
|
|
'silverstripe\test\classa' => "{$this->base}/module/classes/ClassA.php",
|
|
|
|
'silverstripe\test\classb' => "{$this->base}/module/classes/ClassB.php",
|
|
|
|
'silverstripe\test\classc' => "{$this->base}/module/classes/ClassC.php",
|
|
|
|
'silverstripe\test\classd' => "{$this->base}/module/classes/ClassD.php",
|
|
|
|
'silverstripe\test\classe' => "{$this->base}/module/classes/ClassE.php",
|
|
|
|
'silverstripe\test\classf' => "{$this->base}/module/classes/ClassF.php",
|
|
|
|
'silverstripe\test\classg' => "{$this->base}/module/classes/ClassG.php",
|
2013-07-08 10:41:46 +02:00
|
|
|
'silverstripe\test\classh' => "{$this->base}/module/classes/ClassH.php",
|
2016-08-19 00:51:35 +02:00
|
|
|
'sstemplateparser' => FRAMEWORK_PATH."/View/SSTemplateParser.php",
|
|
|
|
'sstemplateparseexception' => FRAMEWORK_PATH."/View/SSTemplateParseException.php",
|
2015-02-13 15:34:41 +01:00
|
|
|
'silverstripe\framework\tests\classi' => "{$this->base}/module/classes/ClassI.php",
|
2011-12-17 02:03:53 +01:00
|
|
|
);
|
2013-07-08 10:41:46 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertEquals($expect, $this->manifest->getClasses());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetClassNames() {
|
|
|
|
$this->assertEquals(
|
2013-07-08 10:41:46 +02:00
|
|
|
array('sstemplateparser', 'sstemplateparseexception', 'silverstripe\test\classa',
|
|
|
|
'silverstripe\test\classb', 'silverstripe\test\classc', 'silverstripe\test\classd',
|
|
|
|
'silverstripe\test\classe', 'silverstripe\test\classf', 'silverstripe\test\classg',
|
2015-02-13 15:34:41 +01:00
|
|
|
'silverstripe\test\classh', 'silverstripe\framework\tests\classi'),
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->manifest->getClassNames());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDescendants() {
|
|
|
|
$expect = array(
|
2012-03-24 04:38:57 +01:00
|
|
|
'silverstripe\test\classa' => array('silverstripe\test\ClassB', 'silverstripe\test\ClassH'),
|
2011-12-17 02:03:53 +01:00
|
|
|
);
|
2013-07-08 10:41:46 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertEquals($expect, $this->manifest->getDescendants());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDescendantsOf() {
|
|
|
|
$expect = array(
|
2012-03-24 04:38:57 +01:00
|
|
|
'SILVERSTRIPE\TEST\CLASSA' => array('silverstripe\test\ClassB', 'silverstripe\test\ClassH'),
|
|
|
|
'silverstripe\test\classa' => array('silverstripe\test\ClassB', 'silverstripe\test\ClassH'),
|
2011-12-17 02:03:53 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($expect as $class => $desc) {
|
|
|
|
$this->assertEquals($desc, $this->manifest->getDescendantsOf($class));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInterfaces() {
|
|
|
|
$expect = array(
|
2012-03-24 04:38:57 +01:00
|
|
|
'silverstripe\test\interfacea' => "{$this->base}/module/interfaces/InterfaceA.php",
|
2011-12-17 02:03:53 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals($expect, $this->manifest->getInterfaces());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetImplementors() {
|
|
|
|
$expect = array(
|
2012-03-24 04:38:57 +01:00
|
|
|
'silverstripe\test\interfacea' => array('silverstripe\test\ClassE'),
|
|
|
|
'interfacea' => array('silverstripe\test\ClassF'),
|
2015-02-13 15:34:41 +01:00
|
|
|
'silverstripe\test\subtest\interfacea' => array('silverstripe\test\ClassG'),
|
2016-06-23 01:37:22 +02:00
|
|
|
'silverstripe\security\permissionprovider' => array('SilverStripe\Framework\Tests\ClassI'),
|
2011-12-17 02:03:53 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals($expect, $this->manifest->getImplementors());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetImplementorsOf() {
|
|
|
|
$expect = array(
|
2012-03-24 04:38:57 +01:00
|
|
|
'SILVERSTRIPE\TEST\INTERFACEA' => array('silverstripe\test\ClassE'),
|
|
|
|
'silverstripe\test\interfacea' => array('silverstripe\test\ClassE'),
|
|
|
|
'INTERFACEA' => array('silverstripe\test\ClassF'),
|
|
|
|
'interfacea' => array('silverstripe\test\ClassF'),
|
|
|
|
'SILVERSTRIPE\TEST\SUBTEST\INTERFACEA' => array('silverstripe\test\ClassG'),
|
|
|
|
'silverstripe\test\subtest\interfacea' => array('silverstripe\test\ClassG'),
|
2011-12-17 02:03:53 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($expect as $interface => $impl) {
|
|
|
|
$this->assertEquals($impl, $this->manifest->getImplementorsOf($interface));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetConfigs() {
|
|
|
|
$expect = array("{$this->base}/module/_config.php");
|
|
|
|
$this->assertEquals($expect, $this->manifest->getConfigs());
|
|
|
|
}
|
2013-07-08 10:41:46 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
public function testGetModules() {
|
2013-04-17 13:14:30 +02:00
|
|
|
$expect = array(
|
|
|
|
"module" => "{$this->base}/module",
|
|
|
|
"moduleb" => "{$this->base}/moduleb"
|
|
|
|
);
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertEquals($expect, $this->manifest->getModules());
|
|
|
|
}
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|