定制 datashaman/phpcheck 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

datashaman/phpcheck

Composer 安装命令:

composer require --dev datashaman/phpcheck

包简介

PHP implementation of Haskell's QuickCheck.

关键字:

README 文档

README

Development Status Build Status

PHP implementation of Haskell's QuickCheck.

NB This is ALPHA status. Do not use it yet.

Table of Contents

installation

Install the composer package into your project. You will require PHP7.2 or higher:

composer require --dev datashaman/phpcheck

Alternately, you can install the composer package globally:

composer global require datashaman/phpcheck

type declarations

PHPCheck will automatically generate arguments for check methods based on type declarations.

For finer-grained control over the arguments, use annotations on the method parameters.

annotations

Annotate your check method parameters to control the arguments provided to the method.

Parameter tags (use them in the description of a parameter, usually the end):

  • {@gen method()} or {@gen method(1, 10)} where method is the name of a generator below.

Method tags:

  • @maxSuccess sets the number of successful checks for a successful result. The default is 100.
  • @tabulate and @coverTable (dicussed below).

tabulate

If you decorate a check method with tabulate, information about test case distribution is collected into a table.

The arguments to tabulate are the table's name and a list of values associated with the current check. An example:

/**
 * @tabulate "Values" [$value]
 */
public function checkBooleans(bool $value)
{
    return true;
}

If you run this check, everything passes and a table is output at the end of the check run:

Tables

1) Datashaman\PHPCheck\Checks\GeneratorCheck::checkBooleans

Values (100 total)

   52% true
   48% false

We'd like to check that the coverage is correct for the generator, so we use a @coverTable method annotation:

/**
 * @coverTable "Values" [[true, 49], [false, 49]]
 * @tabulate "Values" [$value]
 */
public function checkBooleans(bool $value)
{
    return true;
}

The arguments to the annotation are the name of the table, and a list of key value pairs where the value is the expected percentage distribution.

Here's a sample output from the above:

1) Datashaman\PHPCheck\Checks\GeneratorCheck::checkBooleans

Values (100 total)

54% true
46% false

Table 'Values' had only 46.0% false, but expected 49.0%

We are now warned when the distribution does not fall within the accepted percentage of generated values.

In this case, we would benefit from running the checks a lot more times so we approach the expected 50/50 average for a boolean:

/**
 * @coverTable "Values" [[true, 49], [false, 49]]
 * @maxSuccess 10000
 * @tabulate "Values" [$value]
 */
public function checkBooleans(bool $value)
{
    return true;
}

Now with 10000 successful iterations, the warning disappears from the output and the percentage is within the acceptable 1% margin of error:

1) Datashaman\PHPCheck\Checks\GeneratorCheck::checkBooleans

Values (10000 total)

    50.5% true
    49.5% false

generators

Below is the list of generators that are currently available:

  • arguments(callable $callable)
  • arrays()
  • ascii()
  • booleans(int $chanceOfGettingTrue = 50)
  • characters($minChar = null, $maxChar = null)
  • choose($min = PHP_INT_MIN, $max = PHP_INT_MAX)
  • chooseAny(string $type)
  • datetimes($min = null, $max = null, Generator $timezones = null)
  • dates($min = null, $max = null)
  • elements(array $array)
  • faker(...$args)
  • floats(float $min, float $max)
  • frequency(array $frequencies)
  • growingElements(array $array)
  • integers(int $min = PHP_INT_MIN, int $max = PHP_INT_MAX)
  • intervals(array $include = [[PHP_INT_MIN, PHP_INT_MAX]], array $exclude=[])
  • listOf(Generator $gen)
  • listOf1(Generator $gen)
  • mixed()
  • oneof(array $gens)
  • resize(int $n, Generator $gen)
  • scale(callable $f, Generator $gen)
  • strings(Generator $characters = null)
  • suchThat(Generator $gen, callable $f)
  • suchThatMap(Generator $gen, callable $f)
  • suchThatMaybe(Generator $gen, callable $f)
  • timezones()
  • variant(int $seed, Generator $gen)
  • vectorOf(int $n, Generator $gen)

To generate a value from one of the above, use generate:

>>> use function Datashaman\PHPCheck\{
...     choose,
...     generate
... };
>>>
>>> var_dump(generate(choose(0, 10)));
int(2)

To generate a sample of values with increasing size, use sample:

