test_autoloader.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. require_once 'SingletonTestCase.php';
  3. require_once 'autoloader.php';
  4. use webbasics\Autoloader;
  5. define('PATH', 'tests/_files/');
  6. class AutoloaderTest extends SingletonTestCase {
  7. function getClassName() {
  8. return 'webbasics\Autoloader';
  9. }
  10. function tearDown() {
  11. Autoloader::getInstance()->setThrowExceptions(false);
  12. }
  13. function testStripNamespace() {
  14. $rmethod = new ReflectionMethod('webbasics\Autoloader', 'stripNamespace');
  15. $rmethod->setAccessible(true);
  16. $this->assertEquals('Bar', $rmethod->invoke(null, 'foo', 'foo\Bar'));
  17. $this->assertEquals('Bar', $rmethod->invoke(null, '\foo', '\foo\Bar'));
  18. }
  19. function testAddDirectory() {
  20. $autoloader = Autoloader::getInstance();
  21. $rprop = new ReflectionProperty($autoloader, 'directories');
  22. $rprop->setAccessible(true);
  23. $autoloader->addDirectory(PATH);
  24. $this->assertEquals(array(
  25. '\\' => array(PATH)
  26. ), $rprop->getValue($autoloader));
  27. $autoloader->addDirectory(PATH);
  28. $this->assertEquals(array(
  29. '\\' => array(PATH)
  30. ), $rprop->getValue($autoloader));
  31. $autoloader->addDirectory('foo');
  32. $this->assertEquals(array(
  33. '\\' => array(PATH, 'foo/')
  34. ), $rprop->getValue($autoloader));
  35. $autoloader->addDirectory('bar');
  36. $this->assertEquals(array(
  37. '\\' => array(PATH, 'foo/', 'bar/')
  38. ), $rprop->getValue($autoloader));
  39. $autoloader->addDirectory('foodir', 'foo');
  40. $this->assertEquals(array(
  41. '\\' => array(PATH, 'foo/', 'bar/'),
  42. '\foo' => array('foodir/')
  43. ), $rprop->getValue($autoloader));
  44. $autoloader->addDirectory('foobardir', 'foobar');
  45. $this->assertEquals(array(
  46. '\\' => array(PATH, 'foo/', 'bar/'),
  47. '\foo' => array('foodir/'),
  48. '\foobar' => array('foobardir/')
  49. ), $rprop->getValue($autoloader));
  50. }
  51. function testCreatePath() {
  52. $rmethod = new ReflectionMethod('webbasics\Autoloader', 'createPath');
  53. $rmethod->setAccessible(true);
  54. $this->assertEquals($rmethod->invoke(null, 'Foo'), 'Foo.php');
  55. $this->assertEquals($rmethod->invoke(null, '\Foo'), 'Foo.php');
  56. $this->assertEquals($rmethod->invoke(null, 'foo\Bar'), 'foo/Bar.php');
  57. $this->assertEquals($rmethod->invoke(null, 'foo\Bar\Baz'), 'foo/Bar/Baz.php');
  58. $this->assertEquals($rmethod->invoke(null, 'fooBar\Baz'), 'fooBar/Baz.php');
  59. $this->assertEquals($rmethod->invoke(null, 'foo_bar\Baz'), 'foo_bar/Baz.php');
  60. }
  61. function testThrowExceptions() {
  62. $autoloader = Autoloader::getInstance();
  63. $this->assertFalse($autoloader->getThrowExceptions());
  64. $autoloader->setThrowExceptions(true);
  65. $this->assertTrue($autoloader->getThrowExceptions());
  66. }
  67. /**
  68. * @depends testCreatePath
  69. * @depends testThrowExceptions
  70. */
  71. function testLoadClassNotFound() {
  72. $this->assertFalse(Autoloader::getInstance()->loadClass('foobar'));
  73. }
  74. /**
  75. * @depends testLoadClassNotFound
  76. * @expectedException webbasics\ClassNotFoundError
  77. */
  78. function testLoadClassNotFoundException() {
  79. $autoloader = Autoloader::getInstance();
  80. $autoloader->setThrowExceptions(true);
  81. $autoloader->loadClass('foobar');
  82. }
  83. /**
  84. * @depends testLoadClassNotFound
  85. */
  86. function testLoadClassSuccess() {
  87. $autoloader = Autoloader::getInstance();
  88. $this->assertTrue($autoloader->loadClass('Foo'));
  89. $this->assertTrue(class_exists('Foo', false));
  90. $this->assertTrue($autoloader->loadClass('Foo\Bar'));
  91. $this->assertTrue(class_exists('Foo\Bar', false));
  92. }
  93. }
  94. ?>