首页 >php教程 >PHP源码 >学习Laravel - 测试代码模板

学习Laravel - 测试代码模板

PHP中文网
PHP中文网原创
2016-05-23 17:10:191081浏览

学习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