|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of PHPUnit. |
| 4 | + * |
| 5 | + * (c) Sebastian Bergmann <sebastian@phpunit.de> |
| 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\Event\Code; |
| 11 | + |
| 12 | +use Exception; |
| 13 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\Attributes\Small; |
| 16 | +use PHPUnit\Framework\Attributes\TestDox; |
| 17 | +use PHPUnit\Framework\ExpectationFailedException; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use SebastianBergmann\Comparator\ComparisonFailure; |
| 20 | +use stdClass; |
| 21 | + |
| 22 | +#[CoversClass(ComparisonFailureBuilder::class)] |
| 23 | +#[Small] |
| 24 | +final class ComparisonFailureBuilderTest extends TestCase |
| 25 | +{ |
| 26 | + public static function provider(): array |
| 27 | + { |
| 28 | + return [ |
| 29 | + 'type of expected value: string, string representation of expected value: not available, type of actual value: string, string representation of actual value: not available' => [ |
| 30 | + 'expected', |
| 31 | + 'actual', |
| 32 | + '', |
| 33 | + new ExpectationFailedException( |
| 34 | + 'message', |
| 35 | + new ComparisonFailure( |
| 36 | + 'expected', |
| 37 | + 'actual', |
| 38 | + '', |
| 39 | + '', |
| 40 | + 'message', |
| 41 | + ), |
| 42 | + ), |
| 43 | + ], |
| 44 | + |
| 45 | + 'type of expected value: true, string representation of expected value: not available, type of actual value: false, string representation of actual value: not available' => [ |
| 46 | + 'true', |
| 47 | + 'false', |
| 48 | + '', |
| 49 | + new ExpectationFailedException( |
| 50 | + 'message', |
| 51 | + new ComparisonFailure( |
| 52 | + true, |
| 53 | + false, |
| 54 | + '', |
| 55 | + '', |
| 56 | + 'message', |
| 57 | + ), |
| 58 | + ), |
| 59 | + ], |
| 60 | + |
| 61 | + 'type of expected value: null, string representation of expected value: not available, type of actual value: null, string representation of actual value: not available' => [ |
| 62 | + 'null', |
| 63 | + 'null', |
| 64 | + '', |
| 65 | + new ExpectationFailedException( |
| 66 | + 'message', |
| 67 | + new ComparisonFailure( |
| 68 | + null, |
| 69 | + null, |
| 70 | + '', |
| 71 | + '', |
| 72 | + 'message', |
| 73 | + ), |
| 74 | + ), |
| 75 | + ], |
| 76 | + |
| 77 | + 'type of expected value: array, string representation of expected value: not available, type of actual value: object, string representation of actual value: not available' => [ |
| 78 | + '', |
| 79 | + '', |
| 80 | + '', |
| 81 | + new ExpectationFailedException( |
| 82 | + 'message', |
| 83 | + new ComparisonFailure( |
| 84 | + [], |
| 85 | + new stdClass, |
| 86 | + '', |
| 87 | + '', |
| 88 | + 'message', |
| 89 | + ), |
| 90 | + ), |
| 91 | + ], |
| 92 | + |
| 93 | + 'type of expected value: string, string representation of expected value: available, type of actual value: string, string representation of actual value: available' => [ |
| 94 | + 'expected-string', |
| 95 | + 'actual-string', |
| 96 | + <<<'EOT' |
| 97 | +
|
| 98 | +--- Expected |
| 99 | ++++ Actual |
| 100 | +@@ @@ |
| 101 | +-expected-string |
| 102 | ++actual-string |
| 103 | + |
| 104 | +EOT, |
| 105 | + new ExpectationFailedException( |
| 106 | + 'message', |
| 107 | + new ComparisonFailure( |
| 108 | + 'expected', |
| 109 | + 'actual', |
| 110 | + 'expected-string', |
| 111 | + 'actual-string', |
| 112 | + 'message', |
| 113 | + ), |
| 114 | + ), |
| 115 | + ], |
| 116 | + ]; |
| 117 | + } |
| 118 | + |
| 119 | + #[TestDox('Maps exception that is not of type ExpectationFailedException to null')] |
| 120 | + public function testMapsGenericThrowableToNull(): void |
| 121 | + { |
| 122 | + $this->assertNull( |
| 123 | + ComparisonFailureBuilder::from(new Exception), |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + #[TestDox('Maps ExpectationFailedException that does not aggregate a ComparisonFailure object to null')] |
| 128 | + public function testMapsExpectationFailedExceptionWithoutComparisonFailureToNull(): void |
| 129 | + { |
| 130 | + $this->assertNull( |
| 131 | + ComparisonFailureBuilder::from( |
| 132 | + new ExpectationFailedException('message'), |
| 133 | + ), |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + #[DataProvider('provider')] |
| 138 | + #[TestDox('Maps ExpectationFailedException that aggregates a ComparisonFailure object to value object')] |
| 139 | + public function testMapsExpectationFailedExceptionWithComparisonFailureToValueObject(string $expected, string $actual, string $diff, ExpectationFailedException $exception): void |
| 140 | + { |
| 141 | + $comparisonFailure = ComparisonFailureBuilder::from($exception); |
| 142 | + |
| 143 | + $this->assertSame($expected, $comparisonFailure->expected()); |
| 144 | + $this->assertSame($actual, $comparisonFailure->actual()); |
| 145 | + $this->assertSame($diff, $comparisonFailure->diff()); |
| 146 | + } |
| 147 | +} |
0 commit comments