欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

学习Laravel - 测试代码模板

程序员文章站 2022-04-15 22:29:52
...
学习Laravel - 测试代码模板
<?php namespace App\Http\Controllers;
/**
 * 学习 Laravel 的测试用例
 * @link http://laravel.com/docs/5.0
 * @author yanming <ym@mkjump.com>
 * 
 * @tutorial
 * #0, 执行 test/index方法 生成storage/app/route.txt, 添加route.txt内容到app/http/routes.php
 * #1, 进入项目目录, 执行 php artisan route:cache (clear,list), 缓存route
 */
use DB;
use Storage;
use Illuminate\Http\Request;
 
class TestController extends Controller 
{
     
    const NEWLINE = "\n";
    private $route = null; // 生成route的临时变量
     
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
     
    }
     
    /**
     * 反射生成一个route列表
     * @example
     * 测试全部
     * test/index
     * 测试单个例子
     * test/methodName
     */
    public function index($methodName=Null)
    {
        echo "Hello, Lavavel - Self Learning!".self::NEWLINE;
        echo "测试开始".self::NEWLINE;          
        if (!is_null($methodName) && method_exists(new self(), $methodName))
        {
            echo sprintf("测试 %s", $methodName).self::NEWLINE;
            $this->$methodName();
        }
        else
        {
            foreach ($this->getMethod() as $k => $method)
            {
                echo sprintf("测试 %d - %s %s", $k, $method, self::NEWLINE);                              
          //$this->route .= sprintf("Route::get('test/%s', 'TestController@%s')->where(['%s' => '[a-z]+']);", 
          $method, $method, $method).self::NEWLINE;
                // 调用方法
                $this->$method();    
            }   
        }       
        // 生成route  
        //Storage::disk('local')->put('route.txt', $this->route);
    }
     
    /**
     * 反射获取 *Test 方法
     */
    private function getMethod()
    {
        $methods = [];
        $reflector = new \ReflectionClass(new self());
        foreach ($reflector->getMethods() as $methodObj)
        {           
            if (strpos($methodObj->name, "Test") > 0) $methods[] = $methodObj->name;           
        }
        return $methods;
    }
     
    /**
     * The Basics Testing
     */
     
    public function routeTest(){}
     
    public function middlewareTest(){}
     
    public function controllerTest(){}
     
    public function requestTest(){}
     
    public function responseTest(){}
     
    public function viewTest(){}
     
     
    /**
     * Architecture Foundations Testing
     */
    public function serviceProvideTest(){}
     
    public function serviceContainerTest(){}
     
    public function contractsTest(){}
     
    public function facadesTest(){}
     
    public function requestLifeCircleTest(){}
     
    public function applicationStructureTest(){}
     
    /**
     * Service Testing 
     */
     
    public function cacheTest()
    {}
     
    public function collectionTest()
    {}
     
    public function commandBusTest(){}
     
    public function coreExtensionTest(){}
     
     
    public function elixirTest(){}
     
    public function encryptionTest(){}
     
    public function envoyTest(){}
     
    public function errorTest(){}
     
    public function logTest(){}
     
    public function eventsTest(){}
     
    public function filesystemTest(){}
     
    public function hashingTest(){}
     
    public function helpTest(){}
     
    public function localizationTest(){}
     
    public function mailTest(){}
     
    public function packageTest(){}
     
    public function paginationTest(){}
     
    public function queueTest(){}
     
    public function sessionTest(){}
     
    public function templateTest(){}
     
    public function unitTesting() {}
     
    public function validationTest(){}
     
    /**
     * Database Testing
     */
     
    public function basicQueryTest(){}
     
    public function queryBuildTest(){}
     
    public function eloquentTest(){}
     
    public function schemaBuilderTest(){}
     
    public function migrationTest(){}
     
    public function seedTest(){}
     
    public function redisTest(){}
     
     
    /**
     * CLI Testing
     */
     
    public function cliTest(){echo 'cli';}
}

2. [代码]添加到 app/Http/routes.php

Route::get('test/index/{methodName?}', 'TestController@index')->where(['methodName' => '[a-z]+']);

3. [代码]storage/app/route.txt

