ioc.test.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Testing Optional Parameters in classes' Dependency Injection
  4. */
  5. class TestOptionalParamClassForIoC
  6. {
  7. public function __construct($optional_param = 42) {}
  8. }
  9. /**
  10. * Testing Dependency Injection with this class
  11. */
  12. class TestClassOneForIoC
  13. {
  14. public $_variable;
  15. }
  16. /**
  17. * Testing Dependency Injection of ClassOne
  18. */
  19. class TestClassTwoForIoC
  20. {
  21. public $class_one;
  22. public function __construct(TestClassOneForIoC $class_one)
  23. {
  24. $this->class_one = $class_one;
  25. }
  26. }
  27. use \Laravel\IoC as IoC;
  28. class IoCTest extends PHPUnit_Framework_TestCase {
  29. /**
  30. * Test IoC::register and IoC::resolve.
  31. *
  32. * @group laravel
  33. */
  34. public function testRegisteredClassCanBeResolved()
  35. {
  36. IoC::register('foo', function()
  37. {
  38. return 'Taylor';
  39. });
  40. $this->assertEquals('Taylor', IoC::resolve('foo'));
  41. }
  42. /**
  43. * Test that singletons are created once.
  44. *
  45. * @group laravel
  46. */
  47. public function testSingletonsAreCreatedOnce()
  48. {
  49. IoC::singleton('foo', function()
  50. {
  51. return new StdClass;
  52. });
  53. $object = IoC::resolve('foo');
  54. $this->assertTrue($object === IoC::resolve('foo'));
  55. }
  56. /**
  57. * Test the IoC::instance method.
  58. *
  59. * @group laravel
  60. */
  61. public function testInstancesAreReturnedBySingleton()
  62. {
  63. $object = new StdClass;
  64. IoC::instance('bar', $object);
  65. $this->assertTrue($object === IoC::resolve('bar'));
  66. }
  67. /**
  68. * Test the IoC::registered method.
  69. */
  70. public function testRegisteredMethodIndicatesIfRegistered()
  71. {
  72. IoC::register('foo', function() {});
  73. $this->assertTrue(IoC::registered('foo'));
  74. $this->assertFalse(IoC::registered('baz'));
  75. }
  76. /**
  77. * Test the IoC::controller method.
  78. *
  79. * @group laravel
  80. */
  81. public function testControllerMethodRegistersAController()
  82. {
  83. IoC::register('controller: ioc.test', function() {});
  84. $this->assertTrue(IoC::registered('controller: ioc.test'));
  85. }
  86. /**
  87. * Test that classes with optional parameters can resolve
  88. */
  89. public function testOptionalParamClassResolves()
  90. {
  91. $test = IoC::resolve('TestOptionalParamClassForIoC');
  92. $this->assertInstanceOf('TestOptionalParamClassForIoC', $test);
  93. }
  94. /**
  95. * Test that we can resolve TestClassOneForIoC using IoC
  96. */
  97. public function testClassOneForIoCResolves()
  98. {
  99. $test = IoC::resolve('TestClassOneForIoC');
  100. $this->assertInstanceOf('TestClassOneForIoC', $test);
  101. }
  102. /**
  103. * Test that we can resolve TestClassTwoForIoC
  104. */
  105. public function testClassTwoForIoCResolves()
  106. {
  107. $test = IoC::resolve('TestClassTwoForIoC');
  108. $this->assertInstanceOf('TestClassTwoForIoC', $test);
  109. }
  110. /**
  111. * Test that when we resolve TestClassTwoForIoC we auto resolve
  112. * the dependency for TestClassOneForIoC
  113. */
  114. public function testClassTwoResolvesClassOneDependency()
  115. {
  116. $test = IoC::resolve('TestClassTwoForIoC');
  117. $this->assertInstanceOf('TestClassOneForIoC', $test->class_one);
  118. }
  119. /**
  120. * Test that when we resolve TestClassTwoForIoC with a parameter
  121. * that it actually uses that instead of a blank class TestClassOneForIoC
  122. */
  123. public function testClassTwoResolvesClassOneWithArgument()
  124. {
  125. $class_one = IoC::resolve('TestClassOneForIoC');
  126. $class_one->test_variable = 42;
  127. $class_two = IoC::resolve('TestClassTwoForIoC', array($class_one));
  128. $this->assertEquals(42, $class_two->class_one->test_variable);
  129. }
  130. public function testCanUnregisterRegistered()
  131. {
  132. $testClass = 'test';
  133. IoC::register($testClass, function() {});
  134. $this->assertTrue(IoC::registered($testClass));
  135. IoC::unregister($testClass);
  136. $this->assertFalse(IoC::registered($testClass));
  137. }
  138. }