Skip to content

Commit

Permalink
Merge pull request #1923 from hydephp/update-testing-helpers
Browse files Browse the repository at this point in the history
Internal: Add testing helper to export and die
  • Loading branch information
caendesilva authored Jul 30, 2024
2 parents bc39c12 + 7a00e8b commit f68c5ef
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/testing/src/FluentTestingHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,39 @@ protected function assertAllSame(...$vars): void
$this->assertSame($first, $var);
}
}

/**
* @experimental Helper to print and die.
*/
protected function dd($var): void
{
if (is_string($var)) {
echo "```\n";
echo $var.($var[-1] === "\n" ? '' : "\n");
echo "```\n";
} elseif (is_array($var)) {
echo "```php\n";
echo $this->formatArray($var);
echo "```\n";
} else {
dd($var);
}

exit;
}

/**
* @experimental Helper function to format an array as a plain PHP array with [] syntax.
*/
private function formatArray(array $array): string
{
$json = json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

// Transform JSON to PHP array syntax
$php = preg_replace('/^(\s*)\"(\w+)\":/m', '$1$2:', $json); // Remove quotes from keys
$php = str_replace('{', '[', $php); // Replace { with [
$php = str_replace('}', ']', $php); // Replace } with ]

return $php."\n";
}
}

0 comments on commit f68c5ef

Please sign in to comment.