承接 timacdonald/callable-fake 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

timacdonald/callable-fake

Composer 安装命令:

composer require --dev timacdonald/callable-fake

包简介

A testing utility that allows you to fake and capture invokations of a callable / Closure

README 文档

README

Callable Fake: a PHP package by Tim MacDonald

Callable / Closure testing fake

If 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.

It also makes it easy to assert the order of invocations, and how many times a callable has been invoked.

Installation

You can install the package using composer:

composer require timacdonald/callable-fake --dev

Basic usage

This 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...

interface DependencyRepository
{
    public function each(callable $callback): void;
}

This 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.

Before

Let's see what the a test for this method might look like...

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]));
}

After

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]);
    });
}

Available assertions

All assertions are chainable.

assertCalled(callable $callback): self

$callable->assertCalled(function (Dependency $dependency): bool {
    return Str::startsWith($dependency->name, 'spatie/');
});

assertNotCalled(callable $callback): self

$callable->assertNotCalled(function (Dependency $dependency): bool {
    return Str::startsWith($dependency->name, 'timacdonald/');
});

assertCalledIndex(callable $callback, int|array $index): self

Ensure the callable was called in an explicit order, i.e. it was called as the 0th and 5th invocation.

$callable->assertCalledIndex(function (Dependency $dependency): bool {
    return Str::startsWith($dependency, 'spatie/');
}, [0, 5]);

assertCalledTimes(callable $callback, int $times): self

$callable->assertCalledTimes(function (Dependency $dependency): bool {
    return Str::startsWith($dependency, 'spatie/');
}, 999);

assertTimesInvoked(int $times): self

$callable->assertTimesInvoked(2);

assertInvoked(): self

$callable->assertInvoked();

assertNotInvoked(): self

$callable->assertNotInvoked();

Non-assertion API

asClosure(): Closure

If the method is type-hinted with \Closure instead of callable, you can use this method to transform the callable to an instance of \Closure.

$callable = new CallableFake;

$thing->closureTypeHintedMethod($callable->asClosure());

$callable->assertInvoked();

wasInvoked(): bool

if ($callable->wasInvoked()) {
    //
}

wasNotInvoked(): bool

if ($callable->wasNotInvoked()) {
    //
}

called(callable $callback): array

$invocationArguments = $callable->called(function (Dependency $dependency): bool {
    return Str::startsWith($dependency->name, 'spatie/')
});

Specifying return values

If 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.

$callable = CallableFake::withReturnResolver(function (Dependency $dependency): bool {
    if ($dependency->version === '*') {
        return '🤠';
    }

    return '😀';
});

// You would not generally be calling this yourself, this is simply to demonstate
// what will happen under the hood...

$emoji = $callable(new Dependecy(['version' => '*']));

// $emoji === '🤠';

Credits

And a special (vegi) thanks to Caneco for the logo ✨

Thanksware

You 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.

timacdonald/callable-fake 适用场景与选型建议

timacdonald/callable-fake 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.74k 次下载、GitHub Stars 达 45, 最近一次更新时间为 2020 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「closure」 「fake」 「callable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 timacdonald/callable-fake 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 timacdonald/callable-fake 我们能提供哪些服务?
定制开发 / 二次开发

基于 timacdonald/callable-fake 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.74k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 45
  • 点击次数: 18
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 45
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-05