| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Tests\Logger\Monolog; |
| 22 |
|
| 23 |
use AssertionError; |
| 24 |
use InvalidArgumentException; |
| 25 |
use LengthException; |
| 26 |
use LogicException; |
| 27 |
use MediaWiki\Logger\Monolog\LineFormatter; |
| 28 |
use Wikimedia\TestingAccessWrapper; |
| 29 |
|
| 30 |
class LineFormatterTest extends \MediaWikiUnitTestCase { |
| 31 |
|
| 32 |
protected function setUp(): void { |
| 33 |
parent::setUp(); |
| 34 |
if ( !class_exists( \Monolog\Formatter\LineFormatter::class ) ) { |
| 35 |
$this->markTestSkipped( 'This test requires monolog to be installed' ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public function testNormalizeExceptionNoTrace() { |
| 43 |
$fixture = new LineFormatter(); |
| 44 |
$fixture->includeStacktraces( false ); |
| 45 |
$fixture = TestingAccessWrapper::newFromObject( $fixture ); |
| 46 |
$boom = new InvalidArgumentException( 'boom', 0, |
| 47 |
new LengthException( 'too long', 0, |
| 48 |
new LogicException( 'Spock wuz here' ) |
| 49 |
) |
| 50 |
); |
| 51 |
$out = $fixture->normalizeException( $boom ); |
| 52 |
$this->assertStringContainsString( "\n[Exception InvalidArgumentException]", $out ); |
| 53 |
$this->assertStringContainsString( "\nCaused by: [Exception LengthException]", $out ); |
| 54 |
$this->assertStringContainsString( "\nCaused by: [Exception LogicException]", $out ); |
| 55 |
$this->assertStringNotContainsString( "\n #0", $out ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function testNormalizeExceptionTrace() { |
| 62 |
$fixture = new LineFormatter(); |
| 63 |
$fixture->includeStacktraces( true ); |
| 64 |
$fixture = TestingAccessWrapper::newFromObject( $fixture ); |
| 65 |
$boom = new InvalidArgumentException( 'boom', 0, |
| 66 |
new LengthException( 'too long', 0, |
| 67 |
new LogicException( 'Spock wuz here' ) |
| 68 |
) |
| 69 |
); |
| 70 |
$out = $fixture->normalizeException( $boom ); |
| 71 |
$this->assertStringContainsString( "\n[Exception InvalidArgumentException]", $out ); |
| 72 |
$this->assertStringContainsString( "\nCaused by: [Exception LengthException]", $out ); |
| 73 |
$this->assertStringContainsString( "\nCaused by: [Exception LogicException]", $out ); |
| 74 |
$this->assertStringContainsString( "\n #0", $out ); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function testNormalizeExceptionErrorNoTrace() { |
| 81 |
if ( !class_exists( AssertionError::class ) ) { |
| 82 |
$this->markTestSkipped( 'AssertionError class does not exist' ); |
| 83 |
} |
| 84 |
|
| 85 |
$fixture = new LineFormatter(); |
| 86 |
$fixture->includeStacktraces( false ); |
| 87 |
$fixture = TestingAccessWrapper::newFromObject( $fixture ); |
| 88 |
$boom = new InvalidArgumentException( 'boom', 0, |
| 89 |
new LengthException( 'too long', 0, |
| 90 |
new AssertionError( 'Spock wuz here' ) |
| 91 |
) |
| 92 |
); |
| 93 |
$out = $fixture->normalizeException( $boom ); |
| 94 |
$this->assertStringContainsString( "\n[Exception InvalidArgumentException]", $out ); |
| 95 |
$this->assertStringContainsString( "\nCaused by: [Exception LengthException]", $out ); |
| 96 |
$this->assertStringContainsString( "\nCaused by: [Error AssertionError]", $out ); |
| 97 |
$this->assertStringNotContainsString( "\n #0", $out ); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function testNormalizeExceptionErrorTrace() { |
| 104 |
if ( !class_exists( AssertionError::class ) ) { |
| 105 |
$this->markTestSkipped( 'AssertionError class does not exist' ); |
| 106 |
} |
| 107 |
|
| 108 |
$fixture = new LineFormatter(); |
| 109 |
$fixture->includeStacktraces( true ); |
| 110 |
$fixture = TestingAccessWrapper::newFromObject( $fixture ); |
| 111 |
$boom = new InvalidArgumentException( 'boom', 0, |
| 112 |
new LengthException( 'too long', 0, |
| 113 |
new AssertionError( 'Spock wuz here' ) |
| 114 |
) |
| 115 |
); |
| 116 |
$out = $fixture->normalizeException( $boom ); |
| 117 |
$this->assertStringContainsString( "\n[Exception InvalidArgumentException]", $out ); |
| 118 |
$this->assertStringContainsString( "\nCaused by: [Exception LengthException]", $out ); |
| 119 |
$this->assertStringContainsString( "\nCaused by: [Error AssertionError]", $out ); |
| 120 |
$this->assertStringContainsString( "\n #0", $out ); |
| 121 |
} |
| 122 |
} |