wiki.techinc.nl/tests/phpunit/includes/debug/MWDebugTest.php

139 lines
3.3 KiB
PHP
Raw Normal View History

<?php
class MWDebugTest extends MediaWikiTestCase {
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected function setUp() {
parent::setUp();
/** Clear log before each test */
MWDebug::clearLog();
}
public static function setUpBeforeClass() {
MWDebug::init();
MediaWiki\suppressWarnings();
}
public static function tearDownAfterClass() {
MWDebug::deinit();
MediaWiki\restoreWarnings();
}
/**
* @covers MWDebug::log
*/
public function testAddLog() {
MWDebug::log( 'logging a string' );
$this->assertEquals(
[ [
'msg' => 'logging a string',
'type' => 'log',
'caller' => 'MWDebugTest->testAddLog',
] ],
MWDebug::getLog()
);
}
/**
* @covers MWDebug::warning
*/
public function testAddWarning() {
MWDebug::warning( 'Warning message' );
$this->assertEquals(
[ [
'msg' => 'Warning message',
'type' => 'warn',
'caller' => 'MWDebugTest::testAddWarning',
] ],
MWDebug::getLog()
);
}
/**
* @covers MWDebug::deprecated
*/
public function testAvoidDuplicateDeprecations() {
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
// assertCount() not available on WMF integration server
$this->assertEquals( 1,
count( MWDebug::getLog() ),
"Only one deprecated warning per function should be kept"
);
}
/**
* @covers MWDebug::deprecated
*/
public function testAvoidNonConsecutivesDuplicateDeprecations() {
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
MWDebug::warning( 'some warning' );
MWDebug::log( 'we could have logged something too' );
// Another deprecation
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
// assertCount() not available on WMF integration server
$this->assertEquals( 3,
count( MWDebug::getLog() ),
"Only one deprecated warning per function should be kept"
);
}
/**
* @covers MWDebug::appendDebugInfoToApiResult
*/
public function testAppendDebugInfoToApiResultXmlFormat() {
$request = $this->newApiRequest(
[ 'action' => 'help', 'format' => 'xml' ],
'/api.php?action=help&format=xml'
);
$context = new RequestContext();
$context->setRequest( $request );
$apiMain = new ApiMain( $context );
$result = new ApiResult( $apiMain );
MWDebug::appendDebugInfoToApiResult( $context, $result );
$this->assertInstanceOf( 'ApiResult', $result );
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion ApiResult was a mess: some methods could only be used with an array reference instead of manipulating the stored data, methods that had both array-ref and internal-data versions had names that didn't at all correspond, some methods that worked on an array reference were annoyingly non-static, and then the whole mess with setIndexedTagName. ApiFormatXml is also entirely annoying to deal with, as it liked to throw exceptions if certain metadata wasn't provided that no other formatter required. Its legacy also means we have this silly convention of using empty-string rather than boolean true, annoying restrictions on keys (leading to things that should be hashes being arrays of key-value object instead), '*' used as a key all over the place, and so on. So, changes here: * ApiResult is no longer an ApiBase or a ContextSource. * Wherever sensible, ApiResult provides a static method working on an arrayref and a non-static method working on internal data. * Metadata is now always added to ApiResult's internal data structure. Formatters are responsible for stripping it if necessary. "raw mode" is deprecated. * New metadata to replace the '*' key, solve the array() => '[]' vs '{}' question, and so on. * New class for formatting warnings and errors using i18n messages, and support for multiple errors and a more machine-readable format for warnings. For the moment, though, the actual output will not be changing yet (see T47843 for future plans). * New formatversion parameter for format=json and format=php, to select between BC mode and the modern output. * In BC mode, booleans will be converted to empty-string presence style; modules currently returning booleans will need to use ApiResult::META_BC_BOOLS to preserve their current output. Actual changes to the API modules' output (e.g. actually returning booleans for the new formatversion) beyond the use of ApiResult::setContentValue() are left for a future change. Bug: T76728 Bug: T57371 Bug: T33629 Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
$data = $result->getResultData();
$expectedKeys = [ 'mwVersion', 'phpEngine', 'phpVersion', 'gitRevision', 'gitBranch',
'gitViewUrl', 'time', 'log', 'debugLog', 'queries', 'request', 'memory',
'memoryPeak', 'includes', '_element' ];
foreach ( $expectedKeys as $expectedKey ) {
$this->assertArrayHasKey( $expectedKey, $data['debuginfo'], "debuginfo has $expectedKey" );
}
$xml = ApiFormatXml::recXmlPrint( 'help', $data );
// exception not thrown
$this->assertInternalType( 'string', $xml );
}
/**
* @param string[] $params
* @param string $requestUrl
*
* @return FauxRequest
*/
private function newApiRequest( array $params, $requestUrl ) {
$request = $this->getMockBuilder( 'FauxRequest' )
->setMethods( [ 'getRequestURL' ] )
->setConstructorArgs( [
$params
] )
->getMock();
$request->expects( $this->any() )
->method( 'getRequestURL' )
->will( $this->returnValue( $requestUrl ) );
return $request;
}
}