Skip to content

Commit

Permalink
Remove restriction on passing additional parameters as positional arg…
Browse files Browse the repository at this point in the history
…uments.
  • Loading branch information
serge-kvashnin committed Jun 1, 2024
1 parent b1a6fc3 commit af978ed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
4 changes: 0 additions & 4 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ public function __invoke(Closure|callable|array|string $callable, mixed ...$argu
throw new ContainerException('Autowiring must be enabled for invoking callables using container.');
}

if (array_is_list($arguments)) {
throw new ContainerException('Extra parameters should be passed as named parameters.');
}

$closure = $this->closure($callable);
$parameters = $this->parameters(
$arguments,
Expand Down
24 changes: 17 additions & 7 deletions tests/Integration/InvokeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
use Tests\Norvica\Container\Fixtures\Invoke\Fixture289a24a0;
use Tests\Norvica\Container\Fixtures\Invoke\Fixture3b51a559;
use Tests\Norvica\Container\Fixtures\Invoke\Fixture9c429bd8;
use function Norvica\Container\ref;

final class InvokeTest extends BaseTestCase
{
public static function containers(): Generator
{
yield 'cold' => [self::container([])];
yield 'compiled' => [self::compiled([])];
yield 'cold container' => [self::container([])];
yield 'compiled container' => [self::compiled([])];
}

#[DataProvider('containers')]
public function testInvoke(InvokerInterface $container): void
public function testInvokeWithNamedArguments(InvokerInterface $container): void
{
[$a, $b, $c] = $container(Fixture9c429bd8::class, c: 'foo');
$this->assertInstanceOf(Fixture289a24a0::class, $a);
Expand All @@ -31,16 +32,25 @@ public function testInvoke(InvokerInterface $container): void
}

#[DataProvider('containers')]
public function testPositionalArguments(InvokerInterface $container): void
public function testInvokeWithPositionalArguments(InvokerInterface $container): void
{
[$a, $b, $c] = $container(Fixture9c429bd8::class, ref(Fixture3b51a559::class), 'foo');
$this->assertInstanceOf(Fixture289a24a0::class, $a);
$this->assertInstanceOf(Fixture3b51a559::class, $b);
$this->assertEquals('foo', $c);
}

public function testAutowiringDisabledCold(): void
{
$container = self::container([], false);
$this->expectException(ContainerException::class);

$container(Fixture9c429bd8::class, 'foo');
$container(static fn() => null);
}

#[DataProvider('containers')]
public function testAutowiringDisabled(InvokerInterface $container): void
public function testAutowiringDisabledCompiled(): void
{
$container = self::compiled([], false);
$this->expectException(ContainerException::class);

$container(static fn() => null);
Expand Down

0 comments on commit af978ed

Please sign in to comment.