Route::get('test/routeTest', 'TestController@routeTest')->where(['routeTest' => '[a-z]+']);
Route::get('test/middlewareTest', 'TestController@middlewareTest')->where(['middlewareTest' => '[a-z]+']);
Route::get('test/controllerTest', 'TestController@controllerTest')->where(['controllerTest' => '[a-z]+']);
Route::get('test/requestTest', 'TestController@requestTest')->where(['requestTest' => '[a-z]+']);
Route::get('test/responseTest', 'TestController@responseTest')->where(['responseTest' => '[a-z]+']);
Route::get('test/viewTest', 'TestController@viewTest')->where(['viewTest' => '[a-z]+']);
Route::get('test/serviceProvideTest', 'TestController@serviceProvideTest')->where(['serviceProvideTest' => '[a-z]+']);
Route::get('test/serviceContainerTest', 'TestController@serviceContainerTest')->where(['serviceContainerTest' => '[a-z]+']);
Route::get('test/contractsTest', 'TestController@contractsTest')->where(['contractsTest' => '[a-z]+']);
Route::get('test/facadesTest', 'TestController@facadesTest')->where(['facadesTest' => '[a-z]+']);
Route::get('test/requestLifeCircleTest', 'TestController@requestLifeCircleTest')->where(['requestLifeCircleTest' => '[a-z]+']);
Route::get('test/applicationStructureTest', 'TestController@applicationStructureTest')->where(['applicationStructureTest' => '[a-z]+']);
Route::get('test/cacheTest', 'TestController@cacheTest')->where(['cacheTest' => '[a-z]+']);
Route::get('test/collectionTest', 'TestController@collectionTest')->where(['collectionTest' => '[a-z]+']);
Route::get('test/commandBusTest', 'TestController@commandBusTest')->where(['commandBusTest' => '[a-z]+']);
Route::get('test/coreExtensionTest', 'TestController@coreExtensionTest')->where(['coreExtensionTest' => '[a-z]+']);
Route::get('test/elixirTest', 'TestController@elixirTest')->where(['elixirTest' => '[a-z]+']);
Route::get('test/encryptionTest', 'TestController@encryptionTest')->where(['encryptionTest' => '[a-z]+']);
Route::get('test/envoyTest', 'TestController@envoyTest')->where(['envoyTest' => '[a-z]+']);
Route::get('test/errorTest', 'TestController@errorTest')->where(['errorTest' => '[a-z]+']);
Route::get('test/logTest', 'TestController@logTest')->where(['logTest' => '[a-z]+']);
Route::get('test/eventsTest', 'TestController@eventsTest')->where(['eventsTest' => '[a-z]+']);
Route::get('test/filesystemTest', 'TestController@filesystemTest')->where(['filesystemTest' => '[a-z]+']);
Route::get('test/hashingTest', 'TestController@hashingTest')->where(['hashingTest' => '[a-z]+']);
Route::get('test/helpTest', 'TestController@helpTest')->where(['helpTest' => '[a-z]+']);
Route::get('test/localizationTest', 'TestController@localizationTest')->where(['localizationTest' => '[a-z]+']);
Route::get('test/mailTest', 'TestController@mailTest')->where(['mailTest' => '[a-z]+']);
Route::get('test/packageTest', 'TestController@packageTest')->where(['packageTest' => '[a-z]+']);
Route::get('test/paginationTest', 'TestController@paginationTest')->where(['paginationTest' => '[a-z]+']);
Route::get('test/queueTest', 'TestController@queueTest')->where(['queueTest' => '[a-z]+']);
Route::get('test/sessionTest', 'TestController@sessionTest')->where(['sessionTest' => '[a-z]+']);
Route::get('test/templateTest', 'TestController@templateTest')->where(['templateTest' => '[a-z]+']);
Route::get('test/unitTesting', 'TestController@unitTesting')->where(['unitTesting' => '[a-z]+']);
Route::get('test/validationTest', 'TestController@validationTest')->where(['validationTest' => '[a-z]+']);
Route::get('test/basicQueryTest', 'TestController@basicQueryTest')->where(['basicQueryTest' => '[a-z]+']);
Route::get('test/queryBuildTest', 'TestController@queryBuildTest')->where(['queryBuildTest' => '[a-z]+']);
Route::get('test/eloquentTest', 'TestController@eloquentTest')->where(['eloquentTest' => '[a-z]+']);
Route::get('test/schemaBuilderTest', 'TestController@schemaBuilderTest')->where(['schemaBuilderTest' => '[a-z]+']);
Route::get('test/migrationTest', 'TestController@migrationTest')->where(['migrationTest' => '[a-z]+']);
Route::get('test/seedTest', 'TestController@seedTest')->where(['seedTest' => '[a-z]+']);
Route::get('test/redisTest', 'TestController@redisTest')->where(['redisTest' => '[a-z]+']);
Route::get('test/cliTest', 'TestController@cliTest')->where(['cliTest' => '[a-z]+']);

以上就是学习Laravel - 测试代码模板的内容,更多相关内容请关注PHP中文网(www.php.cn)!