PHPackages                             datashaman/phpcheck - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. datashaman/phpcheck

ActiveLibrary[Testing &amp; Quality](/categories/testing)

datashaman/phpcheck
===================

PHP implementation of Haskell's QuickCheck.

1.0.2(7y ago)2231[4 issues](https://github.com/datashaman/phpcheck/issues)MITPHPPHP ^7.2

Since Apr 15Pushed 7y agoCompare

[ Source](https://github.com/datashaman/phpcheck)[ Packagist](https://packagist.org/packages/datashaman/phpcheck)[ RSS](/packages/datashaman-phpcheck/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (7)Dependencies (23)Versions (9)Used By (0)

phpcheck
========

[](#phpcheck)

[![Development Status](https://camo.githubusercontent.com/f52c20e80fbfb19d59c6f71808f3f246b77d10af20f2b41760fdc094a5ab72bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617475732d616c7068612d7265642e737667)](https://camo.githubusercontent.com/f52c20e80fbfb19d59c6f71808f3f246b77d10af20f2b41760fdc094a5ab72bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617475732d616c7068612d7265642e737667)[![Build Status](https://camo.githubusercontent.com/29b338c10b8d21e88c87e7235b9c6ff113ef647ce3d2dbb961a872207ae718b7/68747470733a2f2f7472617669732d63692e6f72672f646174617368616d616e2f706870636865636b2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/datashaman/phpcheck)

PHP implementation of Haskell's QuickCheck.

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

Table of Contents
=================

[](#table-of-contents)

- [phpcheck](#phpcheck)
    - [installation](#installation)
    - [type declarations](#type-declarations)
    - [annotations](#annotations)
        - [tabulate](#tabulate)
    - [generators](#generators)
    - [assertions](#assertions)
    - [examples](#examples)
    - [command line arguments](#command-line-arguments)
    - [storage of results](#storage-of-results)
    - [todo](#todo)

installation
------------

[](#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
-----------------

[](#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
-----------

[](#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

[](#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
----------

[](#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
