PHPackages                             voral/mock-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. voral/mock-builder

ActiveLibrary[Testing &amp; Quality](/categories/testing)

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.

v1.1.1(11mo ago)1451MITPHPPHP &gt;=8.1

Since Apr 24Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/Voral/mock-builder)[ Packagist](https://packagist.org/packages/voral/mock-builder)[ Docs](https://github.com/Voral/mock-builder)[ RSS](/packages/voral-mock-builder/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (16)Used By (1)

Utility for Generating Class Mocks
==================================

[](#utility-for-generating-class-mocks)

[RU](README.ru.md)

Description
-----------

[](#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](doc/testing.md).

---

Installation
------------

[](#installation)

```
composer require --dev voral/mock-builder
```

---

Usage
-----

[](#usage)

### Via Command Line

[](#via-command-line)

You can run the utility using the following command:

```
php bin/vs-mock-builder.php [options]
```

#### Available Options:

[](#available-options)

- `-b, --base `: Specify the base path for source files (default is the current working directory).
- `-t, --target `: Specify the target path for saving generated mocks (default is `./target/`).
- `-f, --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

[](#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](doc/config.md).

If a configuration file exists, its values are used as default parameters. However, parameters passed via the command line take precedence.

---

Features
--------

[](#features)

1. **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.
2. **Class Filtering**:

    - You can specify a list of substrings to filter class names.
    - If no filter is specified, all classes are processed.
3. **PSR-4 Compliance**:

    - Transformed files are saved in a directory structure corresponding to the class namespace.
4. **Method Cleanup**:

    - All public methods remain in the class, but their bodies are cleared (content is removed).
5. **Customization via Visitors**:

    - You can customize the AST processing using visitors. For more details on built-in and custom visitors, see the section [Visitors](doc/visitor.md).

---

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Composer

---

License
-------

[](#license)

The project is distributed under the MIT license. For details, see the [LICENSE](LICENSE) file.

---

Additional Information
----------------------

[](#additional-information)

- [Configuration File](doc/config.md): Detailed description of all configuration parameters.
- [Visitors](doc/visitor.md): Information about built-in and custom visitors.
- [Testing with the MockTools Trait](doc/testing.md): A guide to testing generated mocks.

---

Frequently Asked Questions (FAQ)
--------------------------------

[](#frequently-asked-questions-faq)

1. [What to Do with Classes in the Global Namespace?](doc/faq.md#what-to-do-with-classes-in-the-global-namespace)
2. [How to Create Mocks for Functions in the Global Scope?](doc/faq.md#how-to-create-mocks-for-functions-in-the-global-scope)

---

### Changes

[](#changes)

The change history can be found in the [CHANGELOG.md](CHANGELOG.md) file.

### Todo

[](#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 the `getMock` method with support for `expects`.*

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance51

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Recently: every ~9 days

Total

15

Last Release

341d ago

Major Versions

v0.8.0 → v1.0.02025-05-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/ce4dfd67b12e198becd71253723d0c83044cb4689b735f2fd4eee4ac191c41ec?d=identicon)[Voral](/maintainers/Voral)

---

Top Contributors

[![Voral](https://avatars.githubusercontent.com/u/327165?v=4)](https://github.com/Voral "Voral (75 commits)")

---

Tags

code-transformationlegacy-codemockmockgenmockgeneratorphptestingunit-testingphptestingunit testingdependency-injectionmockPSR-4mock-generatorcode-transformationlegacy-systems

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/voral-mock-builder/health.svg)

```
[![Health](https://phpackages.com/badges/voral-mock-builder/health.svg)](https://phpackages.com/packages/voral-mock-builder)
```

###  Alternatives

[infection/infection

Infection is a Mutation Testing framework for PHP. The mutation adequacy score can be used to measure the effectiveness of a test set in terms of its ability to detect faults.

2.2k26.2M1.8k](/packages/infection-infection)[donatj/mock-webserver

Simple mock web server for unit testing

1382.5M80](/packages/donatj-mock-webserver)[blastcloud/guzzler

Supercharge your app or SDK with a testing library specifically for Guzzle.

272419.3k35](/packages/blastcloud-guzzler)[graham-campbell/analyzer

Checks if referenced classes really exist.

29594.5k112](/packages/graham-campbell-analyzer)[quizlet/hammock

Hammock is a stand-alone mocking library for Hacklang.

27445.5k](/packages/quizlet-hammock)[doppiogancio/mocked-client

A simple way to mock a client

2174.9k3](/packages/doppiogancio-mocked-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
