Move trivially compatible tests to the unit tests suite
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / debug / logger / monolog / LineFormatterTest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Logger\Monolog;
22
23 use AssertionError;
24 use InvalidArgumentException;
25 use LengthException;
26 use LogicException;
27 use Wikimedia\TestingAccessWrapper;
28
29 class LineFormatterTest extends \MediaWikiUnitTestCase {
30
31 protected function setUp() {
32 if ( !class_exists( 'Monolog\Formatter\LineFormatter' ) ) {
33 $this->markTestSkipped( 'This test requires monolog to be installed' );
34 }
35 parent::setUp();
36 }
37
38 /**
39 * @covers MediaWiki\Logger\Monolog\LineFormatter::normalizeException
40 */
41 public function testNormalizeExceptionNoTrace() {
42 $fixture = new LineFormatter();
43 $fixture->includeStacktraces( false );
44 $fixture = TestingAccessWrapper::newFromObject( $fixture );
45 $boom = new InvalidArgumentException( 'boom', 0,
46 new LengthException( 'too long', 0,
47 new LogicException( 'Spock wuz here' )
48 )
49 );
50 $out = $fixture->normalizeException( $boom );
51 $this->assertContains( "\n[Exception InvalidArgumentException]", $out );
52 $this->assertContains( "\nCaused by: [Exception LengthException]", $out );
53 $this->assertContains( "\nCaused by: [Exception LogicException]", $out );
54 $this->assertNotContains( "\n #0", $out );
55 }
56
57 /**
58 * @covers MediaWiki\Logger\Monolog\LineFormatter::normalizeException
59 */
60 public function testNormalizeExceptionTrace() {
61 $fixture = new LineFormatter();
62 $fixture->includeStacktraces( true );
63 $fixture = TestingAccessWrapper::newFromObject( $fixture );
64 $boom = new InvalidArgumentException( 'boom', 0,
65 new LengthException( 'too long', 0,
66 new LogicException( 'Spock wuz here' )
67 )
68 );
69 $out = $fixture->normalizeException( $boom );
70 $this->assertContains( "\n[Exception InvalidArgumentException]", $out );
71 $this->assertContains( "\nCaused by: [Exception LengthException]", $out );
72 $this->assertContains( "\nCaused by: [Exception LogicException]", $out );
73 $this->assertContains( "\n #0", $out );
74 }
75
76 /**
77 * @covers MediaWiki\Logger\Monolog\LineFormatter::normalizeException
78 */
79 public function testNormalizeExceptionErrorNoTrace() {
80 if ( !class_exists( AssertionError::class ) ) {
81 $this->markTestSkipped( 'AssertionError class does not exist' );
82 }
83
84 $fixture = new LineFormatter();
85 $fixture->includeStacktraces( false );
86 $fixture = TestingAccessWrapper::newFromObject( $fixture );
87 $boom = new InvalidArgumentException( 'boom', 0,
88 new LengthException( 'too long', 0,
89 new AssertionError( 'Spock wuz here' )
90 )
91 );
92 $out = $fixture->normalizeException( $boom );
93 $this->assertContains( "\n[Exception InvalidArgumentException]", $out );
94 $this->assertContains( "\nCaused by: [Exception LengthException]", $out );
95 $this->assertContains( "\nCaused by: [Error AssertionError]", $out );
96 $this->assertNotContains( "\n #0", $out );
97 }
98
99 /**
100 * @covers MediaWiki\Logger\Monolog\LineFormatter::normalizeException
101 */
102 public function testNormalizeExceptionErrorTrace() {
103 if ( !class_exists( AssertionError::class ) ) {
104 $this->markTestSkipped( 'AssertionError class does not exist' );
105 }
106
107 $fixture = new LineFormatter();
108 $fixture->includeStacktraces( true );
109 $fixture = TestingAccessWrapper::newFromObject( $fixture );
110 $boom = new InvalidArgumentException( 'boom', 0,
111 new LengthException( 'too long', 0,
112 new AssertionError( 'Spock wuz here' )
113 )
114 );
115 $out = $fixture->normalizeException( $boom );
116 $this->assertContains( "\n[Exception InvalidArgumentException]", $out );
117 $this->assertContains( "\nCaused by: [Exception LengthException]", $out );
118 $this->assertContains( "\nCaused by: [Error AssertionError]", $out );
119 $this->assertContains( "\n #0", $out );
120 }
121 }