Several improvements to DI container

- Refactored invoke into separate resolve method (reusability)
- Added support for associative params array
- Added PHPUnit group doc to DI tests.
- Improved the tests for better coverage, and fixed a bug discovered by the new tests.

close #10811
This commit is contained in:
Sam Mousa
2016-02-11 13:43:40 +00:00
committed by Carsten Brandt
parent 3d719d057e
commit 8ea4c660da
5 changed files with 128 additions and 37 deletions

View File

@ -20,6 +20,7 @@ use yii\validators\NumberValidator;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @group di
*/
class ContainerTest extends TestCase
{
@ -169,4 +170,47 @@ class ContainerTest extends TestCase
$this->assertEquals($response, Yii::$app->response);
}
public function testAssociativeInvoke()
{
$this->mockApplication([
'components' => [
'qux' => [
'class' => 'yiiunit\framework\di\stubs\Qux',
'a' => 'belongApp',
],
'qux2' => [
'class' => 'yiiunit\framework\di\stubs\Qux',
'a' => 'belongAppQux2',
],
]
]);
$closure = function($a, $x = 5, $b) {
return $a > $b;
};
$this->assertFalse(Yii::$container->invoke($closure, ['b' => 5, 'a' => 1]));
$this->assertTrue(Yii::$container->invoke($closure, ['b' => 1, 'a' => 5]));
}
public function testResolveCallableDependencies()
{
$this->mockApplication([
'components' => [
'qux' => [
'class' => 'yiiunit\framework\di\stubs\Qux',
'a' => 'belongApp',
],
'qux2' => [
'class' => 'yiiunit\framework\di\stubs\Qux',
'a' => 'belongAppQux2',
],
]
]);
$closure = function($a, $b) {
return $a > $b;
};
$this->assertEquals([1, 5], Yii::$container->resolveCallableDependencies($closure, ['b' => 5, 'a' => 1]));
$this->assertEquals([1, 5], Yii::$container->resolveCallableDependencies($closure, ['a' => 1, 'b' => 5]));
$this->assertEquals([1, 5], Yii::$container->resolveCallableDependencies($closure, [1, 5]));
}
}