DependencyContainerTest.php 6.0KB

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