Initializing the preprocessor in the constructor allows better dependency injection, and removes code complexity caused by lazy initialization. Any use of the parser is going to end up creating the preprocessor in any case, so deferring the initialization doesn't save any performance. (Best performance is given by not creating the Parser in the first place if it is not needed, which is what DI allows.) Old code tried to unbreak cyclic dependencies by setting the preprocessor to null. This is somewhat of a lost cause, since there are a number of other cyclic dependencies involving the parser, including StripState, LinkHolders, etc. The code complexity is not worth it, given how ineffective it is in any case. This is part of T275160 in so far as it allows Parser::getPreprocessor() to be a simple getter, and thus (once this patch is merged) we can safely replace any direct access to Parser::$mPreprocessor with a call to Parser::getPreprocessor(). Bug: T275160 Change-Id: I38c6fe7d5a97badffdbf34d8b9d725756ed86514
96 lines
3 KiB
PHP
96 lines
3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @covers Parser::__construct
|
|
*/
|
|
class ParserTest extends MediaWikiIntegrationTestCase {
|
|
/**
|
|
* Helper method to create mocks
|
|
* @return array
|
|
*/
|
|
private function createConstructorArguments() {
|
|
// Create a mock Config object that will satisfy ServiceOptions::__construct
|
|
$mockConfig = $this->createMock( Config::class );
|
|
$mockConfig->method( 'has' )->willReturn( true );
|
|
$mockConfig->method( 'get' )->will(
|
|
$this->returnCallback( function ( $arg ) {
|
|
return ( $arg === 'TidyConfig' ) ? null : 'I like otters.';
|
|
} )
|
|
);
|
|
|
|
// Stub out a MagicWordFactory so the Parser can initialize its
|
|
// function hooks when it is created.
|
|
$mwFactory = $this->getMockBuilder( MagicWordFactory::class )
|
|
->disableOriginalConstructor()
|
|
->setMethods( [ 'get', 'getVariableIDs' ] )
|
|
->getMock();
|
|
$mwFactory
|
|
->method( 'get' )->will( $this->returnCallback( function ( $arg ) {
|
|
$mw = $this->getMockBuilder( MagicWord::class )
|
|
->disableOriginalConstructor()
|
|
->setMethods( [ 'getSynonyms' ] )
|
|
->getMock();
|
|
$mw->method( 'getSynonyms' )->willReturn( [] );
|
|
return $mw;
|
|
} ) );
|
|
$mwFactory
|
|
->method( 'getVariableIDs' )->willReturn( [] );
|
|
|
|
return [
|
|
$this->createMock( MediaWiki\Config\ServiceOptions::class ),
|
|
$mwFactory,
|
|
$this->createMock( Language::class ),
|
|
$this->createMock( ParserFactory::class ),
|
|
'a snail can sleep for three years',
|
|
$this->createMock( MediaWiki\Special\SpecialPageFactory::class ),
|
|
$this->createMock( MediaWiki\Linker\LinkRendererFactory::class ),
|
|
$this->createMock( NamespaceInfo::class ),
|
|
new Psr\Log\NullLogger(),
|
|
$this->createMock( MediaWiki\BadFileLookup::class ),
|
|
$this->createMock( MediaWiki\Languages\LanguageConverterFactory::class ),
|
|
$this->createMock( MediaWiki\HookContainer\HookContainer::class ),
|
|
$this->createMock( MediaWiki\Tidy\TidyDriverBase::class ),
|
|
$this->createMock( WANObjectCache::class ),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @covers Parser::__construct
|
|
*/
|
|
public function testConstructorArguments() {
|
|
$args = $this->createConstructorArguments();
|
|
|
|
// Fool Parser into thinking we are constructing via a ParserFactory
|
|
ParserFactory::$inParserFactory += 1;
|
|
try {
|
|
$parser = new Parser( ...$args );
|
|
} finally {
|
|
ParserFactory::$inParserFactory -= 1;
|
|
}
|
|
|
|
$refObject = new ReflectionObject( $parser );
|
|
foreach ( $refObject->getProperties() as $prop ) {
|
|
$prop->setAccessible( true );
|
|
foreach ( $args as $idx => $mockTest ) {
|
|
if ( $prop->getValue( $parser ) === $mockTest ) {
|
|
unset( $args[$idx] );
|
|
}
|
|
}
|
|
}
|
|
// The WANObjectCache gets set on the Preprocessor, not the
|
|
// Parser.
|
|
$preproc = $parser->getPreprocessor();
|
|
$refObject = new ReflectionObject( $preproc );
|
|
foreach ( $refObject->getProperties() as $prop ) {
|
|
$prop->setAccessible( true );
|
|
foreach ( $args as $idx => $mockTest ) {
|
|
if ( $prop->getValue( $preproc ) === $mockTest ) {
|
|
unset( $args[$idx] );
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->assertSame( [], $args, 'Not all arguments to the Parser constructor were ' .
|
|
'found on the Parser object' );
|
|
}
|
|
}
|