wiki.techinc.nl/tests/phpunit/unit/includes/parser/ParserFactoryTest.php
C. Scott Ananian 4497f99796 Parser: initialize preprocessor in constructor
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
2021-03-16 22:37:40 +00:00

89 lines
2.8 KiB
PHP

<?php
use MediaWiki\BadFileLookup;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Languages\LanguageConverterFactory;
use MediaWiki\Linker\LinkRendererFactory;
use MediaWiki\SpecialPage\SpecialPageFactory;
use MediaWiki\Tidy\TidyDriverBase;
use Psr\Log\LoggerInterface;
use Wikimedia\TestingAccessWrapper;
/**
* @covers ParserFactory
*/
class ParserFactoryTest extends MediaWikiUnitTestCase {
private function createFactory() {
$options = $this->getMockBuilder( ServiceOptions::class )
->disableOriginalConstructor()
->setMethods( [ 'assertRequiredOptions', 'get' ] )->getMock();
$options->expects( $this->never() )
->method( $this->anythingBut( 'assertRequiredOptions', 'get' ) );
$this->assertInstanceOf( ServiceOptions::class, $options );
// 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( [] );
$factory = new ParserFactory(
$options,
$mwFactory,
$this->createNoOpMock( Language::class ),
"",
$this->createNoOpMock( SpecialPageFactory::class ),
$this->createNoOpMock( LinkRendererFactory::class ),
$this->createNoOpMock( NamespaceInfo::class ),
$this->createNoOpMock( LoggerInterface::class ),
$this->createNoOpMock( BadFileLookup::class ),
$this->createNoOpMock( LanguageConverterFactory::class ),
$this->createHookContainer(),
$this->createNoOpMock( TidyDriverBase::class ),
$this->createNoOpMock( WANObjectCache::class )
);
return $factory;
}
/**
* @covers ParserFactory::__construct
*/
public function testConstructor() {
$factory = $this->createFactory();
$this->assertNotNull( $factory, "Factory should be created correctly" );
}
/**
* @covers ParserFactory::create
*/
public function testCreate() {
$factory = $this->createFactory();
$parser = $factory->create();
$this->assertNotNull( $factory, "Factory should be created correctly" );
$this->assertNotNull( $parser, "Factory should create parser correctly" );
$this->assertInstanceOf( Parser::class, $parser );
$parserWrapper = TestingAccessWrapper::newFromObject( $parser );
$factoryWrapper = TestingAccessWrapper::newFromObject( $factory );
$this->assertSame(
$factoryWrapper->languageConverterFactory, $parserWrapper->languageConverterFactory
);
$this->assertSame(
$factory, $parserWrapper->factory
);
}
}