Originally we created a Parser object on every request, and so care was taken to make Parser construction lightweight. In particular, all potentially costly initialization was moved into a separate Parser::firstCallInit() method. Starting with 1.32, parser construction has instead been done lazily, via the ParserFactory registered with MediaWikiServices. The extra complexity associated with the old manual lazy initialization of Parser is therefore no longer needed. Deprecate Parser::firstCallInit() as part of a general plan to refactor the Parser class to allow subclasses and alternate parser implementations. Add some tests to assert that parsers are being created lazily, and are not being created when they are not needed. Bug: T250444 Change-Id: Iffd2b38a2f848dad88010d243250b37506b2c715
83 lines
2.5 KiB
PHP
83 lines
2.5 KiB
PHP
<?php
|
|
|
|
use MediaWiki\BadFileLookup;
|
|
use MediaWiki\Config\ServiceOptions;
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
|
use MediaWiki\Linker\LinkRendererFactory;
|
|
use MediaWiki\SpecialPage\SpecialPageFactory;
|
|
use Psr\Log\LoggerInterface;
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
/**
|
|
* @covers ParserFactory
|
|
*/
|
|
class ParserFactoryTest extends MediaWikiUnitTestCase {
|
|
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' ] )
|
|
->getMock();
|
|
$mwFactory
|
|
->method( 'get' )->will( $this->returnCallback( function ( $arg ) {
|
|
$mw = $this->getMockBuilder( MagicWord::class )
|
|
->disableOriginalConstructor()
|
|
->setMethods( [ 'getSynonyms' ] )
|
|
->getMock();
|
|
$mw->method( 'getSynonyms' )->willReturn( [] );
|
|
return $mw;
|
|
} ) );
|
|
|
|
$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 )
|
|
);
|
|
return $factory;
|
|
}
|
|
|
|
/**
|
|
* @covers ParserFactory::__construct
|
|
*/
|
|
public function testConstructor() {
|
|
$factory = $this->createFactory();
|
|
$this->assertNotNull( $factory, "Factory should be created correctly" );
|
|
}
|
|
|
|
/**
|
|
* @covers ParserFactory::create
|
|
*/
|
|
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
|
|
);
|
|
}
|
|
}
|