ClassLoaderTest.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Doctrine\Tests\Common;
  3. use Doctrine\Common\ClassLoader;
  4. class ClassLoaderTest extends \Doctrine\Tests\DoctrineTestCase
  5. {
  6. public function testClassLoader()
  7. {
  8. $classLoader = new ClassLoader('ClassLoaderTest');
  9. $classLoader->setIncludePath(__DIR__);
  10. $classLoader->setFileExtension('.class.php');
  11. $classLoader->setNamespaceSeparator('_');
  12. $this->assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassA'));
  13. $this->assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassB'));
  14. $this->assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassC'));
  15. $this->assertFalse($classLoader->canLoadClass('OtherClass'));
  16. $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), true);
  17. $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassB'), true);
  18. $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassC'), true);
  19. }
  20. public function testClassExists()
  21. {
  22. $this->assertFalse(ClassLoader::classExists('ClassLoaderTest\ClassD'));
  23. $badLoader = function($className) {
  24. require __DIR__ . '/ClassLoaderTest/ClassD.php';
  25. return true;
  26. };
  27. spl_autoload_register($badLoader);
  28. $this->assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassD'));
  29. spl_autoload_unregister($badLoader);
  30. }
  31. public function testGetClassLoader()
  32. {
  33. $cl = new ClassLoader('ClassLoaderTest', __DIR__);
  34. $cl->register();
  35. $this->assertTrue(ClassLoader::getClassLoader('ClassLoaderTest\ClassD') instanceof \Doctrine\Common\ClassLoader);
  36. $this->assertNull(ClassLoader::getClassLoader('This\Class\Does\Not\Exist'));
  37. $cl->unregister();
  38. }
  39. }