test_autoloader.php 2.7 KB

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