Skip to content

Commit

Permalink
Fix #384: Make $config parameter in Container constructor optional
Browse files Browse the repository at this point in the history
  • Loading branch information
np25071984 authored Jan 12, 2025
1 parent bc455c0 commit 56168c3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.3.1 under development

- New #380: Add `TagReference::id()` method (@vjik)
- Enh #384: Make `$config` parameter in `Container` constructor optional (@np25071984)

## 1.3.0 October 14, 2024

Expand Down
4 changes: 3 additions & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ final class Container implements ContainerInterface
*
* @throws InvalidConfigException If configuration is not valid.
*/
public function __construct(ContainerConfigInterface $config)
public function __construct(?ContainerConfigInterface $config = null)
{
$config ??= ContainerConfig::create();

$this->definitions = new DefinitionStorage(
[
ContainerInterface::class => $this,
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/CompositePsrContainerOverYiisoftTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ public function testNotFoundException(): void
{
$compositeContainer = new CompositeContainer();

$container1 = new Container(ContainerConfig::create());
$container1 = new Container();
$container1Id = spl_object_id($container1);
$container2 = new Container(ContainerConfig::create());
$container2 = new Container();
$container2Id = spl_object_id($container2);

$compositeContainer->attach($container1);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/CompositePsrContainerTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testTags(): void
public function testDelegateLookup(): void
{
$compositeContainer = new CompositeContainer();
$firstContainer = new Container(ContainerConfig::create());
$firstContainer = new Container();

$config = ContainerConfig::create()
->withDefinitions([
Expand All @@ -155,7 +155,7 @@ public function testDelegateLookup(): void
public function testDelegateLookupUnionTypes(): void
{
$compositeContainer = new CompositeContainer();
$firstContainer = new Container(ContainerConfig::create());
$firstContainer = new Container();

$config = ContainerConfig::create()
->withDefinitions([
Expand Down
35 changes: 21 additions & 14 deletions tests/Unit/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
*/
final class ContainerTest extends TestCase
{
public function testCanCreateWihtoutConfig(): void
{
$this->expectNotToPerformAssertions();

new Container();
}

public function testSettingScalars(): void
{
$this->expectException(InvalidConfigException::class);
Expand Down Expand Up @@ -89,7 +96,7 @@ public function testIntegerKeys(): void

public function testNullableClassDependency(): void
{
$container = new Container(ContainerConfig::create());
$container = new Container();

$this->expectException(NotFoundException::class);
$container->get(NullableConcreteDependency::class);
Expand All @@ -111,7 +118,7 @@ public function testOptionalResolvableClassDependency(): void

public function testOptionalNotResolvableClassDependency(): void
{
$container = new Container(ContainerConfig::create());
$container = new Container();

$this->assertTrue($container->has(OptionalConcreteDependency::class));
$service = $container->get(OptionalConcreteDependency::class);
Expand Down Expand Up @@ -168,14 +175,14 @@ public static function dataUnionTypes(): array
#[DataProvider('dataUnionTypes')]
public function testUnionTypes(string $class): void
{
$container = new Container(ContainerConfig::create());
$container = new Container();

$this->assertTrue($container->has($class));
}

public function testClassExistsButIsNotResolvable(): void
{
$container = new Container(ContainerConfig::create());
$container = new Container();

$this->assertFalse($container->has('non_existing'));
$this->assertFalse($container->has(Car::class));
Expand All @@ -195,14 +202,14 @@ public static function dataClassExistButIsNotResolvableWithUnionTypes(): array
#[DataProvider('dataClassExistButIsNotResolvableWithUnionTypes')]
public function testClassExistButIsNotResolvableWithUnionTypes(string $class): void
{
$container = new Container(ContainerConfig::create());
$container = new Container();

$this->assertFalse($container->has($class));
}

public function testWithoutDefinition(): void
{
$container = new Container(ContainerConfig::create());
$container = new Container();

$hasEngine = $container->has(EngineMarkOne::class);
$this->assertTrue($hasEngine);
Expand All @@ -213,7 +220,7 @@ public function testWithoutDefinition(): void

public function testCircularClassDependencyWithoutDefinition(): void
{
$container = new Container(ContainerConfig::create());
$container = new Container();
$this->expectException(CircularReferenceException::class);
$container->get(Chicken::class);
}
Expand Down Expand Up @@ -612,7 +619,7 @@ public function testFalsePositiveCircularReferenceWithClassID(): void
{
$this->expectNotToPerformAssertions();

$container = new Container(ContainerConfig::create());
$container = new Container();

// Build an object
$container->get(ColorPink::class);
Expand All @@ -639,7 +646,7 @@ public function testFalsePositiveCircularReferenceWithStringID(): void
{
$this->expectNotToPerformAssertions();

$container = new Container(ContainerConfig::create());
$container = new Container();
try {
// Build an object
$container->get('test');
Expand Down Expand Up @@ -948,7 +955,7 @@ public function testThrowingNotFoundException(): void
{
$this->expectException(NotFoundException::class);

$container = new Container(ContainerConfig::create());
$container = new Container();
$container->get('non_existing');
}

Expand Down Expand Up @@ -1233,16 +1240,16 @@ public function testNewContainerDefinitionInDelegates(): void

$config = ContainerConfig::create()
->withDefinitions([
ContainerInterface::class => new Container(ContainerConfig::create()),
ContainerInterface::class => new Container(),
])
->withDelegates([
function (ContainerInterface $container) use (&$firstContainer): ContainerInterface {
$firstContainer = $container;
return new Container(ContainerConfig::create());
return new Container();
},
function (ContainerInterface $container) use (&$secondContainer): ContainerInterface {
$secondContainer = $container;
return new Container(ContainerConfig::create());
return new Container();
},
]);
$originalContainer = new Container($config);
Expand Down Expand Up @@ -1619,7 +1626,7 @@ public function testDifferentContainerWithProviders(): void
public function getDefinitions(): array
{
return [
ContainerInterface::class => static fn (ContainerInterface $container) => new Container(ContainerConfig::create()),
ContainerInterface::class => static fn (ContainerInterface $container) => new Container(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function ensureProviderRegisterExtensions($provider): void

private function ensureProviderRegisterDefinitions($provider): void
{
$container = new Container(ContainerConfig::create());
$container = new Container();

$this->assertFalse(
$container->has(Car::class),
Expand Down

0 comments on commit 56168c3

Please sign in to comment.