建模型 含数据迁移文件
php artisan make:model Models/TopicKind --migration
创建修改模型迁移文件
php artisan make:migration alter_tablename_table --table=tablename
执行最新数据迁移
php artisan migrate
回滚一次(如果之前漏了写错
php artisan migrate:rollback
重新构建数据库表(别在线上按这个
php artisan migrate:refresh
数据类型相关
https://laravel.com/docs/7.x/migrations
创建单元测试
php artisan make:test Admin/TopicKindTest --unit
测试单个用例(单个文件执行
vendor/phpunit/phpunit/phpunit --filter testTopicKindSave.php
创建工厂,虚拟数据用
php artisan make:factory PostFactory
修改字段的时候需要有还原 ,就是up和down都要写,rollback的时候就是执行down的东东,等于将up的倒过来做一次
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//创表
Schema::create('topic_kinds', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('content',128);
$table->integer('type');
$table->integer('rank')->default(1);
$table->integer('status')->default(1);
$table->timestamps();
});
//加字段
Schema::table('topics', function (Blueprint $table) {
//
$table->integer('kind')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//删表
Schema::dropIfExists('topic_kinds');
//删字段
Schema::table('topics', function (Blueprint $table) {
//
$table->dropColumn('kind');
});
}
初始化每个测试的数据(统一
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
DB::beginTransaction();
factory(TopicKind::class)
->create(
[
'content'=>'test3',
'type'=>1,
'rank'=>1,
'status'=>1
]
);
factory(TopicKind::class)
->create(
[
'content'=>'test1',
'type'=>1,
'rank'=>3,
'status'=>1
]
);
factory(TopicKind::class)
->create(
[
'content'=>'test1',
'type'=>1,
'rank'=>3,
'status'=>0
]
);
factory(TopicKind::class)
->create(
[
'content'=>'test2',
'type'=>1,
'rank'=>2,
'status'=>1
]
);
}
//testcase 基类,不知道为毛他基类是错的
use Tests\TestCase;
如果执行说route class什么的找不到
boost/app.php
require_once __DIR__ . /helpers.php;
现有表生成
composer require --dev "kitloong/laravel-migrations-generator"
php artisan migrate:generate
单个表
php artisan migrate:generate topics
干净数据
use DatabaseTransactions;
https://learnku.com/laravel/t/2588/unit-testing-to-ensure-the-database-clean
factory,虚拟数据的时候用
$factory->define(Record::class, function (Faker $faker) {
return [
'live_id' => factory(\App\Models\Live::class),
'channel_id' => random_int(1, 100),
'title' => function(array $post) {
return \App\Models\Live::find($post['live_id'])->title;
},
'thumb' => random_int(1, 10000),
'url' => $faker->url,
'writer' => 'test',
'source_id' => 0,
];
});
里面的post就是创建的record这个对象,所以title是搜索了一下这个id的live对应的title;另外这里希望是live_id是一个Live的id,factory(\App\Models\Live::class) 这样写live_id就已经是id了(不要问我为什么,我也奇怪,但是实际就是这样
如果需要创建多个对象,且有关联创建
protected function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
//创建测试赛事
$match = factory(Match::class)->create(['name'=>self::testLeague.'赛事']);
//创建测试live和record数据
factory(Live::class, 10)
->create(['match_id' => $match->id, 'title'=>self::testTeam."啦啦啦啦VS神马球队啦啦啦"])
->each(function ($live) {
factory(Record::class)->create(['live_id'=>$live->id]);
});
factory(Live::class, 10)
->create(['match_id' => $match->id, 'title'=>"啦啦啦啦啦VS哈哈哈哈哈哈"])
->each(function ($live) {
factory(Record::class)->create(['live_id'=>$live->id]);
});
}
也可以写到factory
$factory->define(Match::class, function (Faker $faker) {
return [
'name'=>$faker->unique()->name,
'alias'=>$faker->unique()->word,//自动生成随机数据
'intro'=>$faker->text,
'display'=>$faker->randomElement([0,1]),
'type_id'=>$faker->randomElement([0,1]),
'writer'=>'test',
];
});
//match创建后,同时创建20条对应的,lives、records数据
//$factory->afterCreating(Match::class, function ($match, Faker $faker) {
//
//});
测试用例单独执行 group关键字
/**
*@group AdminHouse
* */
public function xxxxxx
vendor/phpunit/phpunit/phpunit --group AdminHouse