Question: Can Container Change an Alias?
jagarsoft opened this issue · comments
jagarsoft commented
Should this test pass?
public function testContainerCanChangeAlias(): void
{
$container = new Container();
$container->add('MyClass', Foo::class);
self::assertTrue($container->has('MyClass'));
$foo = $container->get('MyClass');
self::assertInstanceOf(Foo::class, $foo);
$container->add('MyClass', Bar::class);
self::assertTrue($container->has('MyClass'));
$bar = $container->get('MyClass');
self::assertInstanceOf(Bar::class, $bar);
}
$ vendor/bin/phpunit --verbose --no-coverage --filter testContainerCanChangeAlias
PHPUnit 8.5.26 #StandWithUkraine
Runtime: PHP 7.4.27 with Xdebug 3.1.2
Configuration: C:\Users\Usuario\GitHub\jagarsoft\container\phpunit.xml
F 1 / 1 (100%)
Time: 4.82 seconds, Memory: 6.00 MB
There was 1 failure:
1) League\Container\Test\ContainerTest::testContainerCanChangeAlias
Failed asserting that League\Container\Test\Asset\Foo Object (...) is an instance of class "League\Container\Test\Asset\Bar".
C:\Users\Usuario\GitHub\jagarsoft\container\tests\ContainerTest.php:38
FAILURES!
Tests: 1, Assertions: 4, Failures: 1.
How would be the correct way of change an alias?
Or even remove an alias? (something like: $container->remove('MyClass');)
Thanks in advanced!!
PS: And also thanks for this Extraordinary Package 😏
jagarsoft commented
Ok. This is the right way of changing an alias:
$container->extend('MyClass')->setConcrete(Bar::class);
Sorry for the noise 😏