如何在Codeception单元测试用例中使用Yii2用户身份?

问题描述 投票:0回答:1

我是单元测试的新手。尝试使用 codeception 插件在我的 Yii2 项目中创建一些测试用例。从过去 1 周开始,我正在学习它。

现在我知道如何在单元测试中使用 Fixtures、Mocks 和 Stubs。

我只是想问一下,在某些情况下我们可以在单元测试中使用 Yii2 用户身份类吗?例如,有时我们必须检查登录用户的某些条件。在这种情况下如何在单元测试中使用用户会话或用户身份。

php yii yii2 phpunit codeception
1个回答
0
投票

单元测试的想法是测试代码的单个特定部分,例如函数或方法。如果您测试具有依赖项的函数或方法,例如获取用户 ID 或数据库记录,则单元测试的目的是模拟或存根方法的每个依赖项。

另一种方法是在测试中重建/重新创建引导应用程序。这意味着在您的情况下:调用正确的方法来创建用户对象,例如通过调用登录方法。

如果您想测试应用程序的整个部分,那么功能测试可能是适合您案例的测试类型。在这里,您从一个完整的引导应用程序开始,您可以首先登录用户(调用用户页面),然后执行您的测试用例。

但是:
如果您只需要在测试中进行任何登录,那么您还可以为测试定义一个虚拟用户类。这意味着:

  1. config/test.php
    中添加类似的内容以使用虚拟用户模型:

    'user' => [
                'identityClass' => 'app\tests\models\DummyUser',
                'loginUrl' => ['site/login'],
                'enableAutoLogin' => true,
                'enableSession' => true,
              ],
    
  2. app\tests\models\DummyUser
    中创建一个虚拟用户模型,实现
    IdentityIngterface
    所需的所有方法,如下所示:

    namespace app\tests\models;
    
    use yii\web\IdentityInterface;
    use app\models\User;
    
    
    class DummyUser extends User implements IdentityInterface
    {
       public $id = "Foo";
    
        public static function findIdentity($id)
        {   
            // always return the same dummy user
            return new static ([
                'id' => 'dummyUser',
                'email' => 'dummyUser@domain',
                'name' => 'Dummy User'
    
            ]);
        }
    
        public static function findGuestIdentity()
        {
             return new static ([
                'id' => 'guest',
                'email' => '',
                'name' => 'Guest User'
    
            ]);
        }
    
        public static function findGast()
        {
            return new static ([
                'id' => 'guest'
            ]);
        }
        /**
         * This method is needed to satisfy the interface.
         * {@inheritdoc}
         */
        public static function findIdentityByAccessToken($token, $type = null)
        {
            return true;
        }
    
        /**
         * This method is needed to satisfy the interface.
         * 
         * {@inheritdoc}
         */
        public function getId()
        {
            return true;
        }
    
        /**
         * This method is needed to satisfy the interface.
         * {@inheritdoc}
         */
        public function getAuthKey()
        {
            return "bar";
        }
    
        /**
         * This method is needed to satisfy the interface.
         * {@inheritdoc}
         */
        public function validateAuthKey($authKey)
        {
            return true;
        }
    }
    
    1. 在您的单元测试中使用
      Yii
      和您的虚拟用户模型
      use app\test\models\DummyUser
      并在测试类的块之前运行假登录:
    protected function _before()
    {
        $dummyLogin = DummyUser::findIdentity('dummyUser');
        Yii::$app->user->login($dummyLogin);
    }
    

现在,Yii2 用户身份可在 yii2 codeception 单元测试中使用。现在您在 Yii2 中拥有了用于单元测试的(假)登录名。

© www.soinside.com 2019 - 2024. All rights reserved.