ioc.test.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. class IoCTest extends PHPUnit_Framework_TestCase {
  28. /**
  29. * Test IoC::register and IoC::resolve.
  30. *
  31. * @group laravel
  32. */
  33. public function testRegisteredClassCanBeResolved()
  34. {
  35. IoC::register('foo', function()
  36. {
  37. return 'Taylor';
  38. });
  39. $this->assertEquals('Taylor', IoC::resolve('foo'));
  40. }
  41. /**
  42. * Test that singletons are created once.
  43. *
  44. * @group laravel
  45. */
  46. public function testSingletonsAreCreatedOnce()
  47. {
  48. IoC::singleton('foo', function()
  49. {
  50. return new StdClass;
  51. });
  52. $object = IoC::resolve('foo');
  53. $this->assertTrue($object === IoC::resolve('foo'));
  54. }
  55. /**
  56. * Test the IoC::instance method.
  57. *
  58. * @group laravel
  59. */
  60. public function testInstancesAreReturnedBySingleton()
  61. {
  62. $object = new StdClass;
  63. IoC::instance('bar', $object);
  64. $this->assertTrue($object === IoC::resolve('bar'));
  65. }
  66. /**
  67. * Test the IoC::registered method.
  68. */
  69. public function testRegisteredMethodIndicatesIfRegistered()
  70. {
  71. IoC::register('foo', function() {});
  72. $this->assertTrue(IoC::registered('foo'));
  73. $this->assertFalse(IoC::registered('baz'));
  74. }
  75. /**
  76. * Test the IoC::controller method.
  77. *
  78. * @group laravel
  79. */
  80. public function testControllerMethodRegistersAController()
  81. {
  82. IoC::register('controller: ioc.test', function() {});
  83. $this->assertTrue(IoC::registered('controller: ioc.test'));
  84. }
  85. /**
  86. * Test that classes with optional parameters can resolve
  87. */
  88. public function testOptionalParamClassResolves()
  89. {
  90. $test = IoC::resolve('TestOptionalParamClassForIoC');
  91. $this->assertInstanceOf('TestOptionalParamClassForIoC', $test);
  92. }
  93. /**
  94. * Test that we can resolve TestClassOneForIoC using IoC
  95. */
  96. public function testClassOneForIoCResolves()
  97. {
  98. $test = IoC::resolve('TestClassOneForIoC');
  99. $this->assertInstanceOf('TestClassOneForIoC', $test);
  100. }
  101. /**
  102. * Test that we can resolve TestClassTwoForIoC
  103. */
  104. public function testClassTwoForIoCResolves()
  105. {
  106. $test = IoC::resolve('TestClassTwoForIoC');
  107. $this->assertInstanceOf('TestClassTwoForIoC', $test);
  108. }
  109. /**
  110. * Test that when we resolve TestClassTwoForIoC we auto resolve
  111. * the dependency for TestClassOneForIoC
  112. */
  113. public function testClassTwoResolvesClassOneDependency()
  114. {
  115. $test = IoC::resolve('TestClassTwoForIoC');
  116. $this->assertInstanceOf('TestClassOneForIoC', $test->class_one);
  117. }
  118. /**
  119. * Test that when we resolve TestClassTwoForIoC with a parameter
  120. * that it actually uses that instead of a blank class TestClassOneForIoC
  121. */
  122. public function testClassTwoResolvesClassOneWithArgument()
  123. {
  124. $class_one = IoC::resolve('TestClassOneForIoC');
  125. $class_one->test_variable = 42;
  126. $class_two = IoC::resolve('TestClassTwoForIoC', array($class_one));
  127. $this->assertEquals(42, $class_two->class_one->test_variable);
  128. }
  129. }