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

Make ContainerConfig optional #385

Merged
merged 6 commits into from
Jan 12, 2025
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
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 @@
*
* @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 Expand Up @@ -282,7 +284,7 @@

$this->delegates->attach($delegate);
}
$this->definitions->setDelegateContainer($this->delegates);

Check warning on line 287 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ } $this->delegates->attach($delegate); } - $this->definitions->setDelegateContainer($this->delegates); + } /** * @param mixed $definition Definition to validate.
}

/**
Expand All @@ -306,7 +308,7 @@

$definition = array_merge(
$class === null ? [] : [ArrayDefinition::CLASS_NAME => $class],
[ArrayDefinition::CONSTRUCTOR => $constructorArguments],

Check warning on line 311 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ $methodsAndProperties = $definition['methodsAndProperties']; $definition = array_merge( $class === null ? [] : [ArrayDefinition::CLASS_NAME => $class], - [ArrayDefinition::CONSTRUCTOR => $constructorArguments], + [], // extract only value from parsed definition method array_map(static fn(array $data): mixed => $data[2], $methodsAndProperties) );
// extract only value from parsed definition method
array_map(static fn (array $data): mixed => $data[2], $methodsAndProperties),
);
Expand Down Expand Up @@ -490,7 +492,7 @@
);
}

$this->building[$id] = 1;

Check warning on line 495 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ } throw new CircularReferenceException(sprintf('Circular reference to "%s" detected while building: %s.', $id, implode(', ', array_keys($this->building)))); } - $this->building[$id] = 1; + $this->building[$id] = 2; try { if (!$this->definitions->has($id)) { throw new NotFoundException($id, $this->definitions->getBuildStack());
try {
if (!$this->definitions->has($id)) {
throw new NotFoundException($id, $this->definitions->getBuildStack());
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
Loading