首页 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脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Laravel - Artisan 命令 Laravel - Artisan 命令 Aug 27, 2024 am 10:51 AM

Laravel - Artisan 命令 - Laravel 5.7 提供了处理和测试新命令的新方法。它包括测试 artisan 命令的新功能,下面提到了演示?

Laravel和CodeIgniter的最新版本对比 Laravel和CodeIgniter的最新版本对比 Jun 05, 2024 pm 05:29 PM

Laravel9和CodeIgniter4的最新版本提供了更新的特性和改进。Laravel9采用MVC架构,提供数据库迁移、身份验证和模板引擎等功能。CodeIgniter4采用HMVC架构,提供路由、ORM和缓存。在性能方面,Laravel9的基于服务提供者设计模式和CodeIgniter4的轻量级框架使其具有出色的性能。在实际应用中,Laravel9适用于需要灵活性和强大功能的复杂项目,而CodeIgniter4适用于快速开发和小型应用程序。

Laravel 和 CodeIgniter 中数据处理能力的比较如何? Laravel 和 CodeIgniter 中数据处理能力的比较如何? Jun 01, 2024 pm 01:34 PM

比较Laravel和CodeIgniter的数据处理能力:ORM:Laravel使用EloquentORM,提供类对象关系映射,而CodeIgniter使用ActiveRecord,将数据库模型表示为PHP类的子类。查询构建器:Laravel具有灵活的链式查询API,而CodeIgniter的查询构建器更简单,基于数组。数据验证:Laravel提供了一个Validator类,支持自定义验证规则,而CodeIgniter的验证功能内置较少,需要手动编码自定义规则。实战案例:用户注册示例展示了Lar

Laravel和CodeIgniter:哪种框架更适合大型项目? Laravel和CodeIgniter:哪种框架更适合大型项目? Jun 04, 2024 am 09:09 AM

在选择大型项目框架时,Laravel和CodeIgniter各有优势。Laravel针对企业级应用程序而设计,提供模块化设计、依赖项注入和强大的功能集。CodeIgniter是一款轻量级框架,更适合小型到中型项目,强调速度和易用性。对于具有复杂需求和大量用户的大型项目,Laravel的强大功能和可扩展性更合适。而对于简单项目或资源有限的情况下,CodeIgniter的轻量级和快速开发能力则更为理想。

Laravel 和 CodeIgniter 的模板引擎哪一个更好? Laravel 和 CodeIgniter 的模板引擎哪一个更好? Jun 03, 2024 am 11:30 AM

比较了Laravel的Blade和CodeIgniter的Twig模板引擎,根据项目需求和个人偏好进行选择:Blade基于MVC语法,鼓励良好代码组织和模板继承。Twig是第三方库,提供灵活语法、强大过滤器、扩展支持和安全沙箱。

Laravel 和 CodeIgniter 对于初学者来说哪一个更友好? Laravel 和 CodeIgniter 对于初学者来说哪一个更友好? Jun 05, 2024 pm 07:50 PM

对于初学者来说,CodeIgniter的学习曲线更平缓,功能较少,但涵盖了基本需求。Laravel提供了更广泛的功能集,但学习曲线稍陡。在性能方面,Laravel和CodeIgniter都表现出色。Laravel具有更广泛的文档和活跃的社区支持,而CodeIgniter更简单、轻量级,具有强大的安全功能。在建立博客应用程序的实战案例中,Laravel的EloquentORM简化了数据操作,而CodeIgniter需要更多的手动配置。

C++模板和泛型的比较? C++模板和泛型的比较? Jun 04, 2024 pm 04:24 PM

C++中模板和泛型的区别:模板:编译时定义,明确类型化,效率高,代码体积小。泛型:运行时类型化,抽象接口,提供灵活性,效率较低。

Laravel和CodeIgniter:哪种框架更适合小型项目? Laravel和CodeIgniter:哪种框架更适合小型项目? Jun 04, 2024 pm 05:29 PM

对于小型项目,Laravel适用于大型项目,需要强大的功能和安全性。CodeIgniter适用于非常小的项目,需要轻量级和易用性。

See all articles