木子设计

LZG个人博客

本人专注于web领域开发,博客主要记录学习php/html/css/javascript,框架Yaf/Laravel/TP/CI,Linux及一些常用工具过程中遇到的问题及解决心得,希望对技术推广及他人学习提供薄力。

Laravel 测试,跟tinker结合真尼玛好用

发布时间:2016-08-28 15:27:24

laravel 以dev方式安装,或者更新一下,否则测试的包没有,会报错!
1、安装phpunit
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
2、安装"fzaninotto/faker": "~1.0"包,composer update
3、编写数据工厂生成数据
 $factory->define(App\MsaData::class, function (Faker\Generator $faker) {
  return [
  'fev' => rand(100,800)/100,
  'pef' => rand(30,1000),
  'date' => mkdate()
  ];
});

function mkdate()
{
$start=strtotime("2016-06-05");
$end=strtotime("2016-08-25");
$rand=rand($start,$end);
return date("YmdHis",$rand);
}

4、放到测试类中执行json请求
 $user = factory(App\MsaData::class,20)->make();
$user = array_merge($user,['sbid'=>'759DFAC2-5071-D2C3-86F5-262CF6E55262']);
 $this->post('/msa/upload?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0YWJsZSI6InBhdGllbnQiLCJzdWIiOjQ1LCJpc3MiOiJodHRwOlwvXC93d3cuaW4tbHVuZy5jb21cL3BhdGllbnRcL2xvZ2luIiwiaWF0IjoxNDcyMTE3OTExLCJleHAiOjE0NzI3MjI3MTEsIm5iZiI6MTQ3MjExNzkxMSwianRpIjoiZTAwNDUyMjQxNTZlZDhjNzk1MWYxMzliNmRiMmNiNWUifQ.pH61j8BMqkrq4DQT-VNZOag6I-BO-xJt1iHGhFpj8OM', $user)
  ->seeJson([
  'error_code' => 1,
  ]);