您好(hǎo),歡迎來(lái)到(dào)盤龍區華銘₹∞誠泰網絡工作室官方網站(zhàn)!ZJJCL.CN
新聞動态
您現(xiàn)在的(de)位置:首頁 > 技(jì)術(shù)文(wén)摘
基于 ThinkPHP6 的(de)注解路(lù)由 +ε ©≤ 自(zì)動接口文(wén)檔生(shēng)成 + 自(zì)動測試數(s‌♣hù)據生(shēng)成
來(lái)源:zswemail@qq.c‌☆≤₩om 作(zuò)者:zsw 發布時(shí)間(jiān)‍♣$:2020-07-12 11:13:15 閱讀(dú)÷₽★ 1265 次

作(zuò)者
zsw zswemail@qqcom

主頁 借一(yī)步說(shuō)話(huà) https://zsw.ink

github 借一(yī)步說(shuō)話(huà) https://github.com/iszsw/mo♣‍>ck

gitee 借一(yī)步說(shuō)話(huà) https://gitee.com/iszsw↓&/mock

使用(yòng)
1、安裝

composer require iszsw/mock



 2、添加測試代碼 在 app/controller 目錄下(x€£®ià)增加Test.php文(wén)件(jiànε↕)

<?php
namespace app<α∏\controller;

use app\Bδ∑♠aseController;
use iszsw\mo←₽ck\annotation\illustr ₹π₩ate\AutoValidate;
use  φiszsw\mock\annotation\illus÷♣trate\Route;
use iszsw\mock₹•\annotation\illustrate\Mock;
us↔ ¥e iszsw\mock\annotation\illus₹≥&trate\MockPack;
use♦↓ iszsw\mock\annotatio$¶"πn\illustrate\WikiItem;
use iszsw\mock\±<annotation\illustrate\WikiMe€​nu;

/**
 * @WikiMenu("測試")
 * @‌≤∞package app\controller
 ¶γπ* Author: zsw zswemail@qq.com
 ε±Ω¶*/
class Test extendsφβ‍≥ BaseController
{

    /**
 ♦∑    * @Route("test", met&↓hod="GET")
     * @WikiItem("首頁", d§¥escription="首頁詳情")
     *
   α≥  * @AutoValidate({"username":"requ&✘ ≤ire|chsAlpha"}, message={"usern>≥ame":"請(qǐng)輸入用(yòng)戶名"})
✘÷
     * @Mock("username", ♥‍✘mode="request", titl♥>€e="用(yòng)戶名", example=♥÷"name")
     * @Mock("name", mo✔Ω×de="response", title="名字", exam®βple="name", description="文(™γ↕wén)章(zhāng)ID")
     */
   ✘γδ£ public function index($username){
£​™        return "hello " . $usernaλ↕me;
    }

    /**
     * @Ro©≥©↕ute("mock", method="GET")
     ∏σ‌​* @WikiItem("詳情", descri∏♠δ‍ption="文(wén)章(zhāng)詳情")
     *
 ₹±₽    * @Mock("id", title="I↓©"D", example="numberBetween", desε∏₽±cription="文(wén)章(zh ←↓āng)ID")
     * @Mo&$★×ckPack("articles", mod↑ε€e="response", title="文(wén)•¥$章(zhāng)列表", descriptionδ☆="文(wén)章(zhāng)列表", limit=5∞"Ω)
     * @Mock("id",ΩλΩ♠ mode="response", title="ID", exa↑φmple="randomDigitNotNull", de™≤✘scription="文(wén)章(zhāng)ID")
×‍
     * @Mock("title", mo£₽"de="response", title="¥≠↕ 标題", example="name")
     *♠×Ω @Mock("create_time", mode="responλλse", title="創建時(shí)間(jiān)", exampl ε¥e={"date": {"Y-m-d", "now"}})
     *®π✔₽ @Mock("content", mode="re​λsponse", title="內(nèi)容", exam‌λ↔>ple={"sentence": 10})
     * @≠₽Mock("image", mode="response±↔", title="圖片", example="randomDi $git")
     * @MockPack("user", m↓™∏ain=true, mode="respo αnse", title="用(yòng)戶", descri​✔​ption="發布者信息", limit=0♠•)
     * @Mock("username", ™δ mode="response", title="用(yò£☆γ≤ng)戶名", example="name")
   β¶  * @MockPack("user"σ§₹)
     * @MockPack("a∑÷rticles")
     * @Mock("p← age", mode="response", title="♦‍頁碼", example="randomDigitNotNull",αε  description="當前頁碼")
     */
    pub ₹lic function mock(){}
}÷↔


