【callable-fake】Fake your callable function to speed up testing

藏色散人
Release: 2023-04-08 16:52:01
forward
1807 people have browsed it

Callable fake is a PHP testing utility by Tim Macdonald that "allows you to fake, capture and assert calls to callables/closures". In some cases, this package can help allow developers to pass a callable in tests.

It has an API inspired by Laravel fiction as shown below:

// Before, you might collect callables to assert later...
public function testEachLoopsOverAllDependencies(): void
{
    // arrange
    $received = [];
    $expected = factory(Dependency::class)->times(2)->create();
    $repo = $this->app[DependencyRepository::class];
    // act
    $repo->each(function (Dependency $dependency) use (&$received): void {
        $received[] = $dependency;
    });
    // assert
    $this->assertCount(2, $received);
    $this->assertTrue($expected[0]->is($received[0]));
    $this->assertTrue($expected[1]->is($received[1]));
}
Copy after login

Using this package you can use something like:

public function testEachLoopsOverAllDependencies(): void
{
    // arrange
    $callable = new CallableFake();
    $expected = factory(Dependency::class)->times(2)->create();
    $repo = $this->app[DependencyRepository::class];
    // act
    $repo->each($callable);
    // assert
    $callable->assertTimesInvoked(2);
    $callable->assertCalled(function (Depedency $dependency) use ($expected): bool {
        return $dependency->is($expected[0]);
    });
    $callable->assertCalled(function (Dependency $dependency) use ($expected): bool {
        return $dependency->is($expected[1]);
    });
}
Copy after login

The package Assertions such as assertCalled, assertNotCalled, assertInvoked, etc. are provided. For details and examples, be sure to check out the Full list of available assertions in the project readme.

You can learn more about this package on GitHub, get complete installation instructions, and view the source code at timacdonald/callable-fake.

The above is the detailed content of 【callable-fake】Fake your callable function to speed up testing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!