1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
<?php
namespace Doctrine\Tests\Common;
use ClassLoaderTest\ExternalLoader;
use Doctrine\Common\ClassLoader;
use Doctrine\Tests\DoctrineTestCase;
use function interface_exists;
use function spl_autoload_register;
use function spl_autoload_unregister;
use function trait_exists;
/**
* @group legacy
*/
class ClassLoaderTest extends DoctrineTestCase
{
public function testClassLoader()
{
$classLoader = new ClassLoader('ClassLoaderTest');
$classLoader->setIncludePath(__DIR__);
$classLoader->setFileExtension('.class.php');
$classLoader->setNamespaceSeparator('_');
self::assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassA'));
self::assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassB'));
self::assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassC'));
self::assertFalse($classLoader->canLoadClass('OtherClass'));
self::assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), true);
self::assertEquals($classLoader->loadClass('ClassLoaderTest_ClassB'), true);
self::assertEquals($classLoader->loadClass('ClassLoaderTest_ClassC'), true);
}
public function testClassExists()
{
self::assertFalse(ClassLoader::classExists('ClassLoaderTest\ClassD'));
$badLoader = static function ($className) {
require __DIR__ . '/ClassLoaderTest/ClassD.php';
return true;
};
spl_autoload_register($badLoader);
self::assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassD'));
spl_autoload_unregister($badLoader);
}
public function testGetClassLoader()
{
$cl = new ClassLoader('ClassLoaderTest', __DIR__);
$cl->register();
self::assertTrue(ClassLoader::getClassLoader('ClassLoaderTest\ClassD') instanceof ClassLoader);
self::assertNull(ClassLoader::getClassLoader('This\Class\Does\Not\Exist'));
$cl->unregister();
}
public function testClassExistsWithSilentAutoloader()
{
$test = $this;
$silentLoader = static function ($className) use ($test) {
$test->assertSame('ClassLoaderTest\ClassE', $className);
require __DIR__ . '/ClassLoaderTest/ClassE.php';
};
$additionalLoader = static function () use ($test) {
$test->fail('Should not call this loader, class was already loaded');
};
self::assertFalse(ClassLoader::classExists('ClassLoaderTest\ClassE'));
spl_autoload_register($silentLoader);
spl_autoload_register($additionalLoader);
self::assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassE'));
spl_autoload_unregister($additionalLoader);
spl_autoload_unregister($silentLoader);
}
public function testClassExistsWhenLoaderIsProtected()
{
require_once __DIR__ . '/ClassLoaderTest/ExternalLoader.php';
// Test static call
ExternalLoader::registerStatic();
self::assertFalse(ClassLoader::classExists('ClassLoaderTest\Class\That\Does\Not\Exist'));
ExternalLoader::unregisterStatic();
// Test object
$loader = new ExternalLoader();
$loader->register();
self::assertFalse(ClassLoader::classExists('ClassLoaderTest\Class\That\Does\Not\Exist'));
$loader->unregister();
}
public function testLoadNonExistingClass()
{
$classLoader = new ClassLoader('ClassLoaderTest', __DIR__);
self::assertFalse($classLoader->loadClass('ClassLoaderTest\Non\Existing\ClassName'));
}
public function testLoadFileNotContainingClassClass()
{
$classLoader = new ClassLoader('ClassLoaderTest', __DIR__);
$classLoader->setFileExtension('.class.php');
self::assertFalse($classLoader->loadClass('ClassLoaderTest\EmptyFile'));
}
public function testSupportsInterfaceAutoloading()
{
$classLoader = new ClassLoader();
$classLoader->setIncludePath(__DIR__);
$classLoader->setFileExtension('.class.php');
$classLoader->setNamespaceSeparator('_');
self::assertTrue($classLoader->loadClass('ClassLoaderTest_InterfaceA'));
self::assertTrue(interface_exists('ClassLoaderTest_InterfaceA', false));
}
public function testSupportsTraitAutoloading()
{
$classLoader = new ClassLoader();
$classLoader->setIncludePath(__DIR__);
$classLoader->setFileExtension('.class.php');
$classLoader->setNamespaceSeparator('_');
self::assertTrue($classLoader->loadClass('ClassLoaderTest_TraitA'));
self::assertTrue(trait_exists('ClassLoaderTest_TraitA', false));
}
public function testMultipleAutoloadRequestsWillProduceSameResult()
{
$classLoader = new ClassLoader();
$classLoader->setIncludePath(__DIR__);
$classLoader->setFileExtension('.class.php');
$classLoader->setNamespaceSeparator('_');
self::assertTrue($classLoader->loadClass('ClassLoaderTest_ClassA'));
self::assertTrue($classLoader->loadClass('ClassLoaderTest_ClassA'));
}
}
|