|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of PHPUnit. |
| 4 | + * |
| 5 | + * (c) Sebastian Bergmann <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | +namespace PHPUnit\Util; |
| 11 | + |
| 12 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 13 | +use PHPUnit\Framework\Attributes\Small; |
| 14 | +use PHPUnit\Framework\Attributes\TestDox; |
| 15 | +use PHPUnit\Framework\ExpectationFailedException; |
| 16 | +use PHPUnit\Framework\PhptAssertionFailedError; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use PHPUnit\Runner\ErrorException; |
| 19 | +use RuntimeException; |
| 20 | +use SebastianBergmann\Comparator\ComparisonFailure; |
| 21 | + |
| 22 | +#[CoversClass(ThrowableToStringMapper::class)] |
| 23 | +#[Small] |
| 24 | +#[TestDox('ThrowableToStringMapper')] |
| 25 | +final class ThrowableToStringMapperTest extends TestCase |
| 26 | +{ |
| 27 | + #[TestDox('Maps ExpectationFailedException with ComparisonFailure')] |
| 28 | + public function testMapsExpectationFailedExceptionWithComparisonFailure(): void |
| 29 | + { |
| 30 | + $comparisonFailure = new ComparisonFailure('expected', 'actual', 'expected', 'actual'); |
| 31 | + $e = new ExpectationFailedException('msg', $comparisonFailure); |
| 32 | + |
| 33 | + $mapped = ThrowableToStringMapper::map($e); |
| 34 | + |
| 35 | + $this->assertStringContainsString('msg', $mapped); |
| 36 | + $this->assertStringContainsString($comparisonFailure->getDiff(), $mapped); |
| 37 | + $this->assertStringEndsWith("\n", $mapped); |
| 38 | + } |
| 39 | + |
| 40 | + #[TestDox('Maps PhptAssertionFailedError')] |
| 41 | + public function testMapsPhptAssertionFailedError(): void |
| 42 | + { |
| 43 | + $error = new PhptAssertionFailedError('phpt-message', 0, 'file', 1, [], 'my-diff-string'); |
| 44 | + $mapped = ThrowableToStringMapper::map($error); |
| 45 | + |
| 46 | + $this->assertStringContainsString('phpt-message', $mapped); |
| 47 | + $this->assertStringContainsString('my-diff-string', $mapped); |
| 48 | + $this->assertStringEndsWith("\n", $mapped); |
| 49 | + } |
| 50 | + |
| 51 | + #[TestDox('Maps \ErrorException')] |
| 52 | + public function testMapsErrorException(): void |
| 53 | + { |
| 54 | + $e = new ErrorException('boom'); |
| 55 | + |
| 56 | + $this->assertSame('boom', ThrowableToStringMapper::map($e)); |
| 57 | + } |
| 58 | + |
| 59 | + #[TestDox('Maps \Throwable')] |
| 60 | + public function testMapsThrowable(): void |
| 61 | + { |
| 62 | + $t = new RuntimeException('oops'); |
| 63 | + $expected = RuntimeException::class . ': oops' . "\n"; |
| 64 | + |
| 65 | + $this->assertSame($expected, ThrowableToStringMapper::map($t)); |
| 66 | + } |
| 67 | +} |
0 commit comments