container = $this->createMock(Container::class); $this->environment = $this->createMock(Environment::class); $this->resolver = new EnvAttributeResolver($this->container); $this->container->method('get') ->with(Environment::class) ->willReturn($this->environment); }); describe('EnvAttributeResolver', function () { it('supports Env attribute', function () { $reflectionParam = new \ReflectionParameter( [ServiceWithEnvString::class, '__construct'], 0 ); $attributes = $reflectionParam->getAttributes(EnvAttribute::class); expect($attributes)->not->toBeEmpty(); expect($this->resolver->supports($attributes[0]))->toBeTrue(); }); it('resolves string type from environment', function () { $this->environment->expects($this->once()) ->method('getString') ->with(EnvKey::APP_NAME, '') ->willReturn('My App'); $className = ClassName::create(ServiceWithEnvString::class); $reflectionParam = new \ReflectionParameter( [ServiceWithEnvString::class, '__construct'], 0 ); $result = $this->resolver->resolve($reflectionParam, $className, '__construct'); expect($result)->toBe('My App'); }); it('resolves int type from environment', function () { $this->environment->expects($this->once()) ->method('getInt') ->with(EnvKey::DB_PORT, 0) ->willReturn(5432); $className = ClassName::create(ServiceWithEnvInt::class); $reflectionParam = new \ReflectionParameter( [ServiceWithEnvInt::class, '__construct'], 0 ); $result = $this->resolver->resolve($reflectionParam, $className, '__construct'); expect($result)->toBe(5432); expect($result)->toBeInt(); }); it('resolves bool type from environment', function () { $this->environment->expects($this->once()) ->method('getBool') ->with(EnvKey::APP_DEBUG, false) ->willReturn(true); $className = ClassName::create(ServiceWithEnvBool::class); $reflectionParam = new \ReflectionParameter( [ServiceWithEnvBool::class, '__construct'], 0 ); $result = $this->resolver->resolve($reflectionParam, $className, '__construct'); expect($result)->toBeTrue(); expect($result)->toBeBool(); }); it('resolves float type from environment', function () { $this->environment->expects($this->once()) ->method('getFloat') ->with(EnvKey::APP_NAME, 0.0) ->willReturn(123.45); $className = ClassName::create(ServiceWithEnvFloat::class); $reflectionParam = new \ReflectionParameter( [ServiceWithEnvFloat::class, '__construct'], 0 ); $result = $this->resolver->resolve($reflectionParam, $className, '__construct'); expect($result)->toBe(123.45); expect($result)->toBeFloat(); }); it('uses generic get for unknown types', function () { $this->environment->expects($this->once()) ->method('get') ->with(EnvKey::APP_NAME, null) ->willReturn('some value'); $className = ClassName::create(ServiceWithEnvString::class); $reflectionParam = new \ReflectionParameter( [ServiceWithEnvString::class, '__construct'], 0 ); // Temporär den Typ ändern, um den default-Fall zu testen // (In PHP können wir Reflection nicht direkt ändern, daher testen wir den Fall separat) // Für diesen Test müssen wir einen anderen Ansatz verwenden }); });