Skip to content

Commit

Permalink
fix(Container) bind container and resolver correctly in clone
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Apr 26, 2024
1 parent 9f714b3 commit 6e125c4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ to [Semantic Versioning](http://semver.org/).

## [unreleased] Unreleased

### Fixed

- Correctly bind the container to the builders and resolvers when cloning the container.

## [3.3.6] 2024-04-02;

### Added
Expand Down
24 changes: 24 additions & 0 deletions src/Builders/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,28 @@ public function getBuilder($id, $implementation = null, array $afterBuildMethods

return new ValueBuilder($implementation);
}

/**
* Sets the container the builder should use.
*
* @since TBD
*
* @param Container $container The container to bind.
*
* @return void
*/
public function setContainer( Container $container ) {
$this->container = $container;
}

/**
* Sets the resolver the container should use.
*
* @since TBD
*
* @param Resolver $resolver The resolver the container should use.
*/
public function setResolver( Resolver $resolver ) {
$this->resolver = $resolver;
}
}
2 changes: 2 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,8 @@ public function __clone()
{
$this->resolver = clone $this->resolver;
$this->builders = clone $this->builders;
$this->builders->setContainer($this);
$this->builders->setResolver($this->resolver);
$this->bindThis();
}
}
52 changes: 50 additions & 2 deletions tests/unit/CloneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,28 @@
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

class ContainerExtension extends Container
{
class ContainerExtension extends Container {
}

class CloneTestFoo {
private $baz;

public function __construct( CloneTestBazInterface $baz ) {
$this->baz = $baz;
}

public function getBaz(): CloneTestBazInterface {
return $this->baz;
}
}

interface CloneTestBazInterface {}

class CloneTestBazOne implements CloneTestBazInterface {

}

class CloneTestBazTwo implements CloneTestBazInterface {
}

class CloneTest extends TestCase
Expand Down Expand Up @@ -98,4 +118,32 @@ public function should_bind_the_clone_as_singleton_when_container_class_extended
$this->assertSame($clone, $clone->get(ContainerExtension::class));
$this->assertSame($clone, $clone->get(ContainerInterface::class));
}

/**
* It should use the cloned container to build
*
* @test
*/
public function should_use_the_cloned_container_to_build(): void {
$original = new Container();
$clone = clone $original;

$this->assertNotSame( $original, $clone );

$original->singleton( CloneTestFoo::class );
$original->singleton( CloneTestBazInterface::class, CloneTestBazOne::class );

$clone->singleton( CloneTestFoo::class );
$clone->singleton( CloneTestBazInterface::class, CloneTestBazTwo::class );

$this->assertNotSame( $original->get( CloneTestFoo::class ), $clone->get( CloneTestFoo::class ) );
$this->assertNotSame( $original->get( CloneTestBazInterface::class ),
$clone->get( CloneTestBazInterface::class ) );
$this->assertInstanceOf( CloneTestBazOne::class, $original->get( CloneTestBazInterface::class ) );
$this->assertInstanceOf( CloneTestBazTwo::class, $clone->get( CloneTestBazInterface::class ) );
$this->assertInstanceOf( CloneTestBazOne::class, $original->get( CloneTestFoo::class )->getBaz() );
$this->assertInstanceOf( CloneTestBazTwo::class, $clone->get( CloneTestFoo::class )->getBaz() );
$this->assertNotSame( $original->get( CloneTestFoo::class )->getBaz(),
$clone->get( CloneTestFoo::class )->getBaz() );
}
}

0 comments on commit 6e125c4

Please sign in to comment.