Skip to content

Commit e5fddb0

Browse files
committed
Provide simple PHPUnit integration
This is a simple PHPUnit integration for php-quickcheck. It consists of a custom assertion and a static factory method. To use this in a PHPUnit test, call $this->assertThat(Gen::forAll($generators, $property), QCheckSpec::check(10));
1 parent 2d74ba5 commit e5fddb0

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

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($other): string
65+
{
66+
return $other instanceof \Exception ? $other->getMessage() ."\n" : '';
67+
}
68+
69+
}

0 commit comments

Comments
 (0)