3
namespace Doctrine\Tests\Common;
5
use Doctrine\Common\ClassLoader;
7
class ClassLoaderTest extends \Doctrine\Tests\DoctrineTestCase
9
public function testClassLoader()
11
$classLoader = new ClassLoader('ClassLoaderTest');
12
$classLoader->setIncludePath(__DIR__);
13
$classLoader->setFileExtension('.class.php');
14
$classLoader->setNamespaceSeparator('_');
16
$this->assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassA'));
17
$this->assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassB'));
18
$this->assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassC'));
19
$this->assertFalse($classLoader->canLoadClass('OtherClass'));
20
$this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), true);
21
$this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassB'), true);
22
$this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassC'), true);
25
public function testClassExists()
27
$this->assertFalse(ClassLoader::classExists('ClassLoaderTest\ClassD'));
28
$badLoader = function($className) {
29
require __DIR__ . '/ClassLoaderTest/ClassD.php';
32
spl_autoload_register($badLoader);
33
$this->assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassD'));
34
spl_autoload_unregister($badLoader);
37
public function testGetClassLoader()
39
$cl = new ClassLoader('ClassLoaderTest', __DIR__);
41
$this->assertTrue(ClassLoader::getClassLoader('ClassLoaderTest\ClassD') instanceof \Doctrine\Common\ClassLoader);
42
$this->assertNull(ClassLoader::getClassLoader('This\Class\Does\Not\Exist'));
46
public function testClassExistsWithSilentAutoloader()
49
$silentLoader = function ($className) use ($test) {
50
$test->assertSame('ClassLoaderTest\ClassE', $className);
51
require __DIR__ . '/ClassLoaderTest/ClassE.php';
53
$additionalLoader = function () use ($test) {
54
$test->fail('Should not call this loader, class was already loaded');
57
$this->assertFalse(ClassLoader::classExists('ClassLoaderTest\ClassE'));
58
spl_autoload_register($silentLoader);
59
spl_autoload_register($additionalLoader);
60
$this->assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassE'));
61
spl_autoload_unregister($additionalLoader);
62
spl_autoload_unregister($silentLoader);