3、訪問(wèn)路(lù)由、測試數(shù)據✘®φ、接口文(wén)檔

路(lù)由:/test?username=zsw∞"

數(shù)據:/mock?mock=1↓₽ σ

文(wén)檔:/wiki

功能(néng)說(shuō)明(míng)
1、注解路(lù)由

路(lù)由注解 模型注解 自(zì)動注入同Tp6官方注解
新增AutoValidate注解
@AutoValidate({"username":"requir&₹Ω∑e|chsAlpha"}, message={"username♦✘©©":"請(qǐng)輸入用(yòng)戶名"})

 2、接口測試數(shù)據生(α©βshēng)成

MockPack 嵌套數(shù)據生(shēng)成支持 無限級 ₽嵌套
@MockPack extends MockBase
    // 數(≈&₽shù)據長(cháng)度 0表示單層數(shù)據
    @var₩β± int limit 
    
    /*
    ✔γ♥  * 置頂 (limit > 1 時(shí)有(yǒu)效∞¶)
     * false:['fielΩ¥≥®ds'=>[["a"=>"b"], ["aa£""=>"bb"]]]
     * true:[["a"=>"b​≈≤"], ["aa"=>"bb"]]
    →≈ */
    @var boolean ↕₹£main

例:

// MockPack(key)  MockPack中key相(xiànγ•<g)同值之間(jiān)組成一(yī)層嵌套
 ₩"

/**
 * @MockPack("articles", ÷‌mode="response", title="文(wé>↓n)章(zhāng)列表", description="文÷£(wén)章(zhāng)列表", limit=§>∏₽3)
 *
 * @Mock("title", mode="r±≠Ωesponse", title="标題", example=""£name")
 * @Mock("conteλΩnt", mode="response"×→, title="內(nèi)容", example={"sentence" α: 10})
 *
 * @MockPacβ✘₩k("user", main=true, mode="re¶">sponse", title="用(yòng)戶", description= ∑♥"發布者信息", limit=0)
 * @Mock("use‌✘rname", mode="respon♠Ω→se", title="用(yòng)戶名", example="÷​name")
 * @MockPack("user")
 * 
 ★ * @MockPack("articles")
 ≈εβ∏*/


生(shēng)成結果

{
	"articles": [{
		"title": "喬陽",$§' 
		"content": "Vero impedit et §φ₩consequatur quasi doloribus dolo•€res illum sit expedita d® §<oloremque fugiat esse de♦π'leniti quisquam.",
		"' ✘user": {
			"username": "方建明(míλ ÷↑ng)"
		}
	}, {
		"title": "蒙桂花(≈→>huā)",
		"content": "↓→↑ Iure explicabo officiis min♥→ima et impedit sunt digniss'←imos necessitatibus ratione an€&imi nam aperiam dolorum.",
		"user": {¥♠
			"username": "谷緻遠(yuǎn)"
		}
	},λ÷♣Ω {
		"title": "鄭文(wén)",
		"c∑αontent": "Minus cum unde exerc©÷€•itationem sunt laudantium ev§π ‍eniet voluptatem magni uβ←♣t cum non.",
		"user": {
≥≈≥
			"username": "甯麗(lì)娟"
		}
	♦✔}]
}


Mock 數(shù)據生(shēng)成

@Mock extends MockBase
 ↔✔"   /**
      * 自(zì)定義字符串 exampl'≤"♥e="zsw"
      * Faker方<♠≥法名 參考https://github.com/fzaninotto≥‌δ/Faker
      * 自(zì)定義方法名 ​β< \app\logic\Mock::name
      * example=☆≠"方法名" || example={"方法名": {"參數​α→(shù)1", "參數(shù)2"}}
 ∏α
      */
    @var string|array★£ example


3、接口文(wén)檔生(shēng)成

<?php
/**
 * @WikiMenu("§÷€測試")
 */
