From 3ed5e3570139028c029982829cfc7e185542df20 Mon Sep 17 00:00:00 2001 From: SilverFire - Dmitry Naumenko Date: Mon, 19 Feb 2018 00:10:49 +0200 Subject: [PATCH] Fixed PHPDocs, code and tests for #15422 --- framework/rbac/BaseManager.php | 10 +++++++--- framework/test/FileFixtureTrait.php | 4 ++-- tests/framework/rbac/ManagerTestCase.php | 34 +++++++++++++------------------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/framework/rbac/BaseManager.php b/framework/rbac/BaseManager.php index 3daa0d7..27e4af2 100644 --- a/framework/rbac/BaseManager.php +++ b/framework/rbac/BaseManager.php @@ -10,6 +10,7 @@ namespace yii\rbac; use yii\base\Component; use yii\base\InvalidArgumentException; use yii\base\InvalidConfigException; +use yii\base\InvalidValueException; /** * BaseManager is a base class implementing [[ManagerInterface]] for RBAC management. @@ -18,6 +19,7 @@ use yii\base\InvalidConfigException; * * @property Role[] $defaultRoleInstances Default roles. The array is indexed by the role names. This property * is read-only. + * @property string[] $defaultRoles The default roles. * * @author Qiang Xue * @since 2.0 @@ -198,16 +200,18 @@ abstract class BaseManager extends Component implements ManagerInterface /** * Set default roles * @param array|\Closure $roles either array of roles or a callable returning it + * @throws InvalidArgumentException when $roles is neither array nor Closure + * @throws InvalidValueException when Closure return is not an array * @since 2.0.14 */ public function setDefaultRoles($roles) { if (is_array($roles)) { $this->defaultRoles = $roles; - } elseif (is_callable($roles)) { - $roles = $roles(); + } elseif ($roles instanceof \Closure) { + $roles = call_user_func($roles); if (!is_array($roles)) { - throw new InvalidArgumentException('Default roles closure must return an array'); + throw new InvalidValueException('Default roles closure must return an array'); } $this->defaultRoles = $roles; } else { diff --git a/framework/test/FileFixtureTrait.php b/framework/test/FileFixtureTrait.php index 2313acc..6f88cf1 100644 --- a/framework/test/FileFixtureTrait.php +++ b/framework/test/FileFixtureTrait.php @@ -6,6 +6,7 @@ */ namespace yii\test; + use Yii; use yii\base\InvalidConfigException; @@ -19,7 +20,6 @@ trait FileFixtureTrait { /** * @var string the directory path or [path alias](guide:concept-aliases) that contains the fixture data - * @since 2.0.14 */ public $dataDirectory; /** @@ -44,7 +44,7 @@ trait FileFixtureTrait return []; } - if (basename($file) == $file && $this->dataDirectory !== null) { + if (basename($file) === $file && $this->dataDirectory !== null) { $file = $this->dataDirectory . '/' . $file; } diff --git a/tests/framework/rbac/ManagerTestCase.php b/tests/framework/rbac/ManagerTestCase.php index 6751672..1574eb1 100644 --- a/tests/framework/rbac/ManagerTestCase.php +++ b/tests/framework/rbac/ManagerTestCase.php @@ -8,6 +8,7 @@ namespace yiiunit\framework\rbac; use yii\base\InvalidParamException; +use yii\rbac\BaseManager; use yii\rbac\Item; use yii\rbac\Permission; use yii\rbac\Role; @@ -19,7 +20,7 @@ use yiiunit\TestCase; abstract class ManagerTestCase extends TestCase { /** - * @var \yii\rbac\ManagerInterface + * @var \yii\rbac\ManagerInterface|BaseManager */ protected $auth; @@ -615,26 +616,19 @@ abstract class ManagerTestCase extends TestCase $this->assertInstanceOf(ActionRule::className(), $rule); } - public function testDefaultRoles() + public function testDefaultRolesWithClosureReturningNonArrayValue() { - try { - $this->auth->defaultRoles = 'test'; - } catch (\Exception $e) { - $this->assertInstanceOf('\yii\base\InvalidParamException', $e); - $this->assertEquals('Default roles must be either an array or a callable', $e->getMessage()); - - try { - $this->auth->defaultRoles = function () { - return 'test'; - }; - } catch (\Exception $e) { - $this->assertInstanceOf('\yii\base\InvalidParamException', $e); - $this->assertEquals('Default roles closure must return an array', $e->getMessage()); - } - - return; - } + $this->expectException('yii\base\InvalidValueException'); + $this->expectExceptionMessage('Default roles closure must return an array'); + $this->auth->defaultRoles = function () { + return 'test'; + }; + } - $this->fail('Not rise an exception'); + public function testDefaultRolesWithNonArrayValue() + { + $this->expectException('yii\base\InvalidArgumentException'); + $this->expectExceptionMessage('Default roles must be either an array or a callable'); + $this->auth->defaultRoles = 'test'; } }