2013-09-26 21:44:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Composer\Test\Autoload;
|
|
|
|
|
|
|
|
use Composer\Autoload\ClassLoader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the Composer\Autoload\ClassLoader class.
|
|
|
|
*/
|
|
|
|
class ClassLoaderTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2015-04-30 17:17:19 +00:00
|
|
|
public function testLoadClassDotPhp()
|
|
|
|
{
|
|
|
|
$loader = new ClassLoader();
|
|
|
|
$loader->add('DirDotPhp\\', __DIR__ . '/Fixtures');
|
|
|
|
$loader->addPsr4('DirDotPhp\\', __DIR__ . '/Fixtures/DirDotPhp/psr4');
|
|
|
|
|
|
|
|
$class = 'DirDotPhp\\Dir';
|
|
|
|
$loader->loadClass($class);
|
|
|
|
$this->assertTrue(class_exists($class, false), "->loadClass() loads '$class'.");
|
|
|
|
}
|
|
|
|
|
2013-09-26 21:44:21 +00:00
|
|
|
/**
|
|
|
|
* Tests regular PSR-0 and PSR-4 class loading.
|
|
|
|
*
|
|
|
|
* @dataProvider getLoadClassTests
|
|
|
|
*
|
2014-06-10 14:02:44 +00:00
|
|
|
* @param string $class The fully-qualified class name to test, without preceding namespace separator.
|
|
|
|
* @param bool $prependSeparator Whether to call ->loadClass() with a class name with preceding
|
|
|
|
* namespace separator, as it happens in PHP 5.3.0 - 5.3.2. See https://bugs.php.net/50731
|
2013-09-26 21:44:21 +00:00
|
|
|
*/
|
2014-10-17 17:46:01 +00:00
|
|
|
public function testLoadClass($class, $prependSeparator = false)
|
2013-09-26 21:44:21 +00:00
|
|
|
{
|
|
|
|
$loader = new ClassLoader();
|
|
|
|
$loader->add('Namespaced\\', __DIR__ . '/Fixtures');
|
|
|
|
$loader->add('Pearlike_', __DIR__ . '/Fixtures');
|
|
|
|
$loader->addPsr4('ShinyVendor\\ShinyPackage\\', __DIR__ . '/Fixtures');
|
|
|
|
|
|
|
|
if ($prependSeparator) {
|
2014-01-03 15:31:05 +00:00
|
|
|
$prepend = '\\';
|
|
|
|
$message = "->loadClass() loads '$class'.";
|
|
|
|
} else {
|
|
|
|
$prepend = '';
|
|
|
|
$message = "->loadClass() loads '\\$class', as required in PHP 5.3.0 - 5.3.2.";
|
2013-09-26 21:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$loader->loadClass($prepend . $class);
|
|
|
|
$this->assertTrue(class_exists($class, false), $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides arguments for ->testLoadClass().
|
|
|
|
*
|
2014-01-03 15:31:05 +00:00
|
|
|
* @return array Array of parameter sets to test with.
|
2013-09-26 21:44:21 +00:00
|
|
|
*/
|
|
|
|
public function getLoadClassTests()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('Namespaced\\Foo'),
|
|
|
|
array('Pearlike_Foo'),
|
|
|
|
array('ShinyVendor\\ShinyPackage\\SubNamespace\\Foo'),
|
|
|
|
// "Bar" would not work here, since it is defined in a ".inc" file,
|
|
|
|
// instead of a ".php" file. So, use "Baz" instead.
|
2014-01-03 15:31:05 +00:00
|
|
|
array('Namespaced\\Baz', true),
|
|
|
|
array('Pearlike_Bar', true),
|
|
|
|
array('ShinyVendor\\ShinyPackage\\SubNamespace\\Bar', true),
|
2013-09-26 21:44:21 +00:00
|
|
|
);
|
|
|
|
}
|
2014-11-16 13:24:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getPrefixes method should return empty array if ClassLoader does not have any psr-0 configuration
|
|
|
|
*/
|
|
|
|
public function testGetPrefixesWithNoPSR0Configuration()
|
|
|
|
{
|
|
|
|
$loader = new ClassLoader();
|
|
|
|
$this->assertEmpty($loader->getPrefixes());
|
|
|
|
}
|
2013-09-26 21:44:21 +00:00
|
|
|
}
|