mirror of
https://github.com/pestphp/pest.git
synced 2026-03-11 02:07:23 +01:00
fix(container): resolves dependencies without contructor
Co-Authored-By: Johannes Pichler <fetzi@users.noreply.github.com>
This commit is contained in:
@ -92,6 +92,8 @@ final class Container
|
|||||||
|
|
||||||
return $reflectionClass->newInstanceArgs($params);
|
return $reflectionClass->newInstanceArgs($params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $reflectionClass->newInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
throw ShouldNotHappen::fromMessage(sprintf('A dependency with the name `%s` cannot be resolved.', $id));
|
throw ShouldNotHappen::fromMessage(sprintf('A dependency with the name `%s` cannot be resolved.', $id));
|
||||||
|
|||||||
@ -18,6 +18,10 @@ it('gets an instance', function () {
|
|||||||
assertSame($this->container, $this->container->get(Container::class));
|
assertSame($this->container, $this->container->get(Container::class));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('autowire', function () {
|
||||||
|
assertInstanceOf(Container::class, $this->container->get(Container::class));
|
||||||
|
});
|
||||||
|
|
||||||
it('creates an instance and resolves parameters', function () {
|
it('creates an instance and resolves parameters', function () {
|
||||||
$this->container->add(Container::class, $this->container);
|
$this->container->add(Container::class, $this->container);
|
||||||
$instance = $this->container->get(ClassWithDependency::class);
|
$instance = $this->container->get(ClassWithDependency::class);
|
||||||
@ -39,15 +43,9 @@ it('can resolve builtin value types', function () {
|
|||||||
assertInstanceOf(TestSuite::class, $instance);
|
assertInstanceOf(TestSuite::class, $instance);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('cannot resolve a parameter that requires additional dependencies', function () {
|
|
||||||
$this->expectException(ShouldNotHappen::class);
|
|
||||||
$this->container->get(ClassWithDependency::class);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('cannot resolve a parameter without type', function () {
|
it('cannot resolve a parameter without type', function () {
|
||||||
$this->expectException(ShouldNotHappen::class);
|
|
||||||
$this->container->get(ClassWithoutTypeParameter::class);
|
$this->container->get(ClassWithoutTypeParameter::class);
|
||||||
});
|
})->throws(ShouldNotHappen::class);
|
||||||
|
|
||||||
class ClassWithDependency
|
class ClassWithDependency
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user