From a837c59182f66432cfb2464ff39b1fcd52e3c91c Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 22 Nov 2016 17:49:55 +0100 Subject: [PATCH] improved error message for Instance::ensure() when component does not exist (#12967) fixes #7333 --- framework/CHANGELOG.md | 1 + framework/di/Instance.php | 6 +++++- tests/framework/di/InstanceTest.php | 24 ++++++++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 695da17b0e..f824e9c963 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -22,6 +22,7 @@ Yii Framework 2 Change Log - Bug #12939: Hard coded table names for MSSQL in RBAC migration (arogachev) - Bug #12974: Fixed incorrect order of migrations history in case `yii\console\controllers\MigrateController::$migrationNamespaces` is in use (evgen-d, klimov-paul) - Enh #6809: Added `\yii\caching\Cache::$defaultDuration` property, allowing to set custom default cache duration (sdkiller) +- Enh #7333: Improved error message for `yii\di\Instance::ensure()` when a component does not exist (cebe) - Enh #7420: Attributes for prompt generated with `renderSelectOptions` of `\yii\helpers\Html` helper (arogachev) - Enh #9162: Added support of closures in `value` for attributes in `yii\widgets\DetailView` (arogachev) - Enh #11037: `yii.js` and `yii.validation.js` use `Regexp.test()` instead of `String.match()` (arogachev, nkovacs) diff --git a/framework/di/Instance.php b/framework/di/Instance.php index 6fe4169b3e..a11491a150 100644 --- a/framework/di/Instance.php +++ b/framework/di/Instance.php @@ -128,7 +128,11 @@ class Instance } if ($reference instanceof self) { - $component = $reference->get($container); + try { + $component = $reference->get($container); + } catch(\ReflectionException $e) { + throw new InvalidConfigException('Failed to instantiate component or class "' . $reference->id . '".', 0, $e); + } if ($type === null || $component instanceof $type) { return $component; } else { diff --git a/tests/framework/di/InstanceTest.php b/tests/framework/di/InstanceTest.php index 564d22ead5..2ac448d447 100644 --- a/tests/framework/di/InstanceTest.php +++ b/tests/framework/di/InstanceTest.php @@ -46,7 +46,27 @@ class InstanceTest extends TestCase $this->assertTrue(Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'], 'yii\db\Connection', $container) instanceof Connection); } - public function testEnsureWithoutType() + /** + * ensure an InvalidConfigException is thrown when a component does not exist. + */ + public function testEnsure_NonExistingComponentException() + { + $container = new Container; + $this->setExpectedExceptionRegExp('yii\base\InvalidConfigException', '/^Failed to instantiate component or class/i'); + Instance::ensure('cache', 'yii\cache\Cache', $container); + } + + /** + * ensure an InvalidConfigException is thrown when a class does not exist. + */ + public function testEnsure_NonExistingClassException() + { + $container = new Container; + $this->setExpectedExceptionRegExp('yii\base\InvalidConfigException', '/^Failed to instantiate component or class/i'); + Instance::ensure('yii\cache\DoesNotExist', 'yii\cache\Cache', $container); + } + + public function testEnsure_WithoutType() { $container = new Container; $container->set('db', [ @@ -59,7 +79,7 @@ class InstanceTest extends TestCase $this->assertTrue(Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'], null, $container) instanceof Connection); } - public function testEnsureMinimalSettings() + public function testEnsure_MinimalSettings() { Yii::$container->set('db', [ 'class' => 'yii\db\Connection',