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.

176 satır
5.9KB

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