写一个laravel的composer单元测试组件

开发步骤

    • laravel的compser单元测试组件
      • 1.用别人创建好的第三方工具包来创建一些基本的文件,比较方便,手动上一篇博客也做过了
      • 2.在根目录见一个lixiyong的文件夹,在lixiyong文件夹建一个lunit-laravel的文件夹,在lixiyong文件夹下执行cmd命令:
      • 3.把基础的调用方法的类写好 src\Http\Controllers\LunitController.php
      • 4.添加一下页面 resources\views\index.blade.php
      • 5.添加路由src\Http\routes.php
      • 6.添加服务提供者 src\Providers\LunitServiceProvider.php,laravel的组件都是由服务提供者加载的
      • 7.修改lunit-laravel文件夹中的composer.json,添加服务提供者的加载
      • 8.cd到laravel58项目执行,注意:加载本地的组件包加上dev-master,不然会报一个错误
      • 9.通过php artisan route:list检测加载的路由
      • 10.浏览器访问http://127.0.0.1/lixiyong/laravel58/public/lunit

laravel的compser单元测试组件

1.用别人创建好的第三方工具包来创建一些基本的文件,比较方便,手动上一篇博客也做过了

composer global require "overtrue/package-builder" --prefer-source
package-builder build [目标目录名]

2.在根目录见一个lixiyong的文件夹,在lixiyong文件夹建一个lunit-laravel的文件夹,在lixiyong文件夹下执行cmd命令:

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

3.把基础的调用方法的类写好 src\Http\Controllers\LunitController.php


/**
 * 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);
    }
}

4.添加一下页面 resources\views\index.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('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暂时没作用
写一个laravel的composer单元测试组件_第1张图片

5.添加路由src\Http\routes.php


/**
 * 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');

6.添加服务提供者 src\Providers\LunitServiceProvider.php,laravel的组件都是由服务提供者加载的


/**
 * 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');
        });
    }
}

7.修改lunit-laravel文件夹中的composer.json,添加服务提供者的加载

这一步有两个方法,推荐第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中添加对于这个服务提供者的加载
写一个laravel的composer单元测试组件_第2张图片

8.cd到laravel58项目执行,注意:加载本地的组件包加上dev-master,不然会报一个错误

composer config repositories.lixiyong path ../lunit-laravel
composer require lixiyong/lunit-laravel:dev-master

9.通过php artisan route:list检测加载的路由

写一个laravel的composer单元测试组件_第3张图片

10.浏览器访问http://127.0.0.1/lixiyong/laravel58/public/lunit

写一个laravel的composer单元测试组件_第4张图片
这样一个laravel的composer组件开发完成!!!

你可能感兴趣的:(composer组件)