搜尋
首頁php教程PHP源码学习Laravel - 测试代码模板

学习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(&#39;test/%s&#39;, &#39;TestController@%s&#39;)->where([&#39;%s&#39; => &#39;[a-z]+&#39;]);", 
          $method, $method, $method).self::NEWLINE;
                // 调用方法
                $this->$method();    
            }   
        }       
        // 生成route  
        //Storage::disk(&#39;local&#39;)->put(&#39;route.txt&#39;, $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 &#39;cli&#39;;}
}

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

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

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

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

           

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

       

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能