Skip to content

Commit

Permalink
Replace phpunit ResultPrinter with self describing Exception (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Nov 24, 2023
1 parent f7e4871 commit 936e077
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 136 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit bootstrap="vendor/autoload.php" printerClass="Atk4\Core\Phpunit\ResultPrinter" colors="true">
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
Expand Down
15 changes: 14 additions & 1 deletion src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Atk4\Core;

use Atk4\Core\ExceptionRenderer\RendererAbstract;
use Atk4\Core\Translator\ITranslatorAdapter;
use PHPUnit\Framework\SelfDescribing;

/**
* Base exception of all Agile Toolkit exceptions.
*/
class Exception extends \Exception
class Exception extends \Exception implements SelfDescribing
{
use WarnDynamicPropertyTrait;

Expand Down Expand Up @@ -55,6 +57,17 @@ public function setMessage(string $message): self
return $this;
}

public function toString(): string
{
$res = static::class . ': ' . $this->getMessage() . "\n";
foreach ($this->getParams() as $param => $value) {
$valueStr = RendererAbstract::toSafeString($value, true);
$res .= ' ' . $param . ': ' . str_replace("\n", "\n" . ' ', $valueStr) . "\n";
}

return $res;
}

/**
* Return exception message using color sequences.
*
Expand Down
65 changes: 0 additions & 65 deletions src/Phpunit/ResultPrinter.php

This file was deleted.

22 changes: 22 additions & 0 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ public function testSolution2(): void
self::assertMatchesRegularExpression('~2nd Solution~', $ret);
}

public function testPhpunitSelfDescribing(): void
{
$m = (new Exception('My exception', 0))
->addMoreInfo('x', 'foo')
->addMoreInfo('y', ['bar' => 2.4, [], [[1]]]);

self::assertSame(
<<<'EOF'
Atk4\Core\Exception: My exception
x: 'foo'
y: [
'bar': 2.4,
0: [],
1: [
...
]
]
EOF . "\n", // NL in the string is not parsed by Netbeans, see https://github.com/apache/netbeans/issues/4345
$m->toString()
);
}
public function testExceptionFallback(): void
{
$m = new ExceptionTestThrowError('test', 2);
Expand Down
69 changes: 0 additions & 69 deletions tests/Phpunit/ResultPrinterTest.php

This file was deleted.

0 comments on commit 936e077

Please sign in to comment.