Skip to content

Commit

Permalink
feat: add container builder reference to container
Browse files Browse the repository at this point in the history
  • Loading branch information
lombervid committed Jul 25, 2023
1 parent 1ee3b4b commit e8d068b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Added

- Auto inject container reference into container

## [0.6.0] - 2023-07-25

### Added
Expand Down
6 changes: 6 additions & 0 deletions src/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
use Closure;
use Phetit\DependencyInjection\Resolver\FactoryServiceResolver;
use Phetit\DependencyInjection\Resolver\ServiceResolver;
use Psr\Container\ContainerInterface;

class ContainerBuilder extends Container
{
public function __construct()
{
$this->parameter(ContainerInterface::class, $this);
}

public function register(string $id, Closure $resolver): static
{
$this->set($id, new ServiceResolver($resolver));
Expand Down
15 changes: 13 additions & 2 deletions tests/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
use Phetit\DependencyInjection\ContainerBuilder;
use Phetit\DependencyInjection\Tests\Fixtures\Service;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

class ContainerBuilderTest extends TestCase
{
public function testRegisterMethod(): void
public function testRegisterMethodShouldAddServiceResolver(): void
{
$builder = new ContainerBuilder();

Expand All @@ -25,7 +26,7 @@ public function testRegisterMethod(): void
self::assertSame($serviceOne, $serviceTwo);
}

public function testFactoryMethod(): void
public function testFactoryMethodShouldAddFactoryServiceResolver(): void
{
$builder = new ContainerBuilder();

Expand All @@ -39,4 +40,14 @@ public function testFactoryMethod(): void

self::assertNotSame($serviceOne, $serviceTwo);
}

public function testShouldInjectAutoReference(): void
{
$builder = new ContainerBuilder();

$container = $builder->get(ContainerInterface::class);

self::assertInstanceOf(ContainerBuilder::class, $container);
self::assertSame($builder, $container);
}
}

0 comments on commit e8d068b

Please sign in to comment.