Add basic container implementation

This commit is contained in:
johannes.pichler
2020-06-03 22:08:25 +02:00
parent e8d426f574
commit b20f208b55
4 changed files with 170 additions and 0 deletions

View File

@ -118,6 +118,15 @@
PASS Tests\Unit\Support\Backtrace
✓ it gets file name from called file
PASS Tests\Unit\Support\Container
✓ it exists
✓ it gets an instance
✓ it creates an instance and resolves parameters
✓ it creates an instance and resolves also sub parameters
✓ it can resolve builtin value types
✓ it cannot resolve a parameter that requires additional dependencies
✓ it cannot resolve a parameter without type
PASS Tests\Unit\Support\Reflection
✓ it gets file name from closure
✓ it gets property values
@ -134,5 +143,10 @@
WARN Tests\Visual\Success
s visual snapshot of test suite on success
<<<<<<< HEAD
Tests: 6 skipped, 71 passed
Time: 2.89s
=======
Tests: 6 skipped, 76 passed
Time: 3.32s
>>>>>>> 84c9078... Add basic container implementation

View File

@ -0,0 +1,71 @@
<?php
use Pest\Exceptions\ShouldNotHappen;
use Pest\Support\Container;
use Pest\TestSuite;
uses()->group('container');
beforeEach(function () {
$this->container = new Container();
});
it('exists')
->assertTrue(class_exists(Container::class));
it('gets an instance', function () {
$this->container->add(Container::class, $this->container);
assertSame($this->container, $this->container->get(Container::class));
});
it('creates an instance and resolves parameters', function () {
$this->container->add(Container::class, $this->container);
$instance = $this->container->get(ClassWithDependency::class);
assertInstanceOf(ClassWithDependency::class, $instance);
});
it('creates an instance and resolves also sub parameters', function () {
$this->container->add(Container::class, $this->container);
$instance = $this->container->get(ClassWithSubDependency::class);
assertInstanceOf(ClassWithSubDependency::class, $instance);
});
it('can resolve builtin value types', function () {
$this->container->add('rootPath', getcwd());
$instance = $this->container->get(TestSuite::class);
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 () {
$this->expectException(ShouldNotHappen::class);
$this->container->get(ClassWithoutTypeParameter::class);
});
class ClassWithDependency
{
public function __construct(Container $container)
{
}
}
class ClassWithSubDependency
{
public function __construct(ClassWithDependency $param)
{
}
}
class ClassWithoutTypeParameter
{
public function __construct($param)
{
}
}