开发laravel-简化版的单元测试组件

我们可以直接引用第三方别人写好的构建composer包的工具

composer global require "overtrue/package-builder" --prefer-source

创建项目

package-builder build wunit-laravel
Name of package (example: foo/bar): wunit/wunit-laravel
Namespace of package [Wunit\WunitLaravel]:
Description of package: A simplified version of the laravel framework unit test
Author name of package [wunit]:
Author email of package?1375568288@qq.com
License of package [MIT]: MIT
Do you want to test this package ? [Y/n]: n
Do you want to use php-cs-fixer format your code ? [Y/n]:
Standard name of php-cs-fixer [symfony]:
Package wunit/wunit-laravel created in: ./wunit-laravel

先把基础的调用方法的类写好 srcHttpControllersWunitController.php

<?php
namespace Wunit\WunitLaravel\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

/**
 * use Illuminate\Routing\Controller 这是laravel的基类控制器
 * index() 访问测试的页面
 * store() 是表示用来执行需要测试的方法
 */
class WunitController extends Controller
{
    public function index()
    {
        return view('wunit::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);
    }
}

?>

然后添加一下页面 resourcesviewsindex.blade.php

<!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('test.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>

添加路由srcHttproutes.php

<?php
Route::get('/', 'WunitController@index');
Route::post('/', 'WunitController@store')->name('wunit.store');
?>

laravel项目对于组件加载都是通过服务提供者加载的。添加服务提供者 srcProviderWunitServiceProvide.php

<?php
namespace Wunit\WunitLaravel\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class WunitServiceProvide extends ServiceProvider
{   
    
    public function register()
    {   
        //注册组件路由
        $this->registerRoutes();
        //指定的组件的名称,自定义的资源目录地址
        $this->loadViewsFrom(
            __DIR__.'/../../resources/views', 'wunit'
        );
    }

    public function boot()
    {   

    }

    private function registerRoutes()
    {
        Route::group($this->routeConfiguration(), function () {
            $this->loadRoutesFrom(__DIR__.'/../Http/routes.php');
        });
    }

    private function routeConfiguration()
    {
        return [
            //定义访问路由的域名
            'namespace' => 'Wunit\WunitLaravel\Http\Controllers',
            //这是前缀
            'prefix' => 'wunit',
            //这是中间件
            'middleware' => 'web'

        ];
    }

}
?>

然后在laravel框架中config/app.php中添加对于这个服务提供者的加载

//注册服务提供者的方式
 Wunit\WunitLaravel\Providers\WunitServiceProvide::class,

配置包路径,注意,这里 ../wunit-laravel 为相对路径,不要弄错了

composer config repositories.wunit path ../wunit-laravel
在laravel框架中会显示,这个选项会让composer有更多的选择机会。composer会去包仓库查找,找不到会去本地找:根据repositories参数配置的信息
"repositories": {
        "wunit": {
            "type": "path",
            "url": "../wunit-laravel"
        }
    }

安装扩展包

composer require wunit/wunit-laravel:dev-master

然后再通过php artisan route:list 检测加载的路由

D:\phpstudy\PHPTutorial\WWW\wangruqi\blog>php artisan route:list
+--------+----------+----------+-------------+-----------------------------------------------------------+--------------+
| Domain | Method   | URI      | Name        | Action                                                    | Middleware   |
+--------+----------+----------+-------------+-----------------------------------------------------------+--------------+
|        | GET|HEAD | /        |             | Closure                                                   | web          |
|        | GET|HEAD | api/user |             | Closure                                                   | api,auth:api |
|        | GET|HEAD | wunit    |             | Wunit\WunitLaravel\Http\Controllers\WunitController@index | web          |
|        | POST     | wunit    | wunit.store | Wunit\WunitLaravel\Http\Controllers\WunitController@store | web          |
+--------+----------+----------+-------------+-----------------------------------------------------------+--------------+

然后随便在项目中添加一个测试的类Test.php

通过这个操作访问到在app下定义的test类中的index方法 http://localhost:8181/wangruqi/blog/public/wunit


发现OK!

王如棋博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论