/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Container/AfterResolvingAttributeCallbackTest.php
152 строки
4 KB
Lucas Michot
[13.x] Enable additional Rector rules (#60854)
22 июл 2026, 17:01
Не верифицирован
22 июл 2026, 17:01
b623055
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Container; use Attribute; use Illuminate\Container\Container; use PHPUnit\Framework\TestCase; class AfterResolvingAttributeCallbackTest extends TestCase { public function testCallbackIsCalledAfterDependencyResolutionWithAttribute() { $container = new Container(); $container->afterResolvingAttribute(ContainerTestOnTenant::class, function (ContainerTestOnTenant $attribute, HasTenantImpl $hasTenantImpl, Container $container) { $hasTenantImpl->onTenant($attribute->tenant); }); $hasTenantA = $container->make(ContainerTestHasTenantImplPropertyWithTenantA::class); $this->assertInstanceOf(HasTenantImpl::class, $hasTenantA->property); $this->assertEquals(Tenant::TenantA, $hasTenantA->property->tenant); $hasTenantB = $container->make(ContainerTestHasTenantImplPropertyWithTenantB::class); $this->assertInstanceOf(HasTenantImpl::class, $hasTenantB->property); $this->assertEquals(Tenant::TenantB, $hasTenantB->property->tenant); } public function testCallbackIsCalledAfterClassWithAttributeIsResolved() { $container = new Container(); $container->afterResolvingAttribute( ContainerTestBootable::class, fn ($_, $instance, Container $container) => method_exists($instance, 'booting') && $container->call([$instance, 'booting']) ); $instance = $container->make(ContainerTestHasBootable::class); $this->assertInstanceOf(ContainerTestHasBootable::class, $instance); $this->assertTrue($instance->hasBooted); } public function testCallbackIsCalledAfterClassWithConstructorAndAttributeIsResolved() { $container = new Container(); $container->afterResolvingAttribute(ContainerTestConfiguresClass::class, function (ContainerTestConfiguresClass $attribute, $class) { $class->value = $attribute->value; }); $container->when(ContainerTestHasSelfConfiguringAttributeAndConstructor::class) ->needs('$value') ->give('no-the-right-value'); $instance = $container->make(ContainerTestHasSelfConfiguringAttributeAndConstructor::class); $this->assertInstanceOf(ContainerTestHasSelfConfiguringAttributeAndConstructor::class, $instance); $this->assertSame('the-right-value', $instance->value); } public function testCallbackIsCalledOnAppCall() { $container = new Container(); $container->afterResolvingAttribute(ContainerTestOnTenant::class, function (ContainerTestOnTenant $attribute, HasTenantImpl $hasTenantImpl, Container $container) { $hasTenantImpl->onTenant($attribute->tenant); }); $tenant = $container->call(function (#[ContainerTestOnTenant(Tenant::TenantA)] HasTenantImpl $property) { return $property->tenant; }); $this->assertEquals(Tenant::TenantA, $tenant); } } #[Attribute(Attribute::TARGET_PARAMETER)] final readonly class ContainerTestOnTenant { public function __construct( public Tenant $tenant ) { } } enum Tenant { case TenantA; case TenantB; } final class HasTenantImpl { public ?Tenant $tenant = null; public function onTenant(Tenant $tenant): void { $this->tenant = $tenant; } } final readonly class ContainerTestHasTenantImplPropertyWithTenantA { public function __construct( #[ContainerTestOnTenant(Tenant::TenantA)] public HasTenantImpl $property ) { } } final readonly class ContainerTestHasTenantImplPropertyWithTenantB { public function __construct( #[ContainerTestOnTenant(Tenant::TenantB)] public HasTenantImpl $property ) { } } #[Attribute(Attribute::TARGET_CLASS)] final readonly class ContainerTestConfiguresClass { public function __construct( public string $value ) { } } #[ContainerTestConfiguresClass(value: 'the-right-value')] final class ContainerTestHasSelfConfiguringAttributeAndConstructor { public function __construct( public string $value ) { } } #[Attribute(Attribute::TARGET_CLASS)] final class ContainerTestBootable { } #[ContainerTestBootable] final class ContainerTestHasBootable { public bool $hasBooted = false; public function booting(): void { $this->hasBooted = true; } }