Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
22.22% |
4 / 18 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| IntegrationTestLogger | |
22.22% |
4 / 18 |
|
50.00% |
2 / 4 |
30.05 | |
0.00% |
0 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| failTestShowError | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| resetLoggedException | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLoggedException | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| logException | |
25.00% |
2 / 8 |
|
0.00% |
0 / 1 |
6.80 | |||
| 1 | <?php |
| 2 | namespace Apie\Common; |
| 3 | |
| 4 | use PHPUnit\Framework\TestCase; |
| 5 | use Throwable; |
| 6 | |
| 7 | final class IntegrationTestLogger |
| 8 | { |
| 9 | private static ?Throwable $loggedException = null; |
| 10 | /** |
| 11 | * @codeCoverageIgnore |
| 12 | */ |
| 13 | private function __construct() |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | public static function failTestShowError(int $statusCode = 500): never |
| 18 | { |
| 19 | TestCase::fail( |
| 20 | sprintf( |
| 21 | 'Failed request, got status %s, logged exception: %s' . PHP_EOL . '%s', |
| 22 | $statusCode, |
| 23 | self::$loggedException?->getMessage(), |
| 24 | self::$loggedException?->getTraceAsString() |
| 25 | ) |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | public static function resetLoggedException(): void |
| 30 | { |
| 31 | self::$loggedException = null; |
| 32 | } |
| 33 | |
| 34 | public static function getLoggedException(): ?Throwable |
| 35 | { |
| 36 | return self::$loggedException; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Logs exceptions for integration tests purposes. |
| 41 | */ |
| 42 | public static function logException(Throwable $error): void |
| 43 | { |
| 44 | self::$loggedException = $error; |
| 45 | if (getenv('PHPUNIT_LOG_INTEGRATION_OUTPUT')) { |
| 46 | while ($error) { |
| 47 | $stdErr = @fopen('php://stderr', 'w'); |
| 48 | fwrite($stdErr, get_class($error) . ': ' . $error->getMessage() . PHP_EOL); |
| 49 | fwrite($stdErr, $error->getTraceAsString() . PHP_EOL); |
| 50 | fclose($stdErr); |
| 51 | $error = $error->getPrevious(); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |