Skip to content

Commit 68bb65e

Browse files
authored
Merge pull request #15 from Vinai/phpunit
Provide simple PHPUnit constraint
2 parents 7245a39 + 7c6f1d7 commit 68bb65e

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,35 @@ This will test `my_function` using the `Generator::strings()` generator.
254254
You can also register your own Generators using the
255255
`Annotation::register($type, $generator)` method.
256256

257+
## PHPUnit
258+
259+
To use php-quickcheck with PHPUnit, the assertion `\QCheck\PHPUnit\Constraint\Prop` is provided.
260+
It provides a static constructor method `Prop::check`. Similar to `Quick::check`, the method takes the size and allows also passing options if needed.
261+
262+
```php
263+
public function testStringsAreLessThanTenChars()
264+
{
265+
$property = Gen::forAll([Gen::strings()], function ($s): bool {
266+
return 10 > strlen($s);
267+
});
268+
$this->assertThat($property, Prop::check(50)); // will fail
269+
}
270+
```
271+
The assertion will delegate to `Quick::check($size, $property)`, and if the function returns anything but `true`, it will display a formatted failure description.
272+
273+
```
274+
Failed asserting that property is true.
275+
Tests runs: 16, failing size: 15, seed: 1578486446175, smallest shrunk value(s):
276+
array (
277+
0 => <failed shrunk value>,
278+
)
279+
```
280+
281+
If an exception is thrown or a PHPUnit assertion fails, the message will be included in the output.
282+
283+
To reproduce a test result the displayed seed can be passed via `Prop::check($size, ['seed' => 1578486446175])`.
284+
285+
257286
## Project Status
258287

259288
PhpQuickCheck is highly experimental and in its very early stages. Only
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace QCheck\PHPUnit\Constraint;
4+
5+
use PHPUnit\Framework\Constraint\Constraint;
6+
use QCheck\Quick;
7+
8+
class Prop extends Constraint
9+
{
10+
/**
11+
* @var int
12+
*/
13+
private $size;
14+
15+
/**
16+
* @var array
17+
*/
18+
private $opts;
19+
20+
public function __construct(int $n, array $opts = [])
21+
{
22+
$this->size = $n;
23+
$this->opts = $opts;
24+
}
25+
26+
public static function check($n = 100, array $opts = [])
27+
{
28+
return new self($n, $opts);
29+
}
30+
31+
public function evaluate($prop, string $description = '', bool $returnResult = false)
32+
{
33+
$result = Quick::check($this->size, $prop, $this->opts);
34+
return parent::evaluate($result, $description, $returnResult);
35+
}
36+
37+
protected function matches($other): bool
38+
{
39+
return @$other['result'] === true;
40+
}
41+
42+
public function toString(): string
43+
{
44+
return 'property is true';
45+
}
46+
47+
protected function failureDescription($other): string
48+
{
49+
return $this->toString();
50+
}
51+
52+
protected function additionalFailureDescription($other): string
53+
{
54+
return sprintf(
55+
"%sTests runs: %d, failing size: %d, seed: %s, smallest shrunk value(s):\n%s",
56+
$this->extractExceptionMessage($other['result']),
57+
$other['num_tests'],
58+
$other['failing_size'],
59+
$other['seed'],
60+
var_export($other['shrunk']['smallest'], true)
61+
);
62+
}
63+
64+
private function extractExceptionMessage($result): string
65+
{
66+
return $result instanceof \Exception ? $result->getMessage() . "\n" : '';
67+
}
68+
69+
}

0 commit comments

Comments
 (0)