Download the PHP package voral/mock-builder without Composer
On this page you can find all versions of the php package voral/mock-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download voral/mock-builder
More information about voral/mock-builder
Files in voral/mock-builder
Package mock-builder
Short Description 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.
License MIT
Homepage https://github.com/Voral/mock-builder
Informations about the package mock-builder
Utility for Generating Class Mocks
RU
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
Usage
Via Command Line
You can run the utility using the following command:
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:
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 thegetMock
method with support forexpects
.
All versions of mock-builder with dependencies
nikic/php-parser Version ^5.4
phpdocumentor/reflection-docblock Version ^5.6