>>> use function Datashaman\PHPCheck\{
...     ascii,
...     strings,
...     sample
... };
>>>
>>> var_dump(sample(strings(ascii())));
array(11) {
[0]=>
string(0) ""
[1]=>
string(2) "0]"
[2]=>
string(2) "^|"
[3]=>
string(3) "P@N"
[4]=>
string(5) "G1KPu"
[5]=>
string(5) "q-e1y"
[6]=>
string(4) "NcdL"
[7]=>
string(7) "hS:{_>@"
[8]=>
string(10) "wjv1X"Zm$V"
[9]=>
string(16) "aX-6*s0-WX>#cf~T"
[10]=>
string(12) ";g<&8*b&Q0=)"
}
=> null

See GeneratorCheck and GeneratorTest for examples of how these are used.

The characters generator accepts either characters or integer codepoints for minChar and maxChar. Characters are generated from the complete range of Unicode characters excluding control characters, private ranges and surrogates.

The faker generator takes a variable number of arguments. If you supply one argument, it's assumed to be a property on the Faker generator. If you supply more than one argument, the first argument is the method on the Faker generator and the rest are sent as parameters to that method.

This opens up a lot of domain-specific generators. See Faker for more details.

check methods

Check methods must return a bool indicating success or failure.

exceptions

We have chosen to use Nuno Maduro's Collision for reporting exceptions to the console.

This should be switchable soon.

examples

There is an example check implemented in the examples folder. To run it:

phpcheck examples

The Generator checks for this package are a great illustration of the use of the generators.

command line arguments

The phpcheck program accept a number of arguments and options:

Description:
    Runs checks.

Usage:
    phpcheck [options] [--] [<path>]

Arguments:
    path                           File or folder with checks [default: "checks"]

Options:
        --bootstrap[=BOOTSTRAP]         A PHP script that is included before the checks run
        --coverage-html[=COVERAGE-HTML] Generate HTML code coverage report [default: false]
        --coverage-text[=COVERAGE-TEXT] Generate text code coverage report [default: false]
    -f, --filter[=FILTER]               Filter the checks that will be run
    -j, --log-junit[=LOG-JUNIT]         Log check execution to JUnit XML file [default: false]
    -t, --log-text[=LOG-TEXT]           Log check execution to text file [default: false]
    --max-success=MAX-SUCCESS           Maximum number of successful checks before succeeding. Testing stops at the first failure.
                                        If all tests are passing and you want to run more tests, increase this number. [default: 100]
    -d, --no-defects[=NO-DEFECTS]       Ignore previous defects [default: false]
    -h, --help                          Display this help message
    -q, --quiet                         Do not output any message
    -s, --seed[=SEED]                   Seed the random number generator to get repeatable runs
    -V, --version                       Display this application version
    --ansi                              Force ANSI output
    --no-ansi                           Disable ANSI output
    -n, --no-interaction                Do not ask any interactive question
    -v|vv|vvv, --verbose                Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

The --bootstrap parameter can be included in a phpcheck.xml or phpcheck.xml.dist file. See ours for an example.

The --filter or -f parameter is a filename-style match as follows:

ClassName::
ClassName::MethodName
MethodName

where ClassName and MethodName can include patterns using * and ? as you'd expect.

The console reporter outputs check results much like PHPUnit:

PHPCheck 0.1.0 by Marlin Forbes and contributors.

.............

13 / 13 (100%)

Time: 284 ms, Memory: 6.00 MB

OK (Checks: 13, Iterations: 120006, Failures: 0, Errors: 0)

Using ---verbose 3 or -vvv enables a list of the checks as they are run:

PHPCheck 0.1.0 by Marlin Forbes and contributors.

Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkCharacters' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkCharacters' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkStrings' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkStrings' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkAscii' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkAscii' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkBooleans' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkBooleans' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkBooleansWithPercentage' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkBooleansWithPercentage' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkCharactersWithNumbers' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkCharactersWithNumbers' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkCharactersWithStrings' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkCharactersWithStrings' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkChoose' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkChoose' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkIterations' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkIterations' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkFloats' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkFloats' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkFloatsWithDecimalGen' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkFloatsWithDecimalGen' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkStringsWithMinMax' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkStringsWithMinMax' ended
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkListOfInts' started
Check 'Datashaman\PHPCheck\Checks\GeneratorCheck::checkListOfInts' ended

Time: 305 ms, Memory: 6.00 MB

OK (Checks: 13, Iterations: 120006, Failures: 0, Errors: 0)

storage of results

PHPCheck stores results of check execution in the .phpcheck folder of the project.

You should add the folder to your .gitignore file.

When PHPCheck finds an error or failure, it will retry the defective arguments first before going onto regular iterations with new arguments.

If you wish to ignore the previous defects and run through new iterations only, use --no-defects or -d.

todo

All todo items have been captured as issues.

datashaman/phpcheck 适用场景与选型建议

datashaman/phpcheck 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 datashaman/phpcheck 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-15