Skip to content

Commit 6b9b34f

Browse files
committed
Dumper::dumpException() added option to change output file name via AssertException::$outputName
1 parent a1bc567 commit 6b9b34f

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/Framework/Assert.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,23 +481,29 @@ public static function matchFile(string $file, $actual, string $description = nu
481481
throw new \Exception("Unable to read file '$file'.");
482482

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

486486
} elseif (!self::isMatching($pattern, $actual)) {
487487
if (self::$expandPatterns) {
488488
[$pattern, $actual] = self::expandMatchingPatterns($pattern, $actual);
489489
}
490-
self::fail(self::describe('%1 should match %2', $description), $actual, $pattern);
490+
self::fail(self::describe('%1 should match %2', $description), $actual, $pattern, null, basename($file));
491491
}
492492
}
493493

494494

495495
/**
496496
* Assertion that fails.
497497
*/
498-
public static function fail(string $message, $actual = null, $expected = null, \Throwable $previous = null): void
499-
{
498+
public static function fail(
499+
string $message,
500+
$actual = null,
501+
$expected = null,
502+
\Throwable $previous = null,
503+
string $outputName = null
504+
): void {
500505
$e = new AssertException($message, $expected, $actual, $previous);
506+
$e->outputName = $outputName;
501507
if (self::$onFailure) {
502508
(self::$onFailure)($e);
503509
} else {

src/Framework/AssertException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class AssertException extends \Exception
2121

2222
public $expected;
2323

24+
public $outputName;
25+
2426

2527
public function __construct(string $message, $expected, $actual, \Throwable $previous = null)
2628
{

src/Framework/Dumper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ public static function dumpException(\Throwable $e): string
305305
if ($e instanceof AssertException) {
306306
$expected = $e->expected;
307307
$actual = $e->actual;
308+
$testFile = $e->outputName
309+
? dirname($testFile) . '/' . $e->outputName . '.foo'
310+
: $testFile;
308311

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

0 commit comments

Comments
 (0)