biozshock/phpunit-consecutive 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

biozshock/phpunit-consecutive

最新稳定版本:1.0.4

Composer 安装命令:

composer require --dev biozshock/phpunit-consecutive

包简介

Utility class to replace phpunit withConsecutive

README 文档

README

Why?

In the issue of PHPUnit there are several possibilities to replace missing functionality withConsecutive of PHPUnit. Most of the comments use some sort of the callback:

$expectedArguments = [
    ...
]
->withConsecutive(function ($arg1, $arg2) use (&$index) {
    if ($index === 0) {
        self::assertEquals('some', $arg1);
        ...
    }
});

Eventually i've got sick of writing such code. Writing boilerplate and repeating yourself is no fun.

The class solve also the issue, where you need to operate with arguments <-> return value relation. Giving the developer an ability to define what's returned with each argument set.

Install

composer require --dev biozshock/phpunit-consecutive

Usage

When you need to mock the method which returns a value.

$mock->method('add')
    ->withConsecutive($a, $b)
    ->willReturn(1, 2);

Is replaced by

$mock->method('add')
    ->willRecturnCallback(Consecutive::consecutiveMap([
        [$a, 1],
        [$b, 2]
    ]));

Or return callback, which accepts given arguments:

$mock->method('add')
    ->willRecturnCallback(Consecutive::consecutiveMap([
        [$a, $b, static function (int $a, string $b): bool {
            return $a === (int) $b;
        }],
        [$c, $d, static function (int $c, string $d): bool {
            return str_starts_with($d, (string) $c);
        }]
    ]));

Also, you can test methods that return one of arguments. In this example the test expects zero-index argument $a to be returned:

$mock->method('add')
    ->willRecturnCallback(Consecutive::consecutiveMap([
        [$a, $b],
        [$a, $d]
    ], 0));

Otherwise, when mocked method returns void.

$mock->method('add')
    ->withConsecutive($a, $b);

Is replaced by

$mock->method('add')
    ->willRecturnCallback(Consecutive::consecutiveCall([
        [$a],
        [$b]
    ]));

统计信息

  • 总下载量: 733
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-05