composer global require "overtrue/package-builder" --prefer-source
package-builder build [目标目录名]
package-builder build lunit-laravel
'git' is not recognized as an internal or external command,
operable program or batch file.
Name of package (example: foo/bar): lixiyong/lunit-laravel
Namespace of package [Lixiyong\LunitLaravel]:
Description of package: A simplified version of the laravel framework unit test
Author name of package [lixiyong]:
Author email of [email protected]
License of package [MIT]:
Do you want to test this package ? [Y/n]:
Do you want to use php-cs-fixer format your code ? [Y/n]:
Standard name of php-cs-fixer [symfony]:
Package lixiyong/lunit-laravel created in: ./lunit-laravel
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/11/10 0010
* Time: 7:47
*/
namespace Lixiyong\LunitLaravel\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
/**
* use Illuminate\Routing\Controller 这是laravel的基类控制器
* index() 访问测试的页面
* store() 是表示用来执行需要测试的方法
*/
class LunitController extends Controller
{
public function index()
{
return view('lunit::index');
}
public function store(Request $request)
{
$namespace = $request->input('namespace');
$className = $request->input('className');
$action = $request->input('action');
$param = $request->input('param');
$class = ($className == "") ? $namespace : $namespace.'\\'.$className;
//要替换的值 需要的结果
$class = str_replace("/","\\",$class);
$object = new $class();
$param = ($param=="") ? [] : explode('|',$param);
$data = call_user_func_array([$object,$action],$param);
return (is_array($data)) ? json_encode($data) : dd($data);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>不区分大小写(可以自行完善)</h1>
<br>
<form class="" action="{{route('lunit.store')}}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
命名空间:<input type="text" value='' style="width:300px" name='namespace' placeholder="如:app\index\controller 或app\index\controller\Index">可以写全,然后下面类名不用些 <br>
类名:<input type="text" name='className' placeholder="如:index ">命名空间全可以不用写<br>
测试方法名:<input type="text" name='action' placeholder="index"><br>
传递参数以 | 分割:<input type="text" placeholder="如: 1|2|3" name='param'><br>
<input type="submit" name="" value="测试"/>
</form>
</body>
</html>
对应项目目录,laravel58是一个laravel5.8的项目,自己去GitHub下载即可,laravelnew暂时没作用
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/11/10 0010
* Time: 8:06
*/
Route::get('/','LunitController@index');
Route::post('/','LunitController@store')->name('lunit.store');
// 测试路由
Route::get('test', 'TestController@index');
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/11/10 0010
* Time: 8:08
*/
namespace Lixiyong\LunitLaravel\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
class LunitServiceProvider extends ServiceProvider
{
public function boot()
{
$this->registerRoutes();
$this->loadViewsFrom(
__DIR__.'/../../resources/views','lunit'
);
}
private function routeConfiguration()
{
return [
'namespace'=>'Lixiyong\LunitLaravel\Http\Controllers',
'prefix'=>'lunit',
'middleware' => 'web',
];
}
private function registerRoutes()
{
Route::group($this->routeConfiguration(),function (){
$this->loadRoutesFrom(__DIR__.'/../Http/routes.php');
});
}
}
这一步有两个方法,推荐第1种:
方法1:修改lunit-laravel文件夹中的composer.json,添加一项extra,具体如下:
{
"name": "lixiyong\/lunit-laravel",
"description": "A simplified version of the laravel framework unit test",
"license": "MIT",
"authors": [
{
"name": "lixiyong",
"email": "[email protected]"
}
],
"require": {},
"autoload": {
"psr-4": {
"Lixiyong\\LunitLaravel\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Lixiyong\\LunitLaravel\\Providers\\LunitServiceProvider"
]
}
}
}
方法2:在laravel58中的config/app.php中添加对于这个服务提供者的加载
composer config repositories.lixiyong path ../lunit-laravel
composer require lixiyong/lunit-laravel:dev-master