This repository has been archived by the owner on Sep 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(doctest) Beautiful atoum error messages.
Error message no longer contains the stack trace, or long file names. They are not needed, and introduce a lot of noise when debugging. Stack trace have been removed. File names have been reduced to the basename only.
- Loading branch information
Showing
2 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kitab\DocTest\Report\Cli\Fields; | ||
|
||
use mageekguy\atoum\report\fields; | ||
|
||
class Errors extends fields\runner\errors\cli | ||
{ | ||
public function __toString() | ||
{ | ||
$string = ''; | ||
|
||
if (null !== $this->runner) { | ||
$errors = $this->runner->getScore()->getErrors(); | ||
|
||
$sizeOfErrors = count($errors); | ||
|
||
if ($sizeOfErrors > 0) { | ||
$string .= | ||
$this->titlePrompt . | ||
sprintf( | ||
$this->locale->_('%s:'), | ||
$this->titleColorizer->colorize(sprintf($this->locale->__('There is %d error', 'There are %d errors', $sizeOfErrors), $sizeOfErrors)) | ||
) . | ||
"\n" | ||
; | ||
|
||
$class = null; | ||
$method = null; | ||
|
||
foreach ($errors as $error) { | ||
$error['oldFile'] = $error['file']; | ||
$error['file'] = basename($error['file']); | ||
|
||
if ($error['class'] !== $class || $error['method'] !== $method) { | ||
$string .= | ||
$this->methodPrompt . | ||
sprintf( | ||
$this->locale->_('%s:'), | ||
$this->methodColorizer->colorize($error['class'] . '::' . $error['method'] . '()') | ||
) . | ||
"\n" | ||
; | ||
|
||
$class = $error['class']; | ||
$method = $error['method']; | ||
} | ||
|
||
$string .= $this->errorPrompt; | ||
|
||
$type = static::getType($error['type']); | ||
$case = $error['case'] === null ? '' : sprintf(' in case \'%s\'', $error['case']); | ||
|
||
switch (true) { | ||
case $error['file'] === null: | ||
switch (true) { | ||
case $error['errorFile'] === null: | ||
$errorMessage = $this->locale->_('Error %s in unknown file on unknown line%s, generated by unknown file', $type, $case); | ||
break; | ||
|
||
case $error['errorLine'] === null: | ||
$errorMessage = $this->locale->_('Error %s in unknown file on unknown line, generated by file %s%s', $type, $error['errorFile'], $case); | ||
break; | ||
|
||
case $error['errorLine'] !== null: | ||
$errorMessage = $this->locale->_('Error %s in unknown file on unknown line, generated by file %s on line %d%s', $type, $error['errorFile'], $error['errorLine'], $case); | ||
break; | ||
} | ||
break; | ||
|
||
case $error['line'] === null: | ||
switch (true) { | ||
case $error['errorFile'] === null: | ||
$errorMessage = $this->locale->_('Error %s in %s on unknown line, generated by unknown file%s', $type, $error['file'], $case); | ||
break; | ||
|
||
case $error['errorLine'] === null: | ||
$errorMessage = $this->locale->_('Error %s in %s on unknown line, generated by file %s%s', $type, $error['file'], $error['errorFile'], $case); | ||
break; | ||
|
||
case $error['errorLine'] !== null: | ||
$errorMessage = $this->locale->_('Error %s in %s on unknown line, generated by file %s on line %d%s', $type, $error['file'], $error['errorFile'], $error['errorLine'], $case); | ||
break; | ||
} | ||
break; | ||
|
||
default: | ||
switch (true) { | ||
case $error['errorFile'] === null: | ||
$errorMessage = $this->locale->_('Error %s in %s on line %d, generated by unknown file%s', $type, $error['file'], $error['line'], $case); | ||
break; | ||
|
||
case $error['errorLine'] === null: | ||
$errorMessage = $this->locale->_('Error %s in %s on line %d, generated by file %s%s', $type, $error['file'], $error['line'], $error['errorFile'], $case); | ||
break; | ||
|
||
case $error['errorLine'] !== null: | ||
$errorMessage = $this->locale->_('Error %s in %s on line %d, generated by file %s on line %d%s', $type, $error['file'], $error['line'], $error['errorFile'], $error['errorLine'], $case); | ||
break; | ||
} | ||
break; | ||
} | ||
|
||
$string .= sprintf( | ||
$this->locale->_('%s:'), | ||
$this->errorColorizer->colorize(($errorMessage)) | ||
) . | ||
"\n" | ||
; | ||
|
||
$error['message'] = preg_replace( | ||
[ | ||
'/Stack trace:.*/s', | ||
'/' . preg_quote($error['oldFile'], '/') . '/' | ||
], | ||
[ | ||
'', | ||
basename($error['oldFile']) | ||
], | ||
$error['message'] | ||
); | ||
|
||
$string .= $error['message']; | ||
} | ||
} | ||
} | ||
|
||
return $string; | ||
} | ||
} |