|
| 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