test_autoloader.php 4.8 KB

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