Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Annotation routing based on ThinkPHP6 and how to generate automatic interface documents and automatic test data

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about ThinkPHP6-based annotation routing and how to generate automatic interface documents and automatic test data. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Annotation routing based on ThinkPHP6 + automatic interface document generation + automatic test data generation

> 1. Install

Composer require iszsw/mock

> 2. Add test code and add the Test.php file in the app/controller directory

3. Access routing, test data, interface documentation

Routing: / test?username=zsw data: / mock?mock=1 documentation: / wiki function description 1, comment routing

Route annotations model annotations are automatically injected with Tp6 official annotations

Add AutoValidate comments

@ AutoValidate ({"username": "require | chsAlpha"}, message= {"username": "Please enter a user name"}) 2. API test data generation

MockPack nested data generation supports unlimited nesting

@ MockPack extends MockBase / / data length 0 indicates single-tier data @ var int limit / * * top (valid when limit > 1) * false: ['fields'= > [["a" = > "b"], ["aa" = > "bb"] * true: [["a" = > "b"], ["aa" = > "bb"]] * / @ var boolean main

Example:

/ / the same values of key in MockPack (key) MockPack form a layer of nesting / * * @ 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= "user name", 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": "Osmanthus fragrans" "content": "Iure explicabo officiis minima et impedit sunt dignissimos necessitatibus ratione animi nam aperiam dolorum.", "user": {"username": "Gu Zhiyuan"}}, {"title": "Zheng Wen", "content": "Minus cum unde exercitationem sunt laudantium eveniet voluptatem magni ut cum non." "user": {"username": "Ning Lijuan"}]}

Mock data generation

@ Mock extends MockBase / * Custom string example= "zsw" * Faker method name refer to https://github.com/fzaninotto/Faker * Custom method name\ app\ logic\ Mock::name * example= "method name" | | example= {"method name": {"Parameter 1", "Parameter 2"} * / @ var string | array example3, API document generation

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report