{"id":15008335,"url":"https://github.com/adriansuter/php-autoload-override","last_synced_at":"2025-04-13T09:42:26.387Z","repository":{"id":36244604,"uuid":"222701879","full_name":"adriansuter/php-autoload-override","owner":"adriansuter","description":"Override fully qualified function calls inside your class methods in order to be able to mock them during testing.","archived":false,"fork":false,"pushed_at":"2025-04-04T08:45:59.000Z","size":165,"stargazers_count":21,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-04T09:32:48.288Z","etag":null,"topics":["autoload","override","php","php7","phpunit","testing","unit-testing"],"latest_commit_sha":null,"homepage":"https://adriansuter.github.io/php-autoload-override/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adriansuter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-19T13:24:29.000Z","updated_at":"2025-04-04T08:45:58.000Z","dependencies_parsed_at":"2024-06-18T20:08:02.107Z","dependency_job_id":"8745e4ae-5bd8-4e73-ab37-3b0074d83f08","html_url":"https://github.com/adriansuter/php-autoload-override","commit_stats":{"total_commits":125,"total_committers":3,"mean_commits":"41.666666666666664","dds":"0.14400000000000002","last_synced_commit":"03f896a913abfa64af2ae53de65565b0a83bc6a7"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriansuter%2Fphp-autoload-override","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriansuter%2Fphp-autoload-override/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriansuter%2Fphp-autoload-override/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriansuter%2Fphp-autoload-override/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adriansuter","download_url":"https://codeload.github.com/adriansuter/php-autoload-override/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248693518,"owners_count":21146815,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autoload","override","php","php7","phpunit","testing","unit-testing"],"created_at":"2024-09-24T19:17:40.593Z","updated_at":"2025-04-13T09:42:26.351Z","avatar_url":"https://github.com/adriansuter.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP-Autoload-Override\n\n[![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)\n[![Coverage Status](https://coveralls.io/repos/github/adriansuter/php-autoload-override/badge.svg?branch=master)](https://coveralls.io/github/adriansuter/php-autoload-override?branch=master)\n[![Total Downloads](https://poser.pugx.org/adriansuter/php-autoload-override/downloads)](https://packagist.org/packages/adriansuter/php-autoload-override)\n[![License](https://poser.pugx.org/adriansuter/php-autoload-override/license)](https://packagist.org/packages/adriansuter/php-autoload-override)\n\nThis library allows overriding fully qualified function calls inside your class methods in order to\nbe able to mock them during testing.\n\n**NOTE: The library can be used for other scenarios as well. But we recommend using it for testing purposes\nonly.**\n\n[PHP-Autoload-Override Website](https://adriansuter.github.io/php-autoload-override/)\n\n\n## Requirements \n\n- PHP 8.2 or later\n- Composer with PSR-4 (PSR-0 is not supported)\n\n\n## Installation\n\n```bash\n$ composer require --dev adriansuter/php-autoload-override 2.0\n```\n\n\n## Usage with [PHPUnit](https://phpunit.de/)\n\nSay we want to unit test the following class `Probability`.\n\n```php\nnamespace My\\App;\n\nclass Probability\n{\n    public function pick(int $probability, string $color1, string $color2): string\n    {\n        if (\\rand(1, 100) \u003c= $probability) {\n            return $color1;\n        } else {\n            return $color2;\n        }\n    }\n}\n```\n\nThe class has one method `pick` that takes a probability (between 0 and 100) and two color names as arguments.\nThe method would then use the `rand` function of the global scope to generate a random number and\nif the generated number is smaller equal to the given probability, then the method would return \nthe first color, otherwise the method would return the second color.\n\n### The problem \n\nAs we cannot control the output of the `rand` function (it is in global scope), we cannot unit test\nthat method. Well, until now. Using the PHP-Autoload-Override library, it is possible to \noverride the `rand` function and therefore control its generated random number.\n\n### The solution\n\nAfter installing the PHP-Autoload-Override library, we would open the bootstrap script of our test suite\n(see also [PHPUnit Configuration](https://phpunit.readthedocs.io/en/8.4/configuration.html#the-bootstrap-attribute)).\nThere we will write the following code\n\n```php\n// tests/bootstrap.php\n\n/** @var \\Composer\\Autoload\\ClassLoader $classLoader */\n$classLoader = require_once __DIR__ . '/../vendor/autoload.php';\n\n\\AdrianSuter\\Autoload\\Override\\Override::apply($classLoader, [\n    \\My\\App\\Probability::class =\u003e [\n        'rand' =\u003e function ($min, $max): int {\n            if (isset($GLOBALS['rand_return'])) {\n                return $GLOBALS['rand_return'];\n            }\n\n            return \\rand($min, $max);\n        }\n    ]\n]);\n```\n\nNow the class `Probability` would be loaded into the PHPUnit runtime such that all function calls to the global scoped \n`rand()` function in the class `Probability` get overridden by the closure given above.\n\nOur test class can now be written as follows.\n\n```php\nnamespace My\\App\\Tests;\n\nuse My\\App\\Probability;\nuse PHPUnit\\Framework\\TestCase;\n\nfinal class ProbabilityTest extends TestCase\n{\n    protected function tearDown()\n    {\n        if (isset($GLOBALS['rand_return'])) {\n            unset($GLOBALS['rand_return']);\n        }\n    }\n\n    public function testPick()\n    {\n        $p = new Probability();\n\n        $GLOBALS['rand_return'] = 35;\n\n        $this-\u003eassertEquals('blue', $p-\u003epick(34, 'red', 'blue'));\n        $this-\u003eassertEquals('red', $p-\u003epick(35, 'red', 'blue'));\n    }\n}\n```\n\nThe test case `testPick` would call the `pick` method two times. As we have overridden the `\\rand` function, we can\ncontrol its returned value to be always 35. So the first call checks, if the `else`-block\ngets executed. The second one checks, if the `if`-block gets executed. Hooray, 100% code coverage.\n\nNote that this override would only be applied during the unit tests.\n\n\n## Learn More\n\n- [PHP-Autoload-Override Website](https://adriansuter.github.io/php-autoload-override/)\n\n\n## License\n\nThe PHP-Autoload-Override library is licensed under the MIT license. See [License File](LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadriansuter%2Fphp-autoload-override","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadriansuter%2Fphp-autoload-override","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadriansuter%2Fphp-autoload-override/lists"}