test_autoloader.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. require_once 'autoloader.php';
  3. use WebBasics\Autoloader;
  4. define('PATH', 'tests/_files/');
  5. class AutoloaderTest extends PHPUnit_Framework_TestCase {
  6. function setUp() {
  7. $this->autoloader = new Autoloader(PATH);
  8. }
  9. function test_set_root_namespace() {
  10. $this->assertAttributeEquals('\\', 'root_namespace', $this->autoloader);
  11. $this->autoloader->set_root_namespace('Foo');
  12. $this->assertAttributeEquals('Foo\\', 'root_namespace', $this->autoloader);
  13. $this->autoloader->set_root_namespace('Foo\\');
  14. $this->assertAttributeEquals('Foo\\', 'root_namespace', $this->autoloader);
  15. }
  16. /**
  17. * @depends test_set_root_namespace
  18. */
  19. function test_get_root_namespace() {
  20. $this->autoloader->set_root_namespace('Foo');
  21. $this->assertEquals($this->autoloader->get_root_namespace(), 'Foo\\');
  22. }
  23. /**
  24. * @depends test_set_root_namespace
  25. */
  26. function test_strip_root_namespace() {
  27. $strip = new ReflectionMethod('WebBasics\Autoloader', 'strip_root_namespace');
  28. $strip->setAccessible(true);
  29. $this->autoloader->set_root_namespace('Foo');
  30. $this->assertEquals($strip->invoke($this->autoloader, 'Foo\Bar'), 'Bar');
  31. }
  32. function test_path_with_slash() {
  33. $this->assertEquals(Autoloader::path_with_slash('dirname'), 'dirname/');
  34. $this->assertEquals(Autoloader::path_with_slash('dirname/'), 'dirname/');
  35. }
  36. /**
  37. * @depends test_path_with_slash
  38. */
  39. function test_set_root_directory() {
  40. $this->autoloader->set_root_directory('tests');
  41. $this->assertEquals($this->autoloader->get_root_directory(), 'tests/');
  42. }
  43. function test_classname_to_filename() {
  44. $this->assertEquals(Autoloader::classname_to_filename('Foo'), 'foo');
  45. $this->assertEquals(Autoloader::classname_to_filename('FooBar'), 'foo_bar');
  46. $this->assertEquals(Autoloader::classname_to_filename('fooBar'), 'foo_bar');
  47. $this->assertEquals(Autoloader::classname_to_filename('FooBarBaz'), 'foo_bar_baz');
  48. }
  49. /**
  50. * @depends test_classname_to_filename
  51. */
  52. function test_create_path() {
  53. $this->assertEquals($this->autoloader->create_path('Foo'), PATH.'foo.php');
  54. $this->assertEquals($this->autoloader->create_path('\Foo'), PATH.'foo.php');
  55. $this->assertEquals($this->autoloader->create_path('Foo\Bar'), PATH.'foo/bar.php');
  56. $this->assertEquals($this->autoloader->create_path('Foo\Bar\Baz'), PATH.'foo/bar/baz.php');
  57. $this->assertEquals($this->autoloader->create_path('FooBar\Baz'), PATH.'foo_bar/baz.php');
  58. }
  59. function test_throw_errors() {
  60. $this->assertFalse($this->autoloader->get_throw_errors());
  61. $this->autoloader->set_throw_errors(true);
  62. $this->assertTrue($this->autoloader->get_throw_errors());
  63. }
  64. /**
  65. * @depends test_create_path
  66. * @depends test_throw_errors
  67. */
  68. function test_load_class_not_found() {
  69. $this->assertFalse($this->autoloader->load_class('foobar'));
  70. }
  71. /**
  72. * @depends test_load_class_not_found
  73. * @expectedException WebBasics\FileNotFoundError
  74. * @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
  75. */
  76. function test_load_class_not_found_error() {
  77. $this->autoloader->set_throw_errors(true);
  78. $this->autoloader->load_class('foobar');
  79. }
  80. /**
  81. * @depends test_load_class_not_found
  82. * @expectedException WebBasics\FileNotFoundError
  83. * @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
  84. */
  85. function test_load_class_not_found_noerror_overwrite() {
  86. $this->autoloader->load_class('foobar', true);
  87. }
  88. /**
  89. * @depends test_load_class_not_found
  90. */
  91. function test_load_class_not_found_error_overwrite() {
  92. $this->autoloader->set_throw_errors(true);
  93. $this->assertFalse($this->autoloader->load_class('foobar', false));
  94. }
  95. /**
  96. * @depends test_load_class_not_found
  97. */
  98. function test_load_class() {
  99. $this->assertTrue($this->autoloader->load_class('Foo'));
  100. $this->assertTrue(class_exists('Foo', false));
  101. $this->assertTrue($this->autoloader->load_class('Foo\Bar'));
  102. $this->assertTrue(class_exists('Foo\Bar', false));
  103. }
  104. /**
  105. * @depends test_load_class
  106. * @depends test_strip_root_namespace
  107. */
  108. function test_load_class_root_namespace() {
  109. $autoloader = new Autoloader(PATH.'foo');
  110. $autoloader->set_root_namespace('Foo');
  111. $this->assertTrue($autoloader->load_class('Bar'));
  112. $this->assertTrue(class_exists('Foo\Bar', false));
  113. }
  114. /**
  115. * @depends test_load_class
  116. */
  117. function test_register() {
  118. $this->autoloader->register();
  119. $this->assertTrue(class_exists('Baz'));
  120. }
  121. /**
  122. * @depends test_register
  123. * @depends test_throw_errors
  124. */
  125. function test_register_prepend() {
  126. $second_loader = new Autoloader(PATH.'second');
  127. $this->autoloader->register();
  128. $second_loader->register(true); // Prepend so that the second loader attemps to load Bar first
  129. $this->assertInstanceOf('Foo', new FooBaz());
  130. }
  131. }
  132. ?>