3 class MWDebugTest
extends MediaWikiTestCase
{
5 protected function setUp() {
7 /** Clear log before each test */
11 public static function setUpBeforeClass() {
13 MediaWiki\
suppressWarnings();
16 public static function tearDownAfterClass() {
18 MediaWiki\restoreWarnings
();
22 * @covers MWDebug::log
24 public function testAddLog() {
25 MWDebug
::log( 'logging a string' );
28 'msg' => 'logging a string',
30 'caller' => 'MWDebugTest->testAddLog',
37 * @covers MWDebug::warning
39 public function testAddWarning() {
40 MWDebug
::warning( 'Warning message' );
43 'msg' => 'Warning message',
45 'caller' => 'MWDebugTest::testAddWarning',
52 * @covers MWDebug::deprecated
54 public function testAvoidDuplicateDeprecations() {
55 MWDebug
::deprecated( 'wfOldFunction', '1.0', 'component' );
56 MWDebug
::deprecated( 'wfOldFunction', '1.0', 'component' );
58 // assertCount() not available on WMF integration server
59 $this->assertEquals( 1,
60 count( MWDebug
::getLog() ),
61 "Only one deprecated warning per function should be kept"
66 * @covers MWDebug::deprecated
68 public function testAvoidNonConsecutivesDuplicateDeprecations() {
69 MWDebug
::deprecated( 'wfOldFunction', '1.0', 'component' );
70 MWDebug
::warning( 'some warning' );
71 MWDebug
::log( 'we could have logged something too' );
72 // Another deprecation
73 MWDebug
::deprecated( 'wfOldFunction', '1.0', 'component' );
75 // assertCount() not available on WMF integration server
76 $this->assertEquals( 3,
77 count( MWDebug
::getLog() ),
78 "Only one deprecated warning per function should be kept"
83 * @covers MWDebug::appendDebugInfoToApiResult
85 public function testAppendDebugInfoToApiResultXmlFormat() {
86 $request = $this->newApiRequest(
87 [ 'action' => 'help', 'format' => 'xml' ],
88 '/api.php?action=help&format=xml'
91 $context = new RequestContext();
92 $context->setRequest( $request );
94 $apiMain = new ApiMain( $context );
96 $result = new ApiResult( $apiMain );
98 MWDebug
::appendDebugInfoToApiResult( $context, $result );
100 $this->assertInstanceOf( 'ApiResult', $result );
101 $data = $result->getResultData();
103 $expectedKeys = [ 'mwVersion', 'phpEngine', 'phpVersion', 'gitRevision', 'gitBranch',
104 'gitViewUrl', 'time', 'log', 'debugLog', 'queries', 'request', 'memory',
105 'memoryPeak', 'includes', '_element' ];
107 foreach ( $expectedKeys as $expectedKey ) {
108 $this->assertArrayHasKey( $expectedKey, $data['debuginfo'], "debuginfo has $expectedKey" );
111 $xml = ApiFormatXml
::recXmlPrint( 'help', $data );
113 // exception not thrown
114 $this->assertInternalType( 'string', $xml );
118 * @param string[] $params
119 * @param string $requestUrl
121 * @return FauxRequest
123 private function newApiRequest( array $params, $requestUrl ) {
124 $request = $this->getMockBuilder( 'FauxRequest' )
125 ->setMethods( [ 'getRequestURL' ] )
126 ->setConstructorArgs( [
131 $request->expects( $this->any() )
132 ->method( 'getRequestURL' )
133 ->will( $this->returnValue( $requestUrl ) );