DependencyContainerTest.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/DependencyContainer.php';
  4. class One {
  5. public $arg1, $arg2;
  6. public function __construct($arg1 = null, $arg2 = null) {
  7. $this->arg1 = $arg1;
  8. $this->arg2 = $arg2;
  9. }
  10. }
  11. class Swift_DependencyContainerTest extends Swift_Tests_SwiftUnitTestCase
  12. {
  13. private $_container;
  14. public function setUp()
  15. {
  16. $this->_container = new Swift_DependencyContainer();
  17. }
  18. public function testRegisterAndLookupValue()
  19. {
  20. $this->_container->register('foo')->asValue('bar');
  21. $this->assertIdentical('bar', $this->_container->lookup('foo'));
  22. }
  23. public function testHasReturnsTrueForRegisteredValue()
  24. {
  25. $this->_container->register('foo')->asValue('bar');
  26. $this->assertTrue($this->_container->has('foo'));
  27. }
  28. public function testHasReturnsFalseForUnregisteredValue()
  29. {
  30. $this->assertFalse($this->_container->has('foo'));
  31. }
  32. public function testRegisterAndLookupNewInstance()
  33. {
  34. $this->_container->register('one')->asNewInstanceOf('One');
  35. $this->assertIsA($this->_container->lookup('one'), 'One');
  36. }
  37. public function testHasReturnsTrueForRegisteredInstance()
  38. {
  39. $this->_container->register('one')->asNewInstanceOf('One');
  40. $this->assertTrue($this->_container->has('one'));
  41. }
  42. public function testNewInstanceIsAlwaysNew()
  43. {
  44. $this->_container->register('one')->asNewInstanceOf('One');
  45. $a = $this->_container->lookup('one');
  46. $b = $this->_container->lookup('one');
  47. $this->assertClone($a, $b);
  48. }
  49. public function testRegisterAndLookupSharedInstance()
  50. {
  51. $this->_container->register('one')->asSharedInstanceOf('One');
  52. $this->assertIsA($this->_container->lookup('one'), 'One');
  53. }
  54. public function testHasReturnsTrueForSharedInstance()
  55. {
  56. $this->_container->register('one')->asSharedInstanceOf('One');
  57. $this->assertTrue($this->_container->has('one'));
  58. }
  59. public function testMultipleSharedInstancesAreSameInstance()
  60. {
  61. $this->_container->register('one')->asSharedInstanceOf('One');
  62. $a = $this->_container->lookup('one');
  63. $b = $this->_container->lookup('one');
  64. $this->assertSame($a, $b);
  65. }
  66. public function testNewInstanceWithDependencies()
  67. {
  68. $this->_container->register('foo')->asValue('FOO');
  69. $this->_container->register('one')->asNewInstanceOf('One')
  70. ->withDependencies(array('foo'));
  71. $obj = $this->_container->lookup('one');
  72. $this->assertIdentical('FOO', $obj->arg1);
  73. }
  74. public function testNewInstanceWithMultipleDependencies()
  75. {
  76. $this->_container->register('foo')->asValue('FOO');
  77. $this->_container->register('bar')->asValue(42);
  78. $this->_container->register('one')->asNewInstanceOf('One')
  79. ->withDependencies(array('foo', 'bar'));
  80. $obj = $this->_container->lookup('one');
  81. $this->assertIdentical('FOO', $obj->arg1);
  82. $this->assertIdentical(42, $obj->arg2);
  83. }
  84. public function testNewInstanceWithInjectedObjects()
  85. {
  86. $this->_container->register('foo')->asValue('FOO');
  87. $this->_container->register('one')->asNewInstanceOf('One');
  88. $this->_container->register('two')->asNewInstanceOf('One')
  89. ->withDependencies(array('one', 'foo'));
  90. $obj = $this->_container->lookup('two');
  91. $this->assertClone($this->_container->lookup('one'), $obj->arg1);
  92. $this->assertIdentical('FOO', $obj->arg2);
  93. }
  94. public function testNewInstanceWithAddConstructorValue()
  95. {
  96. $this->_container->register('one')->asNewInstanceOf('One')
  97. ->addConstructorValue('x')
  98. ->addConstructorValue(99);
  99. $obj = $this->_container->lookup('one');
  100. $this->assertIdentical('x', $obj->arg1);
  101. $this->assertIdentical(99, $obj->arg2);
  102. }
  103. public function testNewInstanceWithAddConstructorLookup()
  104. {
  105. $this->_container->register('foo')->asValue('FOO');
  106. $this->_container->register('bar')->asValue(42);
  107. $this->_container->register('one')->asNewInstanceOf('One')
  108. ->addConstructorLookup('foo')
  109. ->addConstructorLookup('bar');
  110. $obj = $this->_container->lookup('one');
  111. $this->assertIdentical('FOO', $obj->arg1);
  112. $this->assertIdentical(42, $obj->arg2);
  113. }
  114. public function testResolvedDependenciesCanBeLookedUp()
  115. {
  116. $this->_container->register('foo')->asValue('FOO');
  117. $this->_container->register('one')->asNewInstanceOf('One');
  118. $this->_container->register('two')->asNewInstanceOf('One')
  119. ->withDependencies(array('one', 'foo'));
  120. $deps = $this->_container->createDependenciesFor('two');
  121. $this->assertEqual(
  122. array($this->_container->lookup('one'), 'FOO'), $deps
  123. );
  124. }
  125. public function testArrayOfDependenciesCanBeSpecified()
  126. {
  127. $this->_container->register('foo')->asValue('FOO');
  128. $this->_container->register('one')->asNewInstanceOf('One');
  129. $this->_container->register('two')->asNewInstanceOf('One')
  130. ->withDependencies(array(array('one', 'foo'), 'foo'));
  131. $obj = $this->_container->lookup('two');
  132. $this->assertEqual(array($this->_container->lookup('one'), 'FOO'), $obj->arg1);
  133. $this->assertIdentical('FOO', $obj->arg2);
  134. }
  135. public function testAliasCanBeSet()
  136. {
  137. $this->_container->register('foo')->asValue('FOO');
  138. $this->_container->register('bar')->asAliasOf('foo');
  139. $this->assertIdentical('FOO', $this->_container->lookup('bar'));
  140. }
  141. public function testAliasOfAliasCanBeSet()
  142. {
  143. $this->_container->register('foo')->asValue('FOO');
  144. $this->_container->register('bar')->asAliasOf('foo');
  145. $this->_container->register('zip')->asAliasOf('bar');
  146. $this->_container->register('button')->asAliasOf('zip');
  147. $this->assertIdentical('FOO', $this->_container->lookup('button'));
  148. }
  149. }