Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Container) bind container and resolver correctly in clone #63

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
);
}
}
Loading