marcin-orlowski/phpunit-extra-asserts 问题修复 & 功能扩展

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

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

marcin-orlowski/phpunit-extra-asserts

Composer 安装命令:

composer require marcin-orlowski/phpunit-extra-asserts

包简介

Extra asserts and helpers to make your unit testing easier.

README 文档

README

Latest Stable Version License

Collection of additional asserts to be used with PHP Unit testing framework. Helpers are split into dedicated namespaces:

  • ExtraAsserts: various asserts to help testing your code,
  • Generator: various helper methods producing random values for your tests,

Installation

composer require --dev marcin-orlowski/phpunit-extra-asserts

Usage

As ExtraAsserts come as set of static methods so you just need to add related use to your test class and all the methods should be simply available via static reference ExtraAsserts::.... For example:

use \MarcinOrlowski\PhpunitExtraAsserts\ExtraAsserts;

class MyBaseTestClass extends ... {

    use \MarcinOrlowski\PhpunitExtraAsserts\ExtraAsserts;
    use \MarcinOrlowski\PhpunitExtraAsserts\Type;

    [...]

    public function testSomething(): void
    {
        [...]

        ExtraAsserts::assertIsType($val, [Type::STRING, Type::BOOL]);
        ExtraAsserts::assertRFC3339($stamp);
    }
}

Available asserts

Assert Description Added
assertArrayElement(string $key, array $array, string $expected_value) Asserts given array has specified key and it's value is according to expectations. v1.0.0
assertArrayEquals(array $array_a, array $array_b) Asserts two arrays are equivalent. v1.0.0
assertArrayHasKeys(array $required_keys, array $array) Asserts array has ALL the required keys. v1.0.0
assertArraysHaveDifferences(int $diff_count, array $array_a, array $array_b) Asserts two arrays differ by exactly given number of elements (default is 0). v1.0.0
assertRFC3339(string $stamp) Asserts provided string is valid RFC3339 timestamp string. v1.0.0
assertRFC3339OrNull(string $stamp) Asserts $stamp string is valid RFC3339 timestamp string or @null. v1.0.0
massAssertEquals(array $array_a, array $array_b, array $ignored_keys) Assert if keys from response have the same values as in original array. Keys listed in $skip_keys are ignored. v1.0.0
assertIsObjectOrExistingClass(string OR object $cls_or_obj, ?string $var_nam = null) Asserts given $value (which the value of variable named $var_name) is an object or name name of existing class. v3.0.0
[DEPRECATED] assertIsArray(mixed $value, ?string $var_name = null) Asserts given $value (which the value of variable named $var_name) is an array. v3.0.0
[DEPRECATED] assertIsBool(mixed $value, ?string $var_name = null) Asserts given $value (which the value of variable named $var_name) is a boolean. v3.0.0
[DEPRECATED] assertIsFloat(mixed $value, ?string $var_name = null) Asserts given $value (which the value of variable named $var_name) is an float. v3.0.0
[DEPRECATED] assertIsInteger(mixed $value, ?string $var_name = null) Asserts given $value (which the value of variable named $var_name) is an integer. v3.0.0
[DEPRECATED] assertIsInteger(mixed $value, ?string $var_name = null) Asserts given $value (which the value of variable named $var_name) is an integer. v3.0.0
[DEPRECATED] assertIsObject(mixed $value, ?string $var_name = null) Asserts given $value (which the value of variable named $var_name) is an object (of any class). v3.0.0
[DEPRECATED] assertIsString(mixed $value, ?string $var_name = null) Asserts given $value (which the value of variable named $var_name) is an string. v3.0.0

NOTE: Deprecated methods will be removed in v5.4.0

Helper methods

Method Description
printArray(array $array, int $indent) Prints content of given array in compacted form.

Generator methods

Method Description
getRandomString(?string $prefix = null, int $length = 24, string $separator = '_') Returns string of total length equal to specified limit (incl. optional prefix or separator strings)
getRandomStringOrNull(?string $prefix = null, int $length = 24, string $separator = '_', float $trueValueProbability = 0.5) Returns either random string or NULL. Probability (float value in range 0-1) specifies the chance of drawing the string value. The higher the value of this argument, the higher the chance for string being returned.
getRandomFloat(float $min, float $max, int $fractionalDigits = 0) Draws random float from given range. If given max is lower than min, these two will be swapped. If number of fractional digits is non-zero, value will be rounded to given number of fractional digits. Value 0 means not rounding.
getRandomInt(int $min = 0, int $max = 100) Draws random int from given range. If given max is lower than min, these two will be swapped.
getRandomBool(float $trueValueProbability = 0.5) Draws random boolean value. Float value (in 0-1 range) defining the chances of drawing the value of TRUE. The higher the of this argument, the higher the chances to draw TRUE.
getRandomLatitude(float $min, float $max, int $round=0) Draws random latitude from given range (default is max allowed range). If given max is lower than min, these two will be swapped.
getRandomLongitude(float $min, float $max, int $round=0) Draws random longitude from given range (default is max allowed range). If given max is lower than min, these two will be swapped.

License

  • Written and copyrighted ©2014-2022 by Marcin Orlowski
  • Open-sourced software licensed under the MIT license

marcin-orlowski/phpunit-extra-asserts 适用场景与选型建议

marcin-orlowski/phpunit-extra-asserts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.75k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 marcin-orlowski/phpunit-extra-asserts 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 17.75k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 8
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-06