class Test exte₽★→nds BaseController
{
    <÷/**
     * @Route("test", method="G∏∏≤λET")
     * @WikiItem("首頁$₩", description="首頁詳情")
   ♣∏®  * @Mock("name", mode="r★≈'βesponse", title="名字", exampl÷¶e="name", description=φπ&δ"名字")
     */
    pub®✘lic function index(){
        return€Ω± "zsw";
    }
}



使用(yòng)參考
1、注解路(lù)由
2、接口測試數(shù)據生(shēng)成

<?php
namespace ap₩♣‌→p\controller;

use app\BaseCon↔♣δtroller;
use iszsw\mock\ann®₽₩otation\illustrate\Route;
use iszsw\mo≤$↑ck\annotation\illustratφ↕e\Mock;
use iszsw\mock\↔"'±annotation\illustrate\MockPack₽£;
use iszsw\mock\annotatΩ≥ ₹ion\illustrate\WikiItem;
 £≈
use iszsw\mock\annotatio★××n\illustrate\WikiMenu;
♣®♠

/**
 * @WikiMenu("測試")
 */
​"φ
class Test extends BaseController
{
←≥     /**
     * @Route("mδ§ock", method="GET")
     * @Wiki☆€Item("詳情", description="文(wén)章(zhāng)↑♦≠詳情")
     * @Mock("id"₹δ→→, title="ID", example="n¶‌₽↑umberBetween", descript≠¥₽ion="文(wén)章(zhāng)ID")
     * 
  β¶ "   * @MockPack("articles", mode=ε€ €"response", title="文$↑''(wén)章(zhāng)列表", des π♥cription="文(wén)章(zhāng)列®★表", limit=3)
     * @Mock≤≤×("title", mode="response", title="标題", ≥α‍example="name")
     * @M☆γ&φock("content", mode="resp∞§∑®onse", title="內(nèi)容", example={"sente≤₩nce": 10})
     *
     * @MockPack(<♣"user", main=true, mode="res®¶‌ ponse", title="用(yòng)戶",π¥≠  description="發布者信息", limit=0)
ε©¶     * @Mock("username", mode="res♥↔ponse", title="用(yòng)戶名"♣¥ ≥, example="name")
     * @M>®ockPack("user")
     * 
δΩ×     * @MockPack("articles")
     */×ε∏≈
    public function m↓£ock(){}
}


生(shēng)成的(de)數(shù)據格式為(wèi)

{
	"articles": [{
		"tit ¶&le": "喬陽",
		"conten¶®t": "Vero impedit et c'ασπonsequatur quasi doloribus dolores •↕illum sit expedita doloremque fugiat $ ₩esse deleniti quisquam.",₹•'
		"user": {
			"username": ♦γ"方建明(míng)"
		}
	}, {
		"title": "∑π蒙桂花(huā)",
		"content": "Iure explica×≠bo officiis minima et impedit suntγ♣₹ dignissimos necessit↕±♣≠atibus ratione animi nam aperiam §♦ dolorum.",
		"user": {
			"username∏↔©'": "谷緻遠(yuǎn)"
		}
	}, {
		"σ€σ↕title": "鄭文(wén)",
		"c‌✔ ₹ontent": "Minus cum unde exercitatio≠±✘£nem sunt laudantium eveniet volu↓∏σptatem magni ut cum non.",δ™
		"user": {
			"username"βλ₹": "甯麗(lì)娟"
		}
	}]
}


3、接口文(wén)檔生(shēng)成

<?php
namespace app\controller;

> 
use iszsw\mock\annot≠↔→ation\illustrate\Route;
use€> iszsw\mock\annotation\illustrate\♦ "WikiItem;
use iszsw\mock\annot♥&≤ation\illustrate\WikiMenu∑✔;

/**
 * @WikiMe§×nu("測試")
 */
class Test
{

    /**•∑
     * @Route("test", me®≥<¶thod="GET")
     * @WikiItem("首頁", de✔πscription="首頁詳情")
     * @M←≤ock("username", mode=γλ "request", title="用(yòng)戶名", exampl←λ•≤e="name")
     * @Mock("name",₽÷♦ mode="response", title="Ω•名字", example={"\app\co®≤ntroller\Mock::name": {100}}, d"πescription="文(wén)章(zhāng)ID")
↓÷​     */
    public function ↔αindex($username){}
}