MediaWiki
REL1_24
|
00001 <?php 00002 00003 class MWDebugTest extends MediaWikiTestCase { 00004 00005 protected function setUp() { 00006 parent::setUp(); 00007 // Make sure MWDebug class is enabled 00008 static $MWDebugEnabled = false; 00009 if ( !$MWDebugEnabled ) { 00010 MWDebug::init(); 00011 $MWDebugEnabled = true; 00012 } 00014 MWDebug::clearLog(); 00015 wfSuppressWarnings(); 00016 } 00017 00018 protected function tearDown() { 00019 wfRestoreWarnings(); 00020 parent::tearDown(); 00021 } 00022 00026 public function testAddLog() { 00027 MWDebug::log( 'logging a string' ); 00028 $this->assertEquals( 00029 array( array( 00030 'msg' => 'logging a string', 00031 'type' => 'log', 00032 'caller' => __METHOD__, 00033 ) ), 00034 MWDebug::getLog() 00035 ); 00036 } 00037 00041 public function testAddWarning() { 00042 MWDebug::warning( 'Warning message' ); 00043 $this->assertEquals( 00044 array( array( 00045 'msg' => 'Warning message', 00046 'type' => 'warn', 00047 'caller' => 'MWDebugTest::testAddWarning', 00048 ) ), 00049 MWDebug::getLog() 00050 ); 00051 } 00052 00056 public function testAvoidDuplicateDeprecations() { 00057 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' ); 00058 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' ); 00059 00060 // assertCount() not available on WMF integration server 00061 $this->assertEquals( 1, 00062 count( MWDebug::getLog() ), 00063 "Only one deprecated warning per function should be kept" 00064 ); 00065 } 00066 00070 public function testAvoidNonConsecutivesDuplicateDeprecations() { 00071 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' ); 00072 MWDebug::warning( 'some warning' ); 00073 MWDebug::log( 'we could have logged something too' ); 00074 // Another deprecation 00075 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' ); 00076 00077 // assertCount() not available on WMF integration server 00078 $this->assertEquals( 3, 00079 count( MWDebug::getLog() ), 00080 "Only one deprecated warning per function should be kept" 00081 ); 00082 } 00083 00087 public function testAppendDebugInfoToApiResultXmlFormat() { 00088 $request = $this->newApiRequest( 00089 array( 'action' => 'help', 'format' => 'xml' ), 00090 '/api.php?action=help&format=xml' 00091 ); 00092 00093 $context = new RequestContext(); 00094 $context->setRequest( $request ); 00095 00096 $apiMain = new ApiMain( $context ); 00097 00098 $result = new ApiResult( $apiMain ); 00099 $result->setRawMode( true ); 00100 00101 MWDebug::appendDebugInfoToApiResult( $context, $result ); 00102 00103 $this->assertInstanceOf( 'ApiResult', $result ); 00104 $data = $result->getData(); 00105 00106 $expectedKeys = array( 'mwVersion', 'phpEngine', 'phpVersion', 'gitRevision', 'gitBranch', 00107 'gitViewUrl', 'time', 'log', 'debugLog', 'queries', 'request', 'memory', 00108 'memoryPeak', 'includes', 'profile', '_element' ); 00109 00110 foreach ( $expectedKeys as $expectedKey ) { 00111 $this->assertArrayHasKey( $expectedKey, $data['debuginfo'], "debuginfo has $expectedKey" ); 00112 } 00113 00114 $xml = ApiFormatXml::recXmlPrint( 'help', $data ); 00115 00116 // exception not thrown 00117 $this->assertInternalType( 'string', $xml ); 00118 } 00119 00126 private function newApiRequest( array $params, $requestUrl ) { 00127 $request = $this->getMockBuilder( 'FauxRequest' ) 00128 ->setMethods( array( 'getRequestURL' ) ) 00129 ->setConstructorArgs( array( 00130 $params 00131 ) ) 00132 ->getMock(); 00133 00134 $request->expects( $this->any() ) 00135 ->method( 'getRequestURL' ) 00136 ->will( $this->returnValue( $requestUrl ) ); 00137 00138 return $request; 00139 } 00140 00141 }