diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 19ef2c38f24..ecf81e33f98 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/di/Instance.php b/framework/di/Instance.php index 4e335db7ecb..f51e0f24f8d 100644 --- a/framework/di/Instance.php +++ b/framework/di/Instance.php @@ -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; @@ -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."); diff --git a/tests/framework/di/InstanceTest.php b/tests/framework/di/InstanceTest.php index 70f7970a6f2..be058b46453 100644 --- a/tests/framework/di/InstanceTest.php +++ b/tests/framework/di/InstanceTest.php @@ -7,6 +7,7 @@ namespace yiiunit\framework\di; +use Yii; use yii\base\Component; use yii\db\Connection; use yii\di\Container; @@ -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(); } }