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