feat: adds unless expectation

This commit is contained in:
Nuno Maduro
2021-09-24 22:12:08 +01:00
parent 457972716f
commit b43a59868d
3 changed files with 129 additions and 2 deletions

View File

@ -221,6 +221,23 @@ final class Expectation
return $this;
}
/**
* Apply the callback if the given "condition" is falsy.
*
* @param (callable(): bool)|bool $condition
* @param callable(Expectation<TValue>): mixed $callback
*/
public function unless($condition, callable $callback): Expectation
{
$condition = is_callable($condition)
? $condition
: static function () use ($condition): mixed {
return $condition;
};
return $this->when(!$condition(), $callback);
}
/**
* Apply the callback if the given "condition" is truthy.
*
@ -236,7 +253,7 @@ final class Expectation
};
if ($condition()) {
$callback(new self($this->value));
$callback($this->and($this->value));
}
return $this;