Skip to content

Commit faa75b1

Browse files
committed
activator test cases
1 parent 58e9258 commit faa75b1

2 files changed

Lines changed: 59 additions & 19 deletions

File tree

src/Activator.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use ReflectionMethod;
1010
use ReflectionFunctionAbstract;
1111
use ReflectionParameter;
12-
use Throwable;
1312
use ReflectionClass;
1413
use Featherbits\ServiceContainer\Contract\Activator as ActivatorContract;
1514

@@ -26,28 +25,20 @@ public function __construct(ContainerContract $container)
2625

2726
public function call(callable $callable)
2827
{
29-
try {
30-
$reflection = self::getCallableReflectionFunctionAbstract($callable);
31-
return $this->track(self::getFunctionIdentifier($reflection), function () use ($reflection, $callable) {
32-
return call_user_func_array($callable, $this->getArguments($reflection));
33-
});
34-
} catch (Throwable $e) {
35-
throw new ContainerException('Failed to call function', 0, $e);
36-
}
28+
$reflection = self::getCallableReflectionFunctionAbstract($callable);
29+
return $this->track(self::getFunctionIdentifier($reflection), function () use ($reflection, $callable) {
30+
return call_user_func_array($callable, $this->getArguments($reflection));
31+
});
3732
}
3833

3934
public function instantiate(string $type): object
4035
{
41-
try {
42-
return $this->track($type, function () use ($type) {
43-
$reflection = new ReflectionClass($type);
44-
return ($constructor = $reflection->getConstructor())
45-
? $reflection->newInstanceArgs($this->getArguments($constructor))
46-
: $reflection->newInstance();
47-
});
48-
} catch (Throwable $e) {
49-
throw new ContainerException('Failed to instantiate ' . $type, 0, $e);
50-
}
36+
return $this->track($type, function () use ($type) {
37+
$reflection = new ReflectionClass($type);
38+
return ($constructor = $reflection->getConstructor())
39+
? $reflection->newInstanceArgs($this->getArguments($constructor))
40+
: $reflection->newInstance();
41+
});
5142
}
5243

5344
private function track(string $identifier, callable $callable)

tests/ActivatorTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use Featherbits\ServiceContainer\Container;
5+
use Featherbits\ServiceContainer\InstanceGetterStrategies\ByFactory;
6+
use Featherbits\ServiceContainer\Activator;
7+
8+
class ClassWithTestInterfaceParameter
9+
{
10+
public TestInterface $instance;
11+
12+
public function __construct(TestInterface $instance)
13+
{
14+
$this->instance = $instance;
15+
}
16+
}
17+
18+
final class ActivatorTest extends TestCase
19+
{
20+
public function testCallCallableWithResolvedParameterFromContainer()
21+
{
22+
$instance = new TestClass;
23+
$container = new Container();
24+
$container->set(TestInterface::class, new ByFactory(function () use ($instance) {
25+
return $instance;
26+
}));
27+
28+
$activator = new Activator($container);
29+
$instance2 = $activator->call(function (TestInterface $instance) {
30+
return $instance;
31+
});
32+
33+
$this->assertSame($instance, $instance2);
34+
}
35+
36+
public function testInstantiateClassWithResolvedParameterFromContainer()
37+
{
38+
$instance = new TestClass;
39+
$container = new Container();
40+
$container->set(TestInterface::class, new ByFactory(function () use ($instance) {
41+
return $instance;
42+
}));
43+
44+
$activator = new Activator($container);
45+
$instance2 = $activator->instantiate(ClassWithTestInterfaceParameter::class)->instance;
46+
47+
$this->assertSame($instance, $instance2);
48+
}
49+
}

0 commit comments

Comments
 (0)