PHPackages                             adriansuter/php-autoload-override - 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. adriansuter/php-autoload-override

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

adriansuter/php-autoload-override
=================================

Override global scoped fully qualified function calls inside your class methods in order to be able to mock them during testing.

2.0(1y ago)2157.7k↑88.5%315MITPHPPHP ^8.2CI failing

Since Nov 23Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/adriansuter/php-autoload-override)[ Packagist](https://packagist.org/packages/adriansuter/php-autoload-override)[ Docs](https://adriansuter.github.io/php-autoload-override/)[ RSS](/packages/adriansuter-php-autoload-override/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (5)Versions (14)Used By (15)

PHP-Autoload-Override
=====================

[](#php-autoload-override)

[![Build Status](https://github.com/adriansuter/php-autoload-override/workflows/Tests/badge.svg?branch=master)](https://github.com/adriansuter/php-autoload-override/actions?query=branch:master)[![Coverage Status](https://camo.githubusercontent.com/74aaca7d6a478aa07c9b5614195fa289843216f1f4ab907344f3c329b4646bfa/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f61647269616e73757465722f7068702d6175746f6c6f61642d6f766572726964652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/adriansuter/php-autoload-override?branch=master)[![Total Downloads](https://camo.githubusercontent.com/65657d247d99e661efdb9ac4ff5808bd43aaa441bc951535d13260b7e0035eb3/68747470733a2f2f706f7365722e707567782e6f72672f61647269616e73757465722f7068702d6175746f6c6f61642d6f766572726964652f646f776e6c6f616473)](https://packagist.org/packages/adriansuter/php-autoload-override)[![License](https://camo.githubusercontent.com/0f8a59612847266aa273d21509a57f4698c23eca84fb934830f536fcc7c9d28d/68747470733a2f2f706f7365722e707567782e6f72672f61647269616e73757465722f7068702d6175746f6c6f61642d6f766572726964652f6c6963656e7365)](https://packagist.org/packages/adriansuter/php-autoload-override)

This library allows overriding fully qualified function calls inside your class methods in order to be able to mock them during testing.

**NOTE: The library can be used for other scenarios as well. But we recommend using it for testing purposes only.**

[PHP-Autoload-Override Website](https://adriansuter.github.io/php-autoload-override/)

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

[](#requirements)

- PHP 8.2 or later
- Composer with PSR-4 (PSR-0 is not supported)

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

[](#installation)

```
$ composer require --dev adriansuter/php-autoload-override 2.0
```

Usage with [PHPUnit](https://phpunit.de/)
-----------------------------------------

[](#usage-with-phpunit)

Say we want to unit test the following class `Probability`.

```
namespace My\App;

class Probability
{
    public function pick(int $probability, string $color1, string $color2): string
    {
        if (\rand(1, 100)  [
        'rand' => function ($min, $max): int {
            if (isset($GLOBALS['rand_return'])) {
                return $GLOBALS['rand_return'];
            }

            return \rand($min, $max);
        }
    ]
]);
```

Now the class `Probability` would be loaded into the PHPUnit runtime such that all function calls to the global scoped `rand()` function in the class `Probability` get overridden by the closure given above.

Our test class can now be written as follows.

```
namespace My\App\Tests;

use My\App\Probability;
use PHPUnit\Framework\TestCase;

final class ProbabilityTest extends TestCase
{
    protected function tearDown()
    {
        if (isset($GLOBALS['rand_return'])) {
            unset($GLOBALS['rand_return']);
        }
    }

    public function testPick()
    {
        $p = new Probability();

        $GLOBALS['rand_return'] = 35;

        $this->assertEquals('blue', $p->pick(34, 'red', 'blue'));
        $this->assertEquals('red', $p->pick(35, 'red', 'blue'));
    }
}
```

The test case `testPick` would call the `pick` method two times. As we have overridden the `\rand` function, we can control its returned value to be always 35. So the first call checks, if the `else`-block gets executed. The second one checks, if the `if`-block gets executed. Hooray, 100% code coverage.

Note that this override would only be applied during the unit tests.

Learn More
----------

[](#learn-more)

- [PHP-Autoload-Override Website](https://adriansuter.github.io/php-autoload-override/)

License
-------

[](#license)

The PHP-Autoload-Override library is licensed under the MIT license. See [License File](LICENSE) for more information.

###  Health Score

56

—

FairBetter than 98% of packages

Maintenance69

Regular maintenance activity

Popularity40

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 86.5% 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 ~236 days

Recently: every ~417 days

Total

9

Last Release

479d ago

Major Versions

v0.1-beta → 1.02020-01-04

1.5 → 2.02025-01-24

PHP version history (4 changes)v0.1-alphaPHP ^7.1

1.2PHP ^7.2 || ^8.0

1.3PHP ^7.3 || ^8.0

2.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/4ff41e3338b4e847e43441cf54099844bae3c789b409a378f8f6374d37ba1057?d=identicon)[adriansuter](/maintainers/adriansuter)

---

Top Contributors

[![adriansuter](https://avatars.githubusercontent.com/u/3974990?v=4)](https://github.com/adriansuter "adriansuter (166 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (25 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")

---

Tags

autoloadoverridephpphp7phpunittestingunit-testingphptestingoverride

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/adriansuter-php-autoload-override/health.svg)

```
[![Health](https://phpackages.com/badges/adriansuter-php-autoload-override/health.svg)](https://phpackages.com/packages/adriansuter-php-autoload-override)
```

###  Alternatives

[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[behat/behat

Scenario-oriented BDD framework for PHP

4.0k96.8M2.0k](/packages/behat-behat)[magento/magento2-functional-testing-framework

Magento2 Functional Testing Framework

15511.5M30](/packages/magento-magento2-functional-testing-framework)[quizlet/hammock

Hammock is a stand-alone mocking library for Hacklang.

27445.5k](/packages/quizlet-hammock)[php-censor/phpdoc-checker

A simple tool for checking that your PHP classes and methods use PHPDocs (PHP DocBlocks Checker fork).

1374.1k2](/packages/php-censor-phpdoc-checker)[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)
