Skip to content

Commit

Permalink
Fixed \yii\di\Instance::ensure() to work with minimum settings
Browse files Browse the repository at this point in the history
  • Loading branch information
lav45 authored and SilverFire committed Jan 1, 2016
1 parent 0611ead commit fcf25e8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Yii Framework 2 Change Log
- Bug #10363: Fixed fallback float and integer formatting (AnatolyRugalev, silverfire)
- Bug #10372: Fixed console controller including DI arguments in help (sammousa)
- Bug #10385: Fixed `yii\validators\CaptchaValidator` passed incorrect hashKey to JS validator when `captchaAction` begins with `/` (silverfire)
- Bug #10467: Fixed `yii\di\Instance::ensure()`to work with minimum settings (LAV45)
- Bug: Fixed generation of canonical URLs for `ViewAction` pages (samdark)
- Bug: Fixed `mb_*` functions calls to use `UTF-8` or `Yii::$app->charset` (silverfire)
- Enh #3506: Added `yii\validators\IpValidator` to perform validation of IP addresses and subnets (SilverFire, samdark)
Expand Down
8 changes: 4 additions & 4 deletions framework/di/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ public static function of($id)
*/
public static function ensure($reference, $type = null, $container = null)
{
if ($reference instanceof $type) {
return $reference;
} elseif (is_array($reference)) {
if (is_array($reference)) {
$class = isset($reference['class']) ? $reference['class'] : $type;
if (!$container instanceof Container) {
$container = Yii::$container;
Expand All @@ -122,11 +120,13 @@ public static function ensure($reference, $type = null, $container = null)

if (is_string($reference)) {
$reference = new static($reference);
} elseif ($type === null || $reference instanceof $type) {
return $reference;
}

if ($reference instanceof self) {
$component = $reference->get($container);
if ($component instanceof $type || $type === null) {
if ($type === null || $component instanceof $type) {
return $component;
} else {
throw new InvalidConfigException('"' . $reference->id . '" refers to a ' . get_class($component) . " component. $type is expected.");
Expand Down
73 changes: 71 additions & 2 deletions tests/framework/di/InstanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace yiiunit\framework\di;

use Yii;
use yii\base\Component;
use yii\db\Connection;
use yii\di\Container;
Expand Down Expand Up @@ -41,9 +42,77 @@ public function testEnsure()

$this->assertTrue(Instance::ensure('db', 'yii\db\Connection', $container) instanceof Connection);
$this->assertTrue(Instance::ensure(new Connection, 'yii\db\Connection', $container) instanceof Connection);
$this->assertTrue(Instance::ensure([
$this->assertTrue(Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'], 'yii\db\Connection', $container) instanceof Connection);
}

public function testEnsureWithoutType()
{
$container = new Container;
$container->set('db', [
'class' => 'yii\db\Connection',
'dsn' => 'test',
]);

$this->assertTrue(Instance::ensure('db', null, $container) instanceof Connection);
$this->assertTrue(Instance::ensure(new Connection, null, $container) instanceof Connection);
$this->assertTrue(Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'], null, $container) instanceof Connection);
}

public function testEnsureMinimalSettings()
{
Yii::$container->set('db', [
'class' => 'yii\db\Connection',
'dsn' => 'test',
]);

$this->assertTrue(Instance::ensure('db') instanceof Connection);
$this->assertTrue(Instance::ensure(new Connection) instanceof Connection);
$this->assertTrue(Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test']) instanceof Connection);

Yii::$container = new Container;
}

public function testExceptionRefersTo()
{
$container = new Container;
$container->set('db', [
'class' => 'yii\db\Connection',
'dsn' => 'test',
], 'yii\db\Connection', $container) instanceof Connection);
]);

$this->setExpectedException('yii\base\InvalidConfigException', '"db" refers to a yii\db\Connection component. yii\base\Widget is expected.');

Instance::ensure('db', 'yii\base\Widget', $container);
Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'], 'yii\base\Widget', $container);
}

public function testExceptionInvalidDataType()
{
$this->setExpectedException('yii\base\InvalidConfigException', 'Invalid data type: yii\db\Connection. yii\base\Widget is expected.');
Instance::ensure(new Connection, 'yii\base\Widget');
}

public function testExceptionComponentIsNotSpecified()
{
$this->setExpectedException('yii\base\InvalidConfigException', 'The required component is not specified.');
Instance::ensure('');
}

public function testGet()
{
$this->mockApplication([
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'test',
]
]
]);

$container = Instance::of('db');

$this->assertTrue($container->get() instanceof Connection);

$this->destroyApplication();
}
}

0 comments on commit fcf25e8

Please sign in to comment.