Backend Development 8 min read

Using iszsw/mock: Installation, Annotation Routing, Mock Data Generation, and API Documentation for PHP Backend Development

This guide explains how to install the iszsw/mock library via Composer, add test controllers with annotation routing and validation, configure static resources, generate nested mock data using MockPack, and produce API documentation, providing complete code examples for PHP backend projects.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using iszsw/mock: Installation, Annotation Routing, Mock Data Generation, and API Documentation for PHP Backend Development

The article introduces the iszsw/mock library, a PHP tool for annotation‑based routing, automatic validation, mock data generation, and API documentation, aimed at backend developers.

Installation

Run the following Composer command to add the library to your project:

composer require iszsw/mock:dev-master

Adding test code

Create a Test.php file under the app/controller directory with the following content:

<?php
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("测试")
 * @package app\controller
 * Author: zsw [email protected]
 */
class Test extends BaseController
{
    /**
     * @Route("test", method="GET")
     * @WikiItem("首页", description="首页详情")
     * @AutoValidate({"username":"require|chsAlpha"}, message={"username":"请输入用户名"})
     * @Mock("username", mode="request", title="用户名", example="name")
     * @Mock("name", mode="response", title="名字", example="name", description="文章ID")
     */
    public function index($username){
        return "hello " . $username;
    }

    /**
     * @Route("mock", method="GET")
     * @WikiItem("详情", description="文章详情")
     * @Mock("id", title="ID", example="numberBetween", description="文章ID")
     * @MockPack("articles", mode="response", title="文章列表", description="文章列表", limit=5)
     * @Mock("title", mode="response", title="标题", example="name")
     * @Mock("content", mode="response", title="内容", example={"sentence":10})
     * @MockPack("user", main=true, mode="response", title="用户", description="发布者信息", limit=0)
     * @Mock("username", mode="response", title="用户名", example="name")
     * @MockPack("user")
     * @MockPack("articles")
     * @Mock("page", mode="response", title="页码", example="randomDigitNotNull", description="当前页码")
     */
    public function mock(){}
}

Static resources

Copy the vendor/iszsw/mock/src/static folder to the public directory. If the path is not /static , adjust it in config/mock.php .

Access URLs

注解路由:/test?username=zsw
测试数据:/mock?mock=1
接口文档:/wiki

Annotation routing

The library supports route, model, and auto‑inject annotations similar to ThinkPHP 6, and introduces an @AutoValidate annotation:

@AutoValidate({"username":"require|chsAlpha"}, message={"username":"请输入用户名"})

Mock data generation

Use @MockPack for nested data generation with unlimited levels. Example annotation:

@MockPack("articles", mode="response", title="文章列表", description="文章列表", limit=3)

Combined with field annotations, it produces JSON like:

{
    "articles": [{
        "title": "乔阳",
        "content": "Vero impedit et consequatur quasi doloribus dolores illum sit expedita doloremque fugiat esse deleniti quisquam.",
        "user": {"username": "方建明"}
    }, {
        "title": "蒙桂花",
        "content": "Iure explicabo officiis minima et impedit sunt dignissimos necessitatibus ratione animi nam aperiam dolorum.",
        "user": {"username": "谷致远"}
    }, {
        "title": "郑文",
        "content": "Minus cum unde exercitationem sunt laudantium eveniet voluptatem magni ut cum non.",
        "user": {"username": "宁丽娟"}
    }]
}

Mock field definition

Define mock values with @Mock and specify Faker methods or custom callbacks:

@Mock("title", mode="response", title="标题", example="name")

Custom example:

@Mock("username", mode="response", title="用户名", example="name")

API documentation generation

Use @WikiMenu , @WikiItem , and related annotations to generate Swagger‑like documentation automatically.

The guide concludes with usage references and links to the original article for further learning.

backendtestingMockPHPAPIannotation
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.