Skip to content

Commit

Permalink
fix ContainerProxy does not clones wrapped container on its own clo…
Browse files Browse the repository at this point in the history
…ning
  • Loading branch information
klimov-paul committed Aug 3, 2023
1 parent 355feca commit 938e20a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,12 @@
Yii1 Dependency Injection extension
===================================

1.0.1 Under Development
-----------------------

- Bug: Fixed `ContainerProxy` does not clones wrapped container on its own cloning (klimov-paul)


1.0.0, July 28, 2023
--------------------

Expand Down
11 changes: 11 additions & 0 deletions src/external/ContainerProxy.php
Expand Up @@ -170,6 +170,17 @@ public function __unset($name)
unset($this->container->$name);
}

/**
* Clones wrapped container instance.
* Do not call this method. This is a PHP magic method that invoked automatically after object has been cloned.
*
* @since 1.0.1
*/
public function __clone()
{
$this->container = clone $this->container;
}

/**
* Creates new self instance.
* This method can be useful when writing chain methods calls.
Expand Down
22 changes: 22 additions & 0 deletions tests/external/ContainerProxyTest.php
Expand Up @@ -86,4 +86,26 @@ public function testCallbackForGet(): void
$object = $proxy->get('any');
$this->assertNull($object);
}

/**
* @depends testForwardCall
*/
public function testClone(): void
{
$container = new Container();

$proxy = new ContainerProxy($container);

$proxy->instance('before-clone', 'before-clone-data');

$proxyClone = clone $proxy;

$proxyClone->instance('after-clone', 'after-clone-data');

$this->assertTrue($proxyClone->has('before-clone'));
$this->assertTrue($proxyClone->has('after-clone'));

$this->assertTrue($proxy->has('before-clone'));
$this->assertFalse($proxy->has('after-clone'));
}
}

0 comments on commit 938e20a

Please sign in to comment.