From 847b06e5584f1fe0f4031ec334d0d9a586bc4c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20A=C5=9Fan?= Date: Thu, 23 Sep 2021 03:35:41 +0300 Subject: [PATCH] add `when()` method --- src/Expectation.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Expectation.php b/src/Expectation.php index 253b1e1e..7a1e6057 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -177,6 +177,26 @@ final class Expectation return $this; } + /** + * It skips the tests in the callback if the condition is not truthy. + * + * @param Closure|bool|string $condition + */ + public function when($condition, callable $callback): Expectation + { + $condition = is_callable($condition) + ? $condition + : function () use ($condition) { + return $condition; + }; + + if ($condition()) { + $callback(new self($this->value)); + } + + return $this; + } + /** * Asserts that two variables have the same type and * value. Used on objects, it asserts that two