View source
<?php
namespace Drupal\xautoload\Tests;
class XAutoloadUnitTestCase extends \DrupalUnitTestCase implements PublicAssertInterface {
static function getInfo() {
return array(
'name' => 'X Autoload unit test',
'description' => 'Test the xautoload class finder.',
'group' => 'X Autoload',
);
}
function assertPublic($status, $message) {
return $this
->assert($status, $message);
}
function setUp() {
require_once dirname(__FILE__) . '/../../../../xautoload.early.inc';
xautoload('loaderManager')
->register('default', TRUE);
parent::setUp();
}
function testAutoloadStackOrder() {
$expected = array(
'xautoload_ClassLoader_NoCache->loadClass()',
'drupal_autoload_class',
'drupal_autoload_interface',
'_simpletest_autoload_psr0',
);
$msg = 'spl_autoload_functions():';
foreach (spl_autoload_functions() as $index => $callback) {
$str = $this
->callbackToString($callback);
if (!isset($expected[$index])) {
$this
->fail("Autoload callback at index {$index} must be empty instead of {$str}.");
}
else {
$expected_str = $expected[$index];
if ($expected_str === $str) {
$this
->pass("Autoload callback at index {$index} must be {$expected_str}.");
}
else {
$this
->fail("Autoload callback at index {$index} must be {$expected_str} instead of {$str}.");
}
}
}
}
protected function callbackToString($callback) {
if (is_array($callback)) {
list($obj, $method) = $callback;
if (is_object($obj)) {
$str = get_class($obj) . '->' . $method . '()';
}
else {
$str = $obj . '::' . $method . '()';
}
}
else {
$str = $callback;
}
return $str;
}
function testNamespaces() {
$finder = new \xautoload_ClassFinder_NamespaceOrPrefix();
$finder
->registerNamespaceDeep('Drupal\\ex_ample', 'sites/all/modules/contrib/ex_ample/lib');
$finder
->registerNamespaceRoot('Drupal\\ex_ample', 'sites/all/modules/contrib/ex_ample/vendor');
$this
->assertFinderSuggestions($finder, 'Drupal\\ex_ample\\Abc_Def', array(
'sites/all/modules/contrib/ex_ample/lib/Abc/Def.php',
'sites/all/modules/contrib/ex_ample/vendor/Drupal/ex_ample/Abc/Def.php',
));
}
function testPrefixes() {
$finder = new \xautoload_ClassFinder_NamespaceOrPrefix();
$finder
->registerPrefixDeep('ex_ample', 'sites/all/modules/contrib/ex_ample/lib');
$finder
->registerPrefixRoot('ex_ample', 'sites/all/modules/contrib/ex_ample/vendor');
$this
->assertFinderSuggestions($finder, 'ex_ample_Abc_Def', array(
'sites/all/modules/contrib/ex_ample/lib/Abc/Def.php',
'sites/all/modules/contrib/ex_ample/vendor/ex/ample/Abc/Def.php',
));
}
protected function assertFinderSuggestions($finder, $class, array $expectedSuggestions) {
for ($iAccept = 0; $iAccept < count($expectedSuggestions); ++$iAccept) {
$api = new \xautoload_Mock_InjectedAPI_findFile($this, $class, $expectedSuggestions, $iAccept);
$finder
->findFile($api, $class);
$api
->finish();
}
$api = new \xautoload_Mock_InjectedAPI_findFile($this, $class, $expectedSuggestions);
$finder
->findFile($api, $class);
$api
->finish();
$this
->assert(TRUE, "Successfully loaded {$class}");
}
}