Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / debug / MWDebugTest.php
blob5c6548315037e73014b2c2dc0ac1db98bcc12f8f
1 <?php
3 class MWDebugTest extends MediaWikiTestCase {
5 protected function setUp() {
6 parent::setUp();
7 /** Clear log before each test */
8 MWDebug::clearLog();
11 public static function setUpBeforeClass() {
12 parent::setUpBeforeClass();
13 MWDebug::init();
14 MediaWiki\suppressWarnings();
17 public static function tearDownAfterClass() {
18 parent::tearDownAfterClass();
19 MWDebug::deinit();
20 MediaWiki\restoreWarnings();
23 /**
24 * @covers MWDebug::log
26 public function testAddLog() {
27 MWDebug::log( 'logging a string' );
28 $this->assertEquals(
29 [ [
30 'msg' => 'logging a string',
31 'type' => 'log',
32 'caller' => 'MWDebugTest->testAddLog',
33 ] ],
34 MWDebug::getLog()
38 /**
39 * @covers MWDebug::warning
41 public function testAddWarning() {
42 MWDebug::warning( 'Warning message' );
43 $this->assertEquals(
44 [ [
45 'msg' => 'Warning message',
46 'type' => 'warn',
47 'caller' => 'MWDebugTest::testAddWarning',
48 ] ],
49 MWDebug::getLog()
53 /**
54 * @covers MWDebug::deprecated
56 public function testAvoidDuplicateDeprecations() {
57 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
58 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
60 // assertCount() not available on WMF integration server
61 $this->assertEquals( 1,
62 count( MWDebug::getLog() ),
63 "Only one deprecated warning per function should be kept"
67 /**
68 * @covers MWDebug::deprecated
70 public function testAvoidNonConsecutivesDuplicateDeprecations() {
71 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
72 MWDebug::warning( 'some warning' );
73 MWDebug::log( 'we could have logged something too' );
74 // Another deprecation
75 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
77 // assertCount() not available on WMF integration server
78 $this->assertEquals( 3,
79 count( MWDebug::getLog() ),
80 "Only one deprecated warning per function should be kept"
84 /**
85 * @covers MWDebug::appendDebugInfoToApiResult
87 public function testAppendDebugInfoToApiResultXmlFormat() {
88 $request = $this->newApiRequest(
89 [ 'action' => 'help', 'format' => 'xml' ],
90 '/api.php?action=help&format=xml'
93 $context = new RequestContext();
94 $context->setRequest( $request );
96 $apiMain = new ApiMain( $context );
98 $result = new ApiResult( $apiMain );
100 MWDebug::appendDebugInfoToApiResult( $context, $result );
102 $this->assertInstanceOf( 'ApiResult', $result );
103 $data = $result->getResultData();
105 $expectedKeys = [ 'mwVersion', 'phpEngine', 'phpVersion', 'gitRevision', 'gitBranch',
106 'gitViewUrl', 'time', 'log', 'debugLog', 'queries', 'request', 'memory',
107 'memoryPeak', 'includes', '_element' ];
109 foreach ( $expectedKeys as $expectedKey ) {
110 $this->assertArrayHasKey( $expectedKey, $data['debuginfo'], "debuginfo has $expectedKey" );
113 $xml = ApiFormatXml::recXmlPrint( 'help', $data );
115 // exception not thrown
116 $this->assertInternalType( 'string', $xml );
120 * @param string[] $params
121 * @param string $requestUrl
123 * @return FauxRequest
125 private function newApiRequest( array $params, $requestUrl ) {
126 $request = $this->getMockBuilder( 'FauxRequest' )
127 ->setMethods( [ 'getRequestURL' ] )
128 ->setConstructorArgs( [
129 $params
131 ->getMock();
133 $request->expects( $this->any() )
134 ->method( 'getRequestURL' )
135 ->will( $this->returnValue( $requestUrl ) );
137 return $request;