{"id":15473006,"url":"https://github.com/timacdonald/callable-fake","last_synced_at":"2025-04-09T06:11:58.967Z","repository":{"id":43888457,"uuid":"253153464","full_name":"timacdonald/callable-fake","owner":"timacdonald","description":"A PHP testing utility that allows you to fake, capture, and assert against invocations of a callable / Closure.","archived":false,"fork":false,"pushed_at":"2025-03-10T23:07:38.000Z","size":1274,"stargazers_count":45,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T02:11:57.096Z","etag":null,"topics":["callable","closure","hacktoberfest","php","testing"],"latest_commit_sha":null,"homepage":"","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/timacdonald.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-05T04:13:27.000Z","updated_at":"2025-03-10T23:07:41.000Z","dependencies_parsed_at":"2024-05-18T05:18:44.791Z","dependency_job_id":"40d26202-8802-4eac-9456-418adeee8247","html_url":"https://github.com/timacdonald/callable-fake","commit_stats":{"total_commits":130,"total_committers":4,"mean_commits":32.5,"dds":0.0692307692307692,"last_synced_commit":"d06e5c14346815c7baaf0fc5bc93b1b1cf76d428"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timacdonald%2Fcallable-fake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timacdonald%2Fcallable-fake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timacdonald%2Fcallable-fake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timacdonald%2Fcallable-fake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timacdonald","download_url":"https://codeload.github.com/timacdonald/callable-fake/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247987285,"owners_count":21028895,"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":["callable","closure","hacktoberfest","php","testing"],"created_at":"2024-10-02T02:42:08.078Z","updated_at":"2025-04-09T06:11:58.944Z","avatar_url":"https://github.com/timacdonald.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003cimg src=\"/art/header.png\" alt=\"Callable Fake: a PHP package by Tim MacDonald\"\u003e\u003c/p\u003e\n\n# Callable / Closure testing fake\n\nIf you have an interface who's public API allows a developer to pass a Closure / callable, but causes no internal or external side-effects, as these are left up to the developer using the interface, this package may assist in testing. This class adds some named assertions which gives you an API that is very much inspired by Laravel's service fakes. It may be a little more verbose, but it changes the language of the tests to better reflect what is going on.\n\nIt also makes it easy to assert the order of invocations, and how many times a callable has been invoked.\n\n## Installation\n\nYou can install the package using [composer](https://getcomposer.org/):\n\n```\ncomposer require timacdonald/callable-fake --dev\n```\n\n## Basic usage\n\nThis packge requires you to be testing a pretty specfic type of API / interaction to be useful. Imagine you are developing a package that ships with the following interface...\n\n```php\ninterface DependencyRepository\n{\n    public function each(callable $callback): void;\n}\n```\n\nThis interface accepts a callback, and under the hood loops through all \"dependecies\" and passes each one to the callback for the developer to work with.\n\n### Before\n\nLet's see what the a test for this method might look like...\n\n```php\npublic function testEachLoopsOverAllDependencies(): void\n{\n    // arrange\n    $received = [];\n    $expected = factory(Dependency::class)-\u003etimes(2)-\u003ecreate();\n    $repo = $this-\u003eapp[DependencyRepository::class];\n\n    // act\n    $repo-\u003eeach(function (Dependency $dependency) use (\u0026$received): void {\n        $received[] = $dependency;\n    });\n\n    // assert\n    $this-\u003eassertCount(2, $received);\n    $this-\u003eassertTrue($expected[0]-\u003eis($received[0]));\n    $this-\u003eassertTrue($expected[1]-\u003eis($received[1]));\n}\n```\n\n### After\n\n```php\npublic function testEachLoopsOverAllDependencies(): void\n{\n    // arrange\n    $callable = new CallableFake();\n    $expected = factory(Dependency::class)-\u003etimes(2)-\u003ecreate();\n    $repo = $this-\u003eapp[DependencyRepository::class];\n\n    // act\n    $repo-\u003eeach($callable);\n\n    // assert\n    $callable-\u003eassertTimesInvoked(2);\n    $callable-\u003eassertCalled(function (Depedency $dependency) use ($expected): bool {\n        return $dependency-\u003eis($expected[0]);\n    });\n    $callable-\u003eassertCalled(function (Dependency $dependency) use ($expected): bool {\n        return $dependency-\u003eis($expected[1]);\n    });\n}\n```\n\n## Available assertions\n\nAll assertions are chainable.\n\n### assertCalled(callable $callback): self\n\n```php\n$callable-\u003eassertCalled(function (Dependency $dependency): bool {\n    return Str::startsWith($dependency-\u003ename, 'spatie/');\n});\n```\n\n### assertNotCalled(callable $callback): self\n\n```php\n$callable-\u003eassertNotCalled(function (Dependency $dependency): bool {\n    return Str::startsWith($dependency-\u003ename, 'timacdonald/');\n});\n```\n\n### assertCalledIndex(callable $callback, int|array $index): self\n\nEnsure the callable was called in an explicit order, i.e. it was called as the 0th and 5th invocation.\n```php\n$callable-\u003eassertCalledIndex(function (Dependency $dependency): bool {\n    return Str::startsWith($dependency, 'spatie/');\n}, [0, 5]);\n```\n\n### assertCalledTimes(callable $callback, int $times): self\n\n```php\n$callable-\u003eassertCalledTimes(function (Dependency $dependency): bool {\n    return Str::startsWith($dependency, 'spatie/');\n}, 999);\n```\n\n### assertTimesInvoked(int $times): self\n\n```php\n$callable-\u003eassertTimesInvoked(2);\n```\n\n### assertInvoked(): self\n\n```php\n$callable-\u003eassertInvoked();\n```\n\n### assertNotInvoked(): self\n\n```php\n$callable-\u003eassertNotInvoked();\n```\n\n## Non-assertion API\n\n### asClosure(): Closure\n\nIf the method is type-hinted with `\\Closure` instead of callable, you can use this method to transform the callable to an instance of `\\Closure`.\n\n```php\n$callable = new CallableFake;\n\n$thing-\u003eclosureTypeHintedMethod($callable-\u003easClosure());\n\n$callable-\u003eassertInvoked();\n```\n\n### wasInvoked(): bool\n\n```php\nif ($callable-\u003ewasInvoked()) {\n    //\n}\n```\n\n### wasNotInvoked(): bool\n\n```php\nif ($callable-\u003ewasNotInvoked()) {\n    //\n}\n```\n\n### called(callable $callback): array\n\n```php\n$invocationArguments = $callable-\u003ecalled(function (Dependency $dependency): bool {\n    return Str::startsWith($dependency-\u003ename, 'spatie/')\n});\n```\n\n## Specifying return values\n\nIf you need to specify return values, this _could_ be an indicator that this is not the right tool for the job. But there are some cases where return values determine control flow, so it can be handy, in which case you can pass a \"return resolver\" to the named constructor `withReturnResolver`.\n\n```php\n$callable = CallableFake::withReturnResolver(function (Dependency $dependency): bool {\n    if ($dependency-\u003eversion === '*') {\n        return '🤠';\n    }\n\n    return '😀';\n});\n\n// You would not generally be calling this yourself, this is simply to demonstate\n// what will happen under the hood...\n\n$emoji = $callable(new Dependecy(['version' =\u003e '*']));\n\n// $emoji === '🤠';\n```\n\n## Credits\n\n- [Tim MacDonald](https://github.com/timacdonald)\n- [All Contributors](../../contributors)\n\nAnd a special (vegi) thanks to [Caneco](https://twitter.com/caneco) for the logo ✨\n\n## Thanksware\n\nYou are free to use this package, but I ask that you reach out to someone (not me) who has previously, or is currently, maintaining or contributing to an open source library you are using in your project and thank them for their work. Consider your entire tech stack: packages, frameworks, languages, databases, operating systems, frontend, backend, etc.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimacdonald%2Fcallable-fake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimacdonald%2Fcallable-fake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimacdonald%2Fcallable-fake/lists"}