Thinkphp6-based annotated routing + automatic interface document generation + automatic test data generation

The author

zsw zswemail@qqcom

Home page ZSW. Ink

github github.com/iszsw/mock

gitee gitee.com/iszsw/mock

use

1, install,

composer require iszsw/mock:dev-master

Add test.php to the app/controller directory


      
namespace app\controller;

use app\BaseController;
use iszsw\mock\annotation\illustrate\AutoValidate;
use iszsw\mock\annotation\illustrate\Route;
use iszsw\mock\annotation\illustrate\Mock;
use iszsw\mock\annotation\illustrate\MockPack;
use iszsw\mock\annotation\illustrate\WikiItem;
use iszsw\mock\annotation\illustrate\WikiMenu;

/ * * *@WikiMenu(" Test ") *@package app\controller
 * Author: zsw zswemail@qq.com
 */
class Test extends BaseController
{

    / * * *@Route("test", method="GET")
     * @WikiItem(" home ", description=" home ") * *@AutoValidate({" username ":" the require | chsAlpha "}, message = {" username ":" please enter the user name "}) *@Mock("username", mode="request", title=" username", example="name") *@Mock("name", mode="response", title=" name", example="name", description=" article ID") */
    public function index($username){
        return "hello " . $username;
    }

    / * * *@Route("mock", method="GET")
     * @WikiItem(" details ", description=" article details ") * *@Mock("id", title=" id", example="numberBetween", description=" article ID") *@MockPack("articles", mode="response", title=" description ", limit=5) *@Mock("id", mode="response", title=" id", example="randomDigitNotNull", description=" article id") *@Mock(" title ", the mode = "response", the title = "title", example = "name") *@Mock(" create_time, "mode =" response ", the title = "creation time," example = {" date ": {" Y -m - d", "now"}}) *@Mock(" x ", "x "," x ", "x "," x ", "x"@Mock("image", mode="response", title=" image", example="randomDigit") *@MockPack("user", main=true, mode="response", title=" user", description=" publisher information ", limit=0) *@Mock("username", mode="response", title=" username", example="name") *@MockPack("user")
     * @MockPack("articles")
     * @Mock("page", mode="response", title=" pagename ", example="randomDigitNotNull", description=" pagename ") */
    public function mock(a){}}Copy the code

3, copy document resource file vendor/iszsw/mock/SRC/static place under the public folder If the path is not may not/static in the config/mock. PHP configuration file changes

4, access,

Annotated route: /test? Username = ZSW test data: /mock? Mock =1 Interface documentation: /wikiCopy the code

Functional specifications

The configuration file config/mock.php

1. Annotated routes

Route annotation model annotations are automatically injected with Tp6 official annotations

Add AutoValidate annotation

@AutoValidate({"username":"require|chsAlpha"}, message={"username":"Please enter user name"})
Copy the code

2. Generated interface test data

MockPack nesting data generation supports infinite levels of nesting

MockPack extends MockBase // Data length 0 represents a single layer of data @var intlimit/* * top (limitValid when > 1) *false: ['fields'= > [["a"= >"b"], ["aa"= >"bb"*]]]true: [["a"= >"b"], ["aa"= >"bb"]]
     */
    @var boolean main
Copy the code

Ex. :

/**
 * @MockPack("articles", mode="response", title="Article List", description="Article List".limit=3)
 * @Mock("title", mode="response", title="Title", example="name")
 * @Mock("content", mode="response", title="Content", example={"sentence": 10})
 *
 * @MockPack("user", main=true, mode="response", title="User", description="Publisher Information".limit=0)
 * @Mock("username", mode="response", title="Username", example="name")
 * @MockPack("user")
 * 
 * @MockPack("articles") */ generate result {"articles": [{
		"title": "[qiao Yang"."content": "Vero impedit et consequatur quasi doloribus dolores illum sit expedita doloremque fugiat esse deleniti quisquam."."user": {
			"username": "Fang Jianming"}}, {"title": "Sweet osmanthus"."content": "Iure explicabo officiis minima et impedit sunt dignissimos necessitatibus ratione animi nam aperiam dolorum."."user": {
			"username": "Valley Zhiyuan"}}, {"title": "Cathy"."content": "Minus cum unde exercitationem sunt laudantium eveniet voluptatem magni ut cum non."."user": {
			"username": "Ning Lijuan"}}}]Copy the code

Mock data generation

@mock extends MockBase /** * Custom string example="zsw"Reference https://github.com/fzaninotto/Faker * * Faker method name custom method name/app/logic/Mock: : name * example ="Method name" || example={"Method name": {Parameters of "1"."Parameter 2"}}
      */
    @var string|array example
Copy the code

3. Generated interface documents


      
/ * * *@WikiMenu("测试")
 */
class Test extends BaseController
{
    / * * *@Route("test", method="GET")
     * @WikiItem(" home ", description=" home details ") *@Mock("name", mode="response", title=" name", example="name", description=" name") */
    public function index(a){
        return "zsw"; }}Copy the code

The use of reference

1. Annotated routes

2. Generated interface test data


      
namespace app\controller;

use app\BaseController;
use iszsw\mock\annotation\illustrate\Route;
use iszsw\mock\annotation\illustrate\Mock;
use iszsw\mock\annotation\illustrate\MockPack;
use iszsw\mock\annotation\illustrate\WikiItem;
use iszsw\mock\annotation\illustrate\WikiMenu;

/ * * *@WikiMenu("测试")
 */
class Test extends BaseController
{
    / * * *@Route("mock", method="GET")
     * @WikiItem(" details ", description=" article details ") *@Mock("id", title=" id", example="numberBetween", description=" article ID") * *@MockPack("articles", mode="response", title=" description ", limit=3) *@Mock(" title ", the mode = "response", the title = "title", example = "name") *@Mock(" content ", the mode = "response," title = "content", example = {10} "sentence" :) * *@MockPack("user", main=true, mode="response", title=" user", description=" publisher information ", limit=0) *@Mock("username", mode="response", title=" username", example="name") *@MockPack("user")
     * 
     * @MockPack("articles")
     */
    public function mock(a){}}Copy the code

The generated data is in the format of

{
	"articles": [{
		"title": "[qiao Yang"."content": "Vero impedit et consequatur quasi doloribus dolores illum sit expedita doloremque fugiat esse deleniti quisquam."."user": {
			"username": "Fang Jianming"}}, {"title": "Sweet osmanthus"."content": "Iure explicabo officiis minima et impedit sunt dignissimos necessitatibus ratione animi nam aperiam dolorum."."user": {
			"username": "Valley Zhiyuan"}}, {"title": "Cathy"."content": "Minus cum unde exercitationem sunt laudantium eveniet voluptatem magni ut cum non."."user": {
			"username": "Ning Lijuan"}}}]Copy the code

3. Generated interface documents


      
namespace app\controller;

use iszsw\mock\annotation\illustrate\Route;
use iszsw\mock\annotation\illustrate\WikiItem;
use iszsw\mock\annotation\illustrate\WikiMenu;

/ * * *@WikiMenu("测试")
 */
class Test
{

    / * * *@Route("test", method="GET")
     * @WikiItem(" home ", description=" home details ") *@Mock("username", mode="request", title=" username", example="name") *@MockMock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock: Mock
    public function index($username){}}Copy the code