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 bfb35e8
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 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
28 changes: 28 additions & 0 deletions src/Builders/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,32 @@ 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.
*
* @return void
*/
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();
}
}
61 changes: 61 additions & 0 deletions tests/unit/CloneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ class ContainerExtension extends Container
{
}

class CloneTestFoo
{
private $baz;

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

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

interface CloneTestBazInterface
{
}

class CloneTestBazOne implements CloneTestBazInterface
{

}

class CloneTestBazTwo implements CloneTestBazInterface
{
}

class CloneTest extends TestCase
{
/**
Expand Down Expand Up @@ -98,4 +126,37 @@ 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()
{
$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 bfb35e8

Please sign in to comment.