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

学习Laravel - 测试代码模板

May 23, 2016 pm 05:10 PM

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

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1317
25
PHP教程
1268
29
C# 教程
1246
24
laravel入門實例 laravel入門實例 Apr 18, 2025 pm 12:45 PM

Laravel 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

解決 Craft CMS 中的緩存問題:使用 wiejeben/craft-laravel-mix 插件 解決 Craft CMS 中的緩存問題:使用 wiejeben/craft-laravel-mix 插件 Apr 18, 2025 am 09:24 AM

在使用CraftCMS開發網站時,常常會遇到資源文件緩存的問題,特別是當你頻繁更新CSS和JavaScript文件時,舊版本的文件可能仍然被瀏覽器緩存,導致用戶無法及時看到最新的更改。這個問題不僅影響用戶體驗,還會增加開發和調試的難度。最近,我在項目中遇到了類似的困擾,經過一番探索,我找到了wiejeben/craft-laravel-mix這個插件,它完美地解決了我的緩存問題。

laravel用戶登錄功能 laravel用戶登錄功能 Apr 18, 2025 pm 12:48 PM

Laravel 提供了一個全面的 Auth 框架,用於實現用戶登錄功能,包括:定義用戶模型(Eloquent 模型)創建登錄表單(Blade 模板引擎)編寫登錄控制器(繼承 Auth\LoginController)驗證登錄請求(Auth::attempt)登錄成功後重定向(redirect)考慮安全因素:哈希密碼、防 CSRF 保護、速率限制和安全標頭。此外,Auth 框架還提供重置密碼、註冊和驗證電子郵件等功能。詳情請參閱 Laravel 文檔:https://laravel.com/doc

Laravel如何學習 怎麼免費學習Laravel Laravel如何學習 怎麼免費學習Laravel Apr 18, 2025 pm 12:51 PM

想要學習 Laravel 框架,但苦於沒有資源或經濟壓力?本文為你提供了免費學習 Laravel 的途徑,教你如何利用網絡平台、文檔和社區論壇等資源,從入門到掌握,為你的 PHP 開發之旅奠定堅實基礎。

laravel框架安裝方法 laravel框架安裝方法 Apr 18, 2025 pm 12:54 PM

文章摘要:本文提供了詳細分步說明,指導讀者如何輕鬆安裝 Laravel 框架。 Laravel 是一個功能強大的 PHP 框架,它 упростил 和加快了 web 應用程序的開發過程。本教程涵蓋了從系統要求到配置數據庫和設置路由等各個方面的安裝過程。通過遵循這些步驟,讀者可以快速高效地為他們的 Laravel 項目打下堅實的基礎。

laravel有哪些版本 laravel新手版本選擇方法 laravel有哪些版本 laravel新手版本選擇方法 Apr 18, 2025 pm 01:03 PM

在面向初学者的 Laravel 框架版本选择指南中,本文深入探討了 Laravel 的版本差異,旨在協助初學者在眾多版本之間做出明智的選擇。我們將重點介紹每個版本的關鍵特徵、比較它們的優缺點,並提供有用的建議,幫助新手根據他們的技能水準和項目需求挑選最合適的 Laravel 版本。對於初學者來說,選擇一個合適的 Laravel 版本至關重要,因為它可以顯著影響他們的學習曲線和整體開發體驗。

laravel怎麼查看版本號 laravel查看版本號方法 laravel怎麼查看版本號 laravel查看版本號方法 Apr 18, 2025 pm 01:00 PM

Laravel框架內置了多種方法來方便地查看其版本號,滿足開發者的不同需求。本文將探討這些方法,包括使用Composer命令行工具、訪問.env文件或通過PHP代碼獲取版本信息。這些方法對於維護和管理Laravel應用程序的版本控制至關重要。

laravel和thinkphp的區別 laravel和thinkphp的區別 Apr 18, 2025 pm 01:09 PM

Laravel 和 ThinkPHP 都是流行的 PHP 框架,在開發中各有優缺點。本文將深入比較這兩者,重點介紹它們的架構、特性和性能差異,以幫助開發者根據其特定項目需求做出明智的選擇。

See all articles