feat: adds ddWhen and ddUnless

This commit is contained in:
Nuno Maduro
2023-08-09 12:14:32 +01:00
parent 02f72aabb2
commit 306b7eb2a6

View File

@ -130,27 +130,35 @@ final class Expectation
/** /**
* Dump the expectation value when the result of the condition is truthy. * Dump the expectation value when the result of the condition is truthy.
* *
* @param bool $boolean * @param (\Closure(TValue): bool)|bool $condition
* @return never * @return self<TValue>
*/ */
public function ddWhen($boolean, mixed ...$arguments): void public function ddWhen(Closure|bool $condition, mixed ...$arguments): Expectation
{ {
if (! $boolean) { $condition = $condition instanceof Closure ? $condition($this->value) : $condition;
return;
if (! $condition) {
return $this;
} }
$this->dd($this->value, ...$arguments); $this->dd(...$arguments);
} }
/** /**
* Dump the expectation value when the result of the condition is falsy. * Dump the expectation value when the result of the condition is falsy.
* *
* @param bool $boolean * @param (\Closure(TValue): bool)|bool $condition
* @return never * @return self<TValue>
*/ */
public function ddUnless($boolean, mixed ...$arguments): void public function ddUnless(Closure|bool $condition, mixed ...$arguments): Expectation
{ {
$this->ddWhen(! $boolean, ...$arguments); $condition = $condition instanceof Closure ? $condition($this->value) : $condition;
if ($condition) {
return $this;
}
$this->dd(...$arguments);
} }
/** /**