codeception (7) 危险的Yii2-codeception

前言

忽然想起之前用codecetion时让我胆战心惊的那刻,那天我在测试服务器上研究着codeception,忽然同事大叫,怎么用户表被清空了,我当时觉得跟自己没关,但想起我今天刚把codeception的config配置更改了,链接到了测试数据库上,并且运行过里面自带的登陆案例,但运行结果是失败的,我觉得很有可能是我捅的篓子,所以我就开始找代码,结果真的是我干的。

怎么找到的

用户表只剩一条数据,就是运行codeception时加的,我根据里面的数据找到了存放数据的文件

tests\codeception\common\unit\fixtures\data\models\user.php

    return [
        [
            'username' => 'bayer.hudson',
            'auth_key' => 'HP187Mvq7Mmm3CTU80dLkGmni_FUH_lR',
            //password_0
            'password_hash' => '$2y$13$EjaPFBnZOQsHdGuHI.xvhuDp1fHpo8hKRSk6yshqa9c5EG8s3C3lO',
            'password_reset_token' => 'ExzkCOaYc1L8IOBs4wdTGGbgNiG3Wz1I_1402312317',
            'created_at' => '1402312317',
            'updated_at' => '1402312317',
            'email' => 'nicole.paucek@schultz.info',
        ],
    ];

怎么解决的

注释掉这个方法中的数据

namespace tests\codeception\frontend\unit\models;
class SignupFormTest extends DbTestCase
{
public function fixtures()
    {
        return [
            /*'user' => [
                'class' => UserFixture::className(),
                'dataFile' => '@tests/codeception/frontend/unit/fixtures/data/models/user.php',
            ],*/
        ];
    }
}

重新运行了了一下单元测试SignupFormTest ,这次数据没有被删除掉

危险原因

我研究了一下被删除的原因,找到了框架底层的代码

namespace yii\test;
class ActiveFixture extends BaseActiveFixture
{
  public function load()
  {
    $this->resetTable();
    $this->data = [];
    $table = $this->getTableSchema();
    foreach ($this->getData() as $alias => $row) {
        $primaryKeys = $this->db->schema->insert($table->fullName, $row);
        $this->data[$alias] = array_merge($row, $primaryKeys);
    }
  }
  protected function resetTable()
  {
    $table = $this->getTableSchema();
    $this->db->createCommand()->delete($table->fullName)->execute();
    if ($table->sequenceName !== null) {
        $this->db->createCommand()->resetSequence($table->fullName, 1)->execute();
    }
  }
}

就是这个resetTable方法把数据给清空了,所以大家还是慎重使用Fixture,虽然是测试服务器,但是里面的数据也是非常重要的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值