2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2013-09-26 21:44:21 +00:00
|
|
|
|
2015-10-13 09:34:02 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2013-09-26 21:44:21 +00:00
|
|
|
namespace Composer\Test\Autoload;
|
|
|
|
|
|
|
|
use Composer\Autoload\ClassLoader;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2013-09-26 21:44:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the Composer\Autoload\ClassLoader class.
|
|
|
|
*/
|
2017-11-04 14:52:13 +00:00
|
|
|
class ClassLoaderTest extends TestCase
|
2013-09-26 21:44:21 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Tests regular PSR-0 and PSR-4 class loading.
|
|
|
|
*
|
|
|
|
* @dataProvider getLoadClassTests
|
|
|
|
*
|
2017-03-08 14:07:29 +00:00
|
|
|
* @param string $class The fully-qualified class name to test, without preceding namespace separator.
|
2013-09-26 21:44:21 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testLoadClass(string $class): void
|
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');
|
2016-10-06 18:54:09 +00:00
|
|
|
$loader->loadClass($class);
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue(class_exists($class, false), "->loadClass() loads '$class'");
|
2013-09-26 21:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides arguments for ->testLoadClass().
|
|
|
|
*
|
2021-10-26 19:48:56 +00:00
|
|
|
* @return array<array<string>> Array of parameter sets to test with.
|
2013-09-26 21:44:21 +00:00
|
|
|
*/
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function getLoadClassTests(): array
|
2013-09-26 21:44:21 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['Namespaced\\Foo'],
|
|
|
|
['Pearlike_Foo'],
|
|
|
|
['ShinyVendor\\ShinyPackage\\SubNamespace\\Foo'],
|
|
|
|
];
|
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
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetPrefixesWithNoPSR0Configuration(): void
|
2014-11-16 13:24:44 +00:00
|
|
|
{
|
|
|
|
$loader = new ClassLoader();
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEmpty($loader->getPrefixes());
|
2014-11-16 13:24:44 +00:00
|
|
|
}
|
2022-12-22 14:17:04 +00:00
|
|
|
|
|
|
|
public function testSerializability(): void
|
|
|
|
{
|
|
|
|
$loader = new ClassLoader();
|
|
|
|
$loader->add('Pearlike_', __DIR__ . '/Fixtures');
|
|
|
|
$loader->add('', __DIR__ . '/FALLBACK');
|
|
|
|
$loader->addPsr4('ShinyVendor\\ShinyPackage\\', __DIR__ . '/Fixtures');
|
|
|
|
$loader->addPsr4('', __DIR__ . '/FALLBACKPSR4');
|
|
|
|
$loader->addClassMap(['A' => '', 'B' => 'path']);
|
|
|
|
$loader->setApcuPrefix('prefix');
|
|
|
|
$loader->setClassMapAuthoritative(true);
|
|
|
|
$loader->setUseIncludePath(true);
|
|
|
|
|
|
|
|
$loader2 = unserialize(serialize($loader));
|
|
|
|
self::assertInstanceOf(ClassLoader::class, $loader2);
|
|
|
|
self::assertSame($loader->getApcuPrefix(), $loader2->getApcuPrefix());
|
|
|
|
self::assertSame($loader->getClassMap(), $loader2->getClassMap());
|
|
|
|
self::assertSame($loader->getFallbackDirs(), $loader2->getFallbackDirs());
|
|
|
|
self::assertSame($loader->getFallbackDirsPsr4(), $loader2->getFallbackDirsPsr4());
|
|
|
|
self::assertSame($loader->getPrefixes(), $loader2->getPrefixes());
|
|
|
|
self::assertSame($loader->getPrefixesPsr4(), $loader2->getPrefixesPsr4());
|
|
|
|
self::assertSame($loader->getUseIncludePath(), $loader2->getUseIncludePath());
|
|
|
|
}
|
2013-09-26 21:44:21 +00:00
|
|
|
}
|