123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace Illuminate\Tests\Container;
- use Illuminate\Container\Container;
- use PHPUnit\Framework\TestCase;
- use stdClass;
- class ContainerExtendTest extends TestCase
- {
- public function testExtendedBindings()
- {
- $container = new Container;
- $container['foo'] = 'foo';
- $container->extend('foo', function ($old, $container) {
- return $old.'bar';
- });
- $this->assertSame('foobar', $container->make('foo'));
- $container = new Container;
- $container->singleton('foo', function () {
- return (object) ['name' => 'taylor'];
- });
- $container->extend('foo', function ($old, $container) {
- $old->age = 26;
- return $old;
- });
- $result = $container->make('foo');
- $this->assertSame('taylor', $result->name);
- $this->assertEquals(26, $result->age);
- $this->assertSame($result, $container->make('foo'));
- }
- public function testExtendInstancesArePreserved()
- {
- $container = new Container;
- $container->bind('foo', function () {
- $obj = new stdClass;
- $obj->foo = 'bar';
- return $obj;
- });
- $obj = new stdClass;
- $obj->foo = 'foo';
- $container->instance('foo', $obj);
- $container->extend('foo', function ($obj, $container) {
- $obj->bar = 'baz';
- return $obj;
- });
- $container->extend('foo', function ($obj, $container) {
- $obj->baz = 'foo';
- return $obj;
- });
- $this->assertSame('foo', $container->make('foo')->foo);
- $this->assertSame('baz', $container->make('foo')->bar);
- $this->assertSame('foo', $container->make('foo')->baz);
- }
- public function testExtendIsLazyInitialized()
- {
- ContainerLazyExtendStub::$initialized = false;
- $container = new Container;
- $container->bind(ContainerLazyExtendStub::class);
- $container->extend(ContainerLazyExtendStub::class, function ($obj, $container) {
- $obj->init();
- return $obj;
- });
- $this->assertFalse(ContainerLazyExtendStub::$initialized);
- $container->make(ContainerLazyExtendStub::class);
- $this->assertTrue(ContainerLazyExtendStub::$initialized);
- }
- public function testExtendCanBeCalledBeforeBind()
- {
- $container = new Container;
- $container->extend('foo', function ($old, $container) {
- return $old.'bar';
- });
- $container['foo'] = 'foo';
- $this->assertSame('foobar', $container->make('foo'));
- }
- public function testExtendInstanceRebindingCallback()
- {
- $_SERVER['_test_rebind'] = false;
- $container = new Container;
- $container->rebinding('foo', function () {
- $_SERVER['_test_rebind'] = true;
- });
- $obj = new stdClass;
- $container->instance('foo', $obj);
- $container->extend('foo', function ($obj, $container) {
- return $obj;
- });
- $this->assertTrue($_SERVER['_test_rebind']);
- }
- public function testExtendBindRebindingCallback()
- {
- $_SERVER['_test_rebind'] = false;
- $container = new Container;
- $container->rebinding('foo', function () {
- $_SERVER['_test_rebind'] = true;
- });
- $container->bind('foo', function () {
- return new stdClass;
- });
- $this->assertFalse($_SERVER['_test_rebind']);
- $container->make('foo');
- $container->extend('foo', function ($obj, $container) {
- return $obj;
- });
- $this->assertTrue($_SERVER['_test_rebind']);
- }
- public function testExtensionWorksOnAliasedBindings()
- {
- $container = new Container;
- $container->singleton('something', function () {
- return 'some value';
- });
- $container->alias('something', 'something-alias');
- $container->extend('something-alias', function ($value) {
- return $value.' extended';
- });
- $this->assertSame('some value extended', $container->make('something'));
- }
- public function testMultipleExtends()
- {
- $container = new Container;
- $container['foo'] = 'foo';
- $container->extend('foo', function ($old, $container) {
- return $old.'bar';
- });
- $container->extend('foo', function ($old, $container) {
- return $old.'baz';
- });
- $this->assertSame('foobarbaz', $container->make('foo'));
- }
- public function testUnsetExtend()
- {
- $container = new Container;
- $container->bind('foo', function () {
- $obj = new stdClass;
- $obj->foo = 'bar';
- return $obj;
- });
- $container->extend('foo', function ($obj, $container) {
- $obj->bar = 'baz';
- return $obj;
- });
- unset($container['foo']);
- $container->forgetExtenders('foo');
- $container->bind('foo', function () {
- return 'foo';
- });
- $this->assertSame('foo', $container->make('foo'));
- }
- }
- class ContainerLazyExtendStub
- {
- public static $initialized = false;
- public function init()
- {
- static::$initialized = true;
- }
- }
|