voral/mock-builder
Composer 安装命令:
composer require --dev voral/mock-builder
包简介
A PHP utility to generate mock classes by processing source code, leaving only public methods and clearing their bodies. Supports PSR-4 and is ideal for testing complex systems without Dependency Injection.
关键字:
README 文档
README
Description
This utility is designed to automatically generate "mock" shells of classes from PHP source code. It allows you to:
- Keep only public methods of classes.
- Clear method bodies to prepare them for use in tests.
- Save the transformed files into a directory structure compliant with the PSR-4 standard.
This tool is particularly useful for testing complex systems where:
- It is impossible or difficult to use standard mocking tools (e.g., PHPUnit).
- Dependency Injection is not supported.
- The PSR-4 standard is not fully implemented.
After processing the classes, you can add a special trait to the generated mocks to control their behavior during testing. For more details on how to use this trait, see the section Testing with the MockTools Trait.
Installation
composer require --dev voral/mock-builder
Usage
Via Command Line
You can run the utility using the following command:
php bin/vs-mock-builder.php [options]
Available Options:
-b, --base <path>: Specify the base path for source files (default is the current working directory).-t, --target <path>: Specify the target path for saving generated mocks (default is./target/).-f, --filter <filter>: Specify a filter to select classes by name (comma-separated).-d, --display: Display the progress of processing.-c, --clear-cache: Clear the class dependency graph cache.-h, --help: Display help information.
Examples:
# Process all files in a directory
php bin/vs-mock-builder.php -b=/path/to/source -t=/custom/target/dir/ -f=Controller
Via Configuration File
You can create a configuration file .vs-mock-builder.php in the root of your project. An example of the file's content
and a description of all available parameters can be found in the section Configuration File.
If a configuration file exists, its values are used as default parameters. However, parameters passed via the command line take precedence.
Features
-
Processing Interfaces, Classes, and Traits:
- The utility supports processing classes, interfaces, and traits.
- A separate file is created in the target directory for each class or interface.
-
Class Filtering:
- You can specify a list of substrings to filter class names.
- If no filter is specified, all classes are processed.
-
PSR-4 Compliance:
- Transformed files are saved in a directory structure corresponding to the class namespace.
-
Method Cleanup:
- All public methods remain in the class, but their bodies are cleared (content is removed).
-
Customization via Visitors:
- You can customize the AST processing using visitors. For more details on built-in and custom visitors, see the section Visitors.
Requirements
- PHP >= 8.1
- Composer
License
The project is distributed under the MIT license. For details, see the LICENSE file.
Additional Information
- Configuration File: Detailed description of all configuration parameters.
- Visitors: Information about built-in and custom visitors.
- Testing with the MockTools Trait: A guide to testing generated mocks.
Frequently Asked Questions (FAQ)
- What to Do with Classes in the Global Namespace?
- How to Create Mocks for Functions in the Global Scope?
Changes
The change history can be found in the CHANGELOG.md file.
Todo
- Add testing for the entire package
It is necessary to cover all key classes with unit tests for the further development of the project. - Add the ability to interact with mocks in a familiar way, as in PHPUnit
For example, implement thegetMockmethod with support forexpects.
voral/mock-builder 适用场景与选型建议
voral/mock-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「testing」 「mock」 「dependency-injection」 「unit-testing」 「PSR-4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 voral/mock-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 voral/mock-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 voral/mock-builder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
Testing Suite For Lumen like Laravel does.
The PHP SDK for Checkmango
Mock PSR-18 HTTP client
Alfabank REST API integration
Provides double configurators with fluent interface support.
统计信息
- 总下载量: 45
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-24