Skip to content

Commit

Permalink
Dumper::dumpException() added option to change output file name via A…
Browse files Browse the repository at this point in the history
…ssertException::$outputName
  • Loading branch information
dg committed Nov 22, 2021
1 parent a1bc567 commit 6b9b34f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,23 +481,29 @@ public static function matchFile(string $file, $actual, string $description = nu
throw new \Exception("Unable to read file '$file'.");

} elseif (!is_scalar($actual)) {
self::fail(self::describe('%1 should match %2', $description), $actual, $pattern);
self::fail(self::describe('%1 should match %2', $description), $actual, $pattern, null, basename($file));

} elseif (!self::isMatching($pattern, $actual)) {
if (self::$expandPatterns) {
[$pattern, $actual] = self::expandMatchingPatterns($pattern, $actual);
}
self::fail(self::describe('%1 should match %2', $description), $actual, $pattern);
self::fail(self::describe('%1 should match %2', $description), $actual, $pattern, null, basename($file));
}
}


/**
* Assertion that fails.
*/
public static function fail(string $message, $actual = null, $expected = null, \Throwable $previous = null): void
{
public static function fail(
string $message,
$actual = null,
$expected = null,
\Throwable $previous = null,
string $outputName = null
): void {
$e = new AssertException($message, $expected, $actual, $previous);
$e->outputName = $outputName;
if (self::$onFailure) {
(self::$onFailure)($e);
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/Framework/AssertException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class AssertException extends \Exception

public $expected;

public $outputName;


public function __construct(string $message, $expected, $actual, \Throwable $previous = null)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Framework/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ public static function dumpException(\Throwable $e): string
if ($e instanceof AssertException) {
$expected = $e->expected;
$actual = $e->actual;
$testFile = $e->outputName
? dirname($testFile) . '/' . $e->outputName . '.foo'
: $testFile;

if (is_object($expected) || is_array($expected) || (is_string($expected) && strlen($expected) > self::$maxLength)
|| is_object($actual) || is_array($actual) || (is_string($actual) && (strlen($actual) > self::$maxLength || preg_match('#[\x00-\x1F]#', $actual)))
Expand Down

0 comments on commit 6b9b34f

Please sign in to comment.