基于 ThinkPHP6 的(de)注解路(lù)由 + 自(zì)動接口σ'文(wén)檔生(shēng)成 + 自×↓φ(zì)動測試數(shù)據生(shēng)成
來(lái)源:zswemail@qq.com 作(zuò)者:zsw 發布時(shí)間✘™(jiān):2020-07-12 11:13:15 閱讀(dú) 1243 次
作(zuò)者
zsw zswemail@qqcom
主頁 借一(yī)步說(shuō)話(huà)&nb ±sp;https://zsw.ink
github 借一(yī)步說(shuō)話(huà) https://github.com/iszsw/mock
gitee 借一(yī)步說(shuō)話(huà)&nb ♠sp;https://gitee.com/iszsw/mock
使用(yòng)
1、安裝
composer require iszsw/mock
2、添加測試代碼 在 app/controller 目Ω'錄下(xià)增加Test.php文(wén)件(jiàn¶≤δ®)
<?php
namespace app\cont↔₩ $roller;
use app\BaseController;
use±γ iszsw\mock\annotation\illustrat×β>☆e\AutoValidate;
use iszsw\mock\annotation\illu≠∞ε≈strate\Route;
use iszsw\mock\annotation\illustra ®♥te\Mock;
use iszsw\moc≈δ→k\annotation\illustrate\MockPackσ₹≥;
use iszsw\mock\annotation\illustr©♠λate\WikiItem;
use iszsw\mo&♣→∞ck\annotation\illustrate\Wikφ∑iMenu;
/**
* @WikiMenu("測試")
* @package ✘¶©app\controller
* Author: zsw→♠♥ zswemail@qq.com
*/
class Test extends B☆★↓aseController
{
/**
₹← * @Route("test", method="GET")
* @WikiIt£em("首頁", description="首頁詳情")₽Ω↓
*
* @AutoValid¥Ω→<ate({"username":"require|chsAlpha"₹}, message={"username":"請(qǐng)輸入用(yòng)戶名¶€β"})
* @Mock("usernam↕↓→e", mode="request", title="用(yòng)戶名",∞✘♥☆ example="name")
* @Mock("name☆✔←", mode="response", title="名字", example="name", ♥α"×description="文(wén)章(zhāng)ID")
✔¶*/
public function♦↑•σ index($username){
return "hello " . $§ππusername;
}
/**
* @Route>¥("mock", method="GET")
* ✔α≠@WikiItem("詳情", description="文(wén)章(zhāng)詳情≤α")
*
* @Mock("id", title="ID", •→↔λexample="numberBetween", description="文(wén)章ε×≠(zhāng)ID")
* @MockPack("articles", mode=π©"response", title="文(wén)章(zhāng)列表", λ"description="文(wén)章(zhāng)列表", limit=5)
±♠ * @Mock("id", mode="respon♦δ←₩se", title="ID", example="ran÷φ₽domDigitNotNull", description="文(wén)章(Ω¥zhāng)ID")
* @Mock("title", mode="response"♥ε∞, title="标題", example="name")
'←Ω * @Mock("create_time", mode="response"×☆→, title="創建時(shí)間(jiān)", ex♦ample={"date": {"Y-m-d", "now"}})
* @λ₹Mock("content", mode="re×₽₹sponse", title="內(nèi)容", example ±={"sentence": 10})
↔₩ * @Mock("image", mode="response", title="圖片"π↔♦∞, example="randomDigit")
* @Mo&¥ckPack("user", main=true, ₩>mode="response", title="用(yòng)戶", description✔↑←="發布者信息", limit=0)
* @Mock("user♦≤ε↕name", mode="response", ≤®£title="用(yòng)戶名", example="name")
↓Ω€ * @MockPack("user")±πφ∏
* @MockPack("articles")
♠• * @Mock("page", m£✔ode="response", title="頁碼"→>≥♠, example="randomDigitNotNull"♥φ, description="當前頁碼"§↑)
*/
public 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":"require|chsAlpha$₹γ"}, message={"username":"請(qǐng)輸入用(yòng)戶名"})
2、接口測試數(shù)據生(shēng)成
MockPack 嵌套數(shù)據生(shēng)成支持 無限級嵌套
@MockPack extends MockBase
// 數(shù)據長λ•(cháng)度 0表示單層數(shù)據
@va≤∏∏r int limit
/*
* 置頂 (limiΩ&™₹t > 1 時(shí)有(yǒu)效)
* false:['fie∏δπlds'=>[["a"=>"b"], ["aa"=>"bb"]]]
≠☆>σ * true:[["a"=>"b"], <↑["aa"=>"bb"]]
*/
@var boolean main
例:
// MockPack(key) MockPack中key相(xiàng)同值之間(jiān)®ε₩組成一(yī)層嵌套
/**
* @Mock∞↓∑Pack("articles", mode="response", title="文(wén)章♥♠←(zhāng)列表", description="♣≈文(wén)章(zhāng)列表", limit=3)
*
* @Mock("tit♦↔le", mode="response", title="标題 $", example="name")
∑ * @Mock("content", mode="response", tit&✘≤&le="內(nèi)容", example={"sentence": 10} < )
*
* @MockPack("use♥©r", main=true, mode="response", title="用(yòng)戶" >, description="發布者信息", limit=™™0)
* @Mock("username", mode="response"σβ÷§, title="用(yòng)戶名", example="name")
* @MockP₩&ack("user")
*
* @MockP♣"'ack("articles")
*/
生(shēng)成結果
{
"articles": [{
"title": "喬陽",φ&
"content": "Vero impedit et consequatur q± uasi doloribus dolores ill♠≠≥um sit expedita doloremque fugiat esse ★αdeleniti quisquam.",
"user": {
"username": "方建明(míng)"
δ$α≠ }
}, {
"title": "蒙桂花(huā)",
"content": §Ω"Iure explicabo officiis minima et impedit suntΩ↔ dignissimos necessitatφibus ratione animi nam aperiam dolorum.",&÷≤β
"user": {
"username": "谷緻遠(yuǎn)"
}
§£★ }, {
"title": "鄭文(wén)"♦€,
"content": "Minus>β cum unde exercitationem sunt laudantium ¶↓eveniet voluptatem magni ut cum ♣↕non.",
"user": {
"usernaε ♥me": "甯麗(lì)娟"
}
}]
}
Mock 數(shù)據生(shēng)成
@Mock extends MockBase
/**
☆π♥ * 自(zì)定義字符串 example="zsw"
★λ♠ * Faker方法名 參考https://github.com/fzaninΩ♠otto/Faker
* 自(zì)定義方法名 \app\lo±∑gic\Mock::name
* example="方法名∞'←" || example={"方法名": {"參數(shù)1",>∏♦ "參數(shù)2"}}
*/
@v☆₹ar string|array example
3、接口文(wén)檔生(shēng)成
<?php
/**
* @WikiMenu("測試")
ε */
class Test extends BaseController
{
♣ /**
* @Route("test", method="GET")
¥βσ * @WikiItem("首頁", descriptiβσon="首頁詳情")
* @Mock("name",★< mode="response", title="名字", example="naΩ me", description="名字")
*/
public fun£≥↓&ction index(){
retu±₩÷rn "zsw";
}
}
使用(yòng)參考
1、注解路(lù)由
2、接口測試數(shù)據生(shēng)成
<?php
namespace app\controller;
§
use app\BaseControl ¶♠↓ler;
use iszsw\mock\annotati"♣₩on\illustrate\Route;
use iszsw\mock\annotati☆♦σon\illustrate\Mock;
use iszsw\mock\a ×'¥nnotation\illustrate\MockPack;
∏×
use iszsw\mock\annotation\illustrate\♥¥γWikiItem;
use iszsw\mock\annotatioε₽n\illustrate\WikiMenu;
/**
* @WikiMe±✔¥≈nu("測試")
*/
class Test <∑↑extends BaseController
{
&' /**
* @Route("mock", method="GET")
♦↕&* @WikiItem("詳情", description="文(w™¥ ↕én)章(zhāng)詳情")
* @Mock("id", title="ID", →↔example="numberBetween", desc§∑©ription="文(wén)章(zhāng)ID")
ΩΩ*
* @MockPack("articles", mode="response"♠£÷♣, title="文(wén)章(zhāng∑✘₩)列表", description="文(wén)≈δ♠章(zhāng)列表", limit=3)
* @Mock("title", mo'↔↔de="response", title="标題", example="π£name")
* @Mock("conte ↑★←nt", mode="response", title="內(nèi)容", exam§β¶≥ple={"sentence": 10})
*
₩™£ * @MockPack("user", ma≠σin=true, mode="response"→ φ, title="用(yòng)戶", de ₩scription="發布者信息", limit=0)
* @Mock♦ ("username", mode="response", title="用(yòng)戶名", λ↓←example="name")
* @MockPack(ε←₽"user")
*
* @MockPac↕∞₩ k("articles")
*/
public function mock✘₽σ(){}
}
生(shēng)成的(de)數(shù)εα≤♠據格式為(wèi)
{
"articles": [{
"title": "喬××₽陽",
"content": "Vero impα✘→edit et consequatur quasi doloribus dolores illum₩π sit expedita doloremque fugiat esse delen₽↑λ iti quisquam.",
"user": {
"username®π¶": "方建明(míng)"
}
♠≥★♥ }, {
"title": "蒙桂花(huā)",
¶&♦ "content": "Iure explicabo officii☆s minima et impedit sunt dignissim♣✘os necessitatibus ratione animi nam aperiam dolor<π♥um.",
"user": {
"username": "谷緻遠(yuǎn)"
✘¶
}
}, {
"title":φ¥↑ "鄭文(wén)",
"content": δ¥α<"Minus cum unde exer↑ δ€citationem sunt laudantiΩα↕um eveniet voluptatem magni ut cum noσγn.",
"user": {
♥↑→' "username": "甯麗(lì)娟"
≠•}
}]
}
3、接口文(wén)檔生(shēng)成
<?php
namespace γapp\controller;
use iszsw\mock\annotat✔♣ion\illustrate\Route;
use iszsw\m→ ∏ock\annotation\illustrat$₽♦e\WikiItem;
use iszsw\mockπ≈\annotation\illustrate\Wikλβ©♠iMenu;
/**
* @WikiMenu("測試")
*/
cl¶<©ass Test
{
/**
* @R↑oute("test", method="GET")
* @WikiItem("首頁"€¥✔, description="首頁詳情")
* @Mock("usγ>÷ername", mode="request", title="用(yò★≈☆≤ng)戶名", example="name"Ω÷♠)
* @Mock("name", mode="response", title="名♠ 字", example={"\app\controller\Mock::name": {1←™00}}, description="文(wén↔)章(zhāng)ID")
*/
publi☆λc function index($username){}
}
- 下(xià)一(yī)篇:搭建簡單的(de)PHP運行(xíng)環境