测试夹具(fixtures)简单使用 [ 1.0 版本 ]
最近在学习fixtures,所以边学边总结吧。(待修正)
欢迎大家讨论
使用场景: 在我们开发或者测试的时候,可能会需要某种特定状态下的数据,比如我想要用户刚下订单还未支付这个状态,那木我们就需要手动进行添加购物车、下订单等惭怍,但是fixtures可以直接将这个状态保存下来,如果需要使用这个状态,只要命令行里面加载一下对应的fixtures就好了。(待续)
用例框架:Yii2 advanced
项目根目录: /data/www/yii,下文中ROOT
在根根目录建立文件夹
ROOT/tests/unit/fixtures/data
ROOT/tests/unit/models首先要在ROOT/console/config/bootstrap.php中加入别名
Yii::setAlias('tests', dirname(dirname(DIR)).'/tests');在fixtures文件夹下建立PersonalUserAccountFixture.php(用户账户信息) \ PersonalUserFixture.php(用户信息)
PersonalUserFixture.php
<?php
/**
* Created by PhpStorm.
* User: yang
* Date: 2016/4/7
* Time: 11:21
*/
namespace tests\unit\fixtures;
use yii\test\ActiveFixture;
class PersonalUserFixture extends ActiveFixture
{
public $modelClass = 'common\models\user\User';
public function fixtures()
{
return [
PersonalUserFixture::className(),
];
}
public function load()
{
$this->resetTable();
$this->data = [];
$table = $this->getTableSchema();
$data = $this->getData();
$primaryKeys = $this->db->schema->insert($table->fullName, $data);
$this->data = array_merge($data, $primaryKeys);
}
public function getData()
{
if ($this->dataFile === null) {
$class = new \ReflectionClass($this);
$dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
if(is_file($dataFile)){
$data = require($dataFile);
if(isset($data['personal_user'])){
return $data['personal_user'];
}
}
return [];
} else {
return parent::getData();
}
}
}
PersonalUserAccountFixture.php
<?php
/**
* Created by PhpStorm.
* User: yang
* Date: 2016/4/7
* Time: 13:09
*/
namespace tests\unit\fixtures;
use yii\test\ActiveFixture;
class PersonalUserAccountFixture extends ActiveFixture
{
public $modelClass = 'common\models\user\UserAccount';
public function init()
{
parent::init();
$this->data = $this->getData();
}
public function fixtures()
{
return [
PersonalUserAccountFixture::className(),
];
}
public function load()
{
$this->resetTable();
$table = $this->getTableSchema();
$data = $this->data;
$primaryKeys = $this->db->schema->insert($table->fullName, $data);
$this->data = array_merge($data, $primaryKeys);
}
public function getData()
{
if ($this->dataFile === null) {
$class = new \ReflectionClass($this);
$dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
if(is_file($dataFile)){
$data = require($dataFile);
if(isset($data['personal_user_account'])){
return $data['personal_user_account'];
}
}
return [];
} else {
return parent::getData();
}
}
}
在data文件夹下建立 user.php(用户表user) 和 user_account.php(账户表user_account),并在里里边添加需要的数据
在 models目录下建立TestPersonalUser.php
<?php
/**
* Created by PhpStorm.
* User: yang
* Date: 2016/4/7
* Time: 13:23
*/
namespace tests\unit\models;
use yii\test\FixtureTrait;
class TestPersonalUser
{
use FixtureTrait;
private $ids = [];
public $personal_user_fixture = ['tests\unit\fixtures\PersonalUserFixture'];
public $personal_user_account_fixture = ['tests\unit\fixtures\PersonalUserAccountFixture'];
public function load(){
//加载PersonalUserFixture
$fixturesObjects = $this->createFixtures($this->personal_user_fixture);
foreach($fixturesObjects as $fixture){
$fixture->load();
$data = $fixture->data;
$this->ids[] = isset($data['id'])?$data['id']:null;
}
//加载PersonalUserAccountFixture
$fixturesObjects = $this->createFixtures($this->personal_user_account_fixture);
$k = 0;
foreach($fixturesObjects as $fixture){
$fixture->data = array_merge($fixture->data,['uid'=>$this->ids[$k]]);
$fixture->load();
$k++;
}
}
}
- 加载fixtures (new TestPersonalUser())->load() ,能够在user表中生成一条数据,并在user_account表中生成一条数据
specialnot
注册时间:2015-08-06
最后登录:2019-08-16
在线时长:27小时54分
最后登录:2019-08-16
在线时长:27小时54分
- 粉丝43
- 金钱1175
- 威望200
- 积分3445
共 0 条评论