Boilerplate to use a Directus Instance to Build a Custom Website, Content will be Manage by Directus
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.2 KiB

5 years ago
  1. <?php
  2. /**
  3. * Flight: An extensible micro-framework.
  4. *
  5. * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
  6. * @license MIT, http://flightphp.com/license
  7. */
  8. require_once 'vendor/autoload.php';
  9. require_once __DIR__.'/classes/User.php';
  10. require_once __DIR__.'/classes/Factory.php';
  11. class LoaderTest extends PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var \flight\core\Loader
  15. */
  16. private $loader;
  17. function setUp(){
  18. $this->loader = new \flight\core\Loader();
  19. $this->loader->autoload(true, __DIR__.'/classes');
  20. }
  21. // Autoload a class
  22. function testAutoload(){
  23. $this->loader->register('tests', 'User');
  24. $test = $this->loader->load('tests');
  25. $this->assertTrue(is_object($test));
  26. $this->assertEquals('User', get_class($test));
  27. }
  28. // Register a class
  29. function testRegister(){
  30. $this->loader->register('a', 'User');
  31. $user = $this->loader->load('a');
  32. $this->assertTrue(is_object($user));
  33. $this->assertEquals('User', get_class($user));
  34. $this->assertEquals('', $user->name);
  35. }
  36. // Register a class with constructor parameters
  37. function testRegisterWithConstructor(){
  38. $this->loader->register('b', 'User', array('Bob'));
  39. $user = $this->loader->load('b');
  40. $this->assertTrue(is_object($user));
  41. $this->assertEquals('User', get_class($user));
  42. $this->assertEquals('Bob', $user->name);
  43. }
  44. // Register a class with initialization
  45. function testRegisterWithInitialization(){
  46. $this->loader->register('c', 'User', array('Bob'), function($user){
  47. $user->name = 'Fred';
  48. });
  49. $user = $this->loader->load('c');
  50. $this->assertTrue(is_object($user));
  51. $this->assertEquals('User', get_class($user));
  52. $this->assertEquals('Fred', $user->name);
  53. }
  54. // Get a non-shared instance of a class
  55. function testSharedInstance() {
  56. $this->loader->register('d', 'User');
  57. $user1 = $this->loader->load('d');
  58. $user2 = $this->loader->load('d');
  59. $user3 = $this->loader->load('d', false);
  60. $this->assertTrue($user1 === $user2);
  61. $this->assertTrue($user1 !== $user3);
  62. }
  63. // Gets an object from a factory method
  64. function testRegisterUsingCallable(){
  65. $this->loader->register('e', array('Factory','create'));
  66. $obj = $this->loader->load('e');
  67. $this->assertTrue(is_object($obj));
  68. $this->assertEquals('Factory', get_class($obj));
  69. $obj2 = $this->loader->load('e');
  70. $this->assertTrue(is_object($obj2));
  71. $this->assertEquals('Factory', get_class($obj2));
  72. $this->assertTrue($obj === $obj2);
  73. $obj3 = $this->loader->load('e', false);
  74. $this->assertTrue(is_object($obj3));
  75. $this->assertEquals('Factory', get_class($obj3));
  76. $this->assertTrue($obj !== $obj3);
  77. }
  78. // Gets an object from a callback function
  79. function testRegisterUsingCallback(){
  80. $this->loader->register('f', function(){
  81. return Factory::create();
  82. });
  83. $obj = $this->loader->load('f');
  84. $this->assertTrue(is_object($obj));
  85. $this->assertEquals('Factory', get_class($obj));
  86. }
  87. }