ContainerExtendTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Illuminate\Tests\Container;
  3. use Illuminate\Container\Container;
  4. use PHPUnit\Framework\TestCase;
  5. use stdClass;
  6. class ContainerExtendTest extends TestCase
  7. {
  8. public function testExtendedBindings()
  9. {
  10. $container = new Container;
  11. $container['foo'] = 'foo';
  12. $container->extend('foo', function ($old, $container) {
  13. return $old.'bar';
  14. });
  15. $this->assertSame('foobar', $container->make('foo'));
  16. $container = new Container;
  17. $container->singleton('foo', function () {
  18. return (object) ['name' => 'taylor'];
  19. });
  20. $container->extend('foo', function ($old, $container) {
  21. $old->age = 26;
  22. return $old;
  23. });
  24. $result = $container->make('foo');
  25. $this->assertSame('taylor', $result->name);
  26. $this->assertEquals(26, $result->age);
  27. $this->assertSame($result, $container->make('foo'));
  28. }
  29. public function testExtendInstancesArePreserved()
  30. {
  31. $container = new Container;
  32. $container->bind('foo', function () {
  33. $obj = new stdClass;
  34. $obj->foo = 'bar';
  35. return $obj;
  36. });
  37. $obj = new stdClass;
  38. $obj->foo = 'foo';
  39. $container->instance('foo', $obj);
  40. $container->extend('foo', function ($obj, $container) {
  41. $obj->bar = 'baz';
  42. return $obj;
  43. });
  44. $container->extend('foo', function ($obj, $container) {
  45. $obj->baz = 'foo';
  46. return $obj;
  47. });
  48. $this->assertSame('foo', $container->make('foo')->foo);
  49. $this->assertSame('baz', $container->make('foo')->bar);
  50. $this->assertSame('foo', $container->make('foo')->baz);
  51. }
  52. public function testExtendIsLazyInitialized()
  53. {
  54. ContainerLazyExtendStub::$initialized = false;
  55. $container = new Container;
  56. $container->bind(ContainerLazyExtendStub::class);
  57. $container->extend(ContainerLazyExtendStub::class, function ($obj, $container) {
  58. $obj->init();
  59. return $obj;
  60. });
  61. $this->assertFalse(ContainerLazyExtendStub::$initialized);
  62. $container->make(ContainerLazyExtendStub::class);
  63. $this->assertTrue(ContainerLazyExtendStub::$initialized);
  64. }
  65. public function testExtendCanBeCalledBeforeBind()
  66. {
  67. $container = new Container;
  68. $container->extend('foo', function ($old, $container) {
  69. return $old.'bar';
  70. });
  71. $container['foo'] = 'foo';
  72. $this->assertSame('foobar', $container->make('foo'));
  73. }
  74. public function testExtendInstanceRebindingCallback()
  75. {
  76. $_SERVER['_test_rebind'] = false;
  77. $container = new Container;
  78. $container->rebinding('foo', function () {
  79. $_SERVER['_test_rebind'] = true;
  80. });
  81. $obj = new stdClass;
  82. $container->instance('foo', $obj);
  83. $container->extend('foo', function ($obj, $container) {
  84. return $obj;
  85. });
  86. $this->assertTrue($_SERVER['_test_rebind']);
  87. }
  88. public function testExtendBindRebindingCallback()
  89. {
  90. $_SERVER['_test_rebind'] = false;
  91. $container = new Container;
  92. $container->rebinding('foo', function () {
  93. $_SERVER['_test_rebind'] = true;
  94. });
  95. $container->bind('foo', function () {
  96. return new stdClass;
  97. });
  98. $this->assertFalse($_SERVER['_test_rebind']);
  99. $container->make('foo');
  100. $container->extend('foo', function ($obj, $container) {
  101. return $obj;
  102. });
  103. $this->assertTrue($_SERVER['_test_rebind']);
  104. }
  105. public function testExtensionWorksOnAliasedBindings()
  106. {
  107. $container = new Container;
  108. $container->singleton('something', function () {
  109. return 'some value';
  110. });
  111. $container->alias('something', 'something-alias');
  112. $container->extend('something-alias', function ($value) {
  113. return $value.' extended';
  114. });
  115. $this->assertSame('some value extended', $container->make('something'));
  116. }
  117. public function testMultipleExtends()
  118. {
  119. $container = new Container;
  120. $container['foo'] = 'foo';
  121. $container->extend('foo', function ($old, $container) {
  122. return $old.'bar';
  123. });
  124. $container->extend('foo', function ($old, $container) {
  125. return $old.'baz';
  126. });
  127. $this->assertSame('foobarbaz', $container->make('foo'));
  128. }
  129. public function testUnsetExtend()
  130. {
  131. $container = new Container;
  132. $container->bind('foo', function () {
  133. $obj = new stdClass;
  134. $obj->foo = 'bar';
  135. return $obj;
  136. });
  137. $container->extend('foo', function ($obj, $container) {
  138. $obj->bar = 'baz';
  139. return $obj;
  140. });
  141. unset($container['foo']);
  142. $container->forgetExtenders('foo');
  143. $container->bind('foo', function () {
  144. return 'foo';
  145. });
  146. $this->assertSame('foo', $container->make('foo'));
  147. }
  148. }
  149. class ContainerLazyExtendStub
  150. {
  151. public static $initialized = false;
  152. public function init()
  153. {
  154. static::$initialized = true;
  155. }
  156. }