Annotation routing based on ThinkPHP6 and how to generate automatic interface documents and automatic test data
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