test_autoloader.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. require_once 'autoloader.php';
  3. use BasicWeb\Autoloader;
  4. define('PATH', 'tests/_files/');
  5. class AutoloaderTest extends PHPUnit_Framework_TestCase {
  6. var $autoloader;
  7. function setUp() {
  8. $this->autoloader = new Autoloader(PATH);
  9. }
  10. function tearDown() {
  11. unset($this->autoloader);
  12. }
  13. function test_path_with_slash() {
  14. $this->assertEquals(Autoloader::path_with_slash('dirname'), 'dirname/');
  15. $this->assertEquals(Autoloader::path_with_slash('dirname/'), 'dirname/');
  16. }
  17. /**
  18. * @depends test_path_with_slash
  19. */
  20. function test_set_root_directory() {
  21. $this->autoloader->set_root_directory('tests');
  22. $this->assertEquals($this->autoloader->get_root_directory(), 'tests/');
  23. }
  24. function test_classname_to_filename() {
  25. $this->assertEquals(Autoloader::classname_to_filename('Foo'), 'foo');
  26. $this->assertEquals(Autoloader::classname_to_filename('FooBar'), 'foo_bar');
  27. $this->assertEquals(Autoloader::classname_to_filename('fooBar'), 'foo_bar');
  28. $this->assertEquals(Autoloader::classname_to_filename('FooBarBaz'), 'foo_bar_baz');
  29. }
  30. /**
  31. * @depends test_classname_to_filename
  32. */
  33. function test_create_path() {
  34. $this->assertEquals($this->autoloader->create_path('Foo'), PATH.'foo.php');
  35. $this->assertEquals($this->autoloader->create_path('\Foo'), PATH.'foo.php');
  36. $this->assertEquals($this->autoloader->create_path('Foo\Bar'), PATH.'foo/bar.php');
  37. $this->assertEquals($this->autoloader->create_path('Foo\Bar\Baz'), PATH.'foo/bar/baz.php');
  38. $this->assertEquals($this->autoloader->create_path('FooBar\Baz'), PATH.'foo_bar/baz.php');
  39. }
  40. /**
  41. * @depends test_create_path
  42. * @expectedException BasicWeb\FileNotFoundError
  43. * @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
  44. */
  45. function test_load_class_not_found() {
  46. $this->autoloader->load_class('foobar');
  47. }
  48. /**
  49. * @depends test_load_class_not_found
  50. */
  51. function test_load_class() {
  52. $this->assertTrue($this->autoloader->load_class('Foo'));
  53. $this->assertTrue(class_exists('Foo', false));
  54. $this->assertTrue($this->autoloader->load_class('Foo\Bar'));
  55. $this->assertTrue(class_exists('Foo\Bar', false));
  56. }
  57. /**
  58. * @depends test_load_class
  59. */
  60. function test_register() {
  61. $this->autoloader->register();
  62. $this->assertTrue(class_exists('Baz'));
  63. }
  64. function test_throw_errors() {
  65. $this->assertTrue($this->autoloader->get_throw_errors());
  66. $this->autoloader->set_throw_errors(false);
  67. $this->assertFalse($this->autoloader->get_throw_errors());
  68. }
  69. /**
  70. * @depends test_register
  71. * @depends test_throw_errors
  72. */
  73. function test_register_prepend() {
  74. $second_loader = new Autoloader(PATH.'second');
  75. $this->autoloader->register();
  76. $second_loader->register(true); // Prepend so that the second loader attemps to load Bar first
  77. $second_loader->set_throw_errors(false);
  78. $this->assertInstanceOf('Foo', new FooBaz());
  79. }
  80. }
  81. ?>