PhpInvariant is a property-based testing framework for php
It runs your code on customized random data and checks predefined invariants (similar to QuickCheck)
The recommended way to install PhpInvariant is through Composer
composer require --dev sergey-bel/phpinvariant
- Create a folder
invariants
- Create
...Invariant
class inside this folder- Class must extends
BaseInvariant
- Each public method with a name 'check...' will be launched
- Class must extends
- Run command
vendor/bin/phpinvariant run --path=invariants
See examples
class IntegerInvariant extends BaseInvariant
{
// run check method 10 times
#[FinishRuns(10)]
public function checkInteger()
{
// $x is a random integer in [50, 100]
$x = $this->provider->integer(50, 100)->get();
$this->assertTrue(is_integer($x));
// fail when $x=100
$this->assertLessOrEqual($x, 99);
$this->assertGreaterOrEqual($x, 50);
}
}
Provider is a main class to generate random data
This example will generate a alphabetic string with length between 5 and 10 (inclusive)
$this->provider
->string(5, 10)
->alphabetic()
->get();
Finish conditions are used to determine when to end the check execution
Finish condition is specified by method attribute
--path
Specifies directory with Check classes
--config
Specifies the path to a configuration file
--no-progress
Do not show progress bar
--quiet
Do not output any message
--seed
Specifies random seed
PhpInvariant uses YAML configuration format. All command line options are supported in configuration file parameters
section
Example:
parameters:
path: invariants
no-progress: false
A config file can be passed in --config
option:
vendor/bin/phpinvariant run --config=phpinvariant.yml
git clone https://github.com/SergeyBel/phpinvariant.git
docker-compose up -d
Use Makefile commands:
fix
- run code style fixer
static
- run static analyzer
test
- run tests