Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,19 @@ function with($value, ?callable $callback = null)
return is_null($callback) ? $value : $callback($value);
}
}

if (!function_exists('dd_when')) {
/**
* Dump and die when a condition is met.
*
* @param bool $condition
* @param mixed ...$vars
* @return void
*/
function dd_when(bool $condition, mixed ...$vars): void
{
if ($condition) {
dd(...$vars);
}
}
}
24 changes: 24 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,30 @@ public function testPregReplaceArray($pattern, $replacements, $subject, $expecte
preg_replace_array($pattern, $replacements, $subject)
);
}

public function testDdWhenDoesNotDumpWhenConditionIsFalse()
{
// Ensure that when the condition is false, dd() does not execute.
try {
dd_when(false, 'This should not dump');
} catch (\Exception $e) {
$this->fail('dd_when should not have called dd when condition is false.');
}

$this->assertTrue(true); // If no exception, the test passes.
}

public function testDdWhenDumpsWhenConditionIsTrue()
{
try {
dd_when(true, 'This should not dump');
} catch (\Exception $e) {
$this->assertTrue(true); // If an exception is thrown, the test passes.
return;
}

$this->fail('dd_when should have called dd when condition is true.');
}
}

trait SupportTestTraitOne
Expand Down
Loading