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.
Requires
- php: >=8.1
- nikic/php-parser: ^5.4
- phpdocumentor/reflection-docblock: ^5.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.75
- phpstan/phpstan: ^2.1
- voral/version-increment: ^2.1
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.