wiki.techinc.nl/tests/phpunit/includes/parser/MagicWordFactoryTest.php
thiemowmde 10a828ba72 Deprecate MagicWordFactory::getSubstIDs
The main motivation is to further reduce the complexity of the class:
* There is no code that ever writes to $this->mSubstIDs. It's
  effectively a constant.
* According to CodeSearch the getSubstIDs() method is not used
  anywhere. It's @internal to the parser.
* I find it weird that the parser needs to call 2 factory methods to
  do 1 thing.
* I still find it a good idea to keep the knowledge encapsulated in
  the factory and not have the [ 'subst', 'safesubst' ] array in the
  parser. That's why I propose the new method.

Change-Id: I5c147c75200c3c34a410d93a0328b56ea00a050f
2023-11-13 11:10:24 +01:00

72 lines
2.2 KiB
PHP

<?php
use MediaWiki\Parser\MagicWord;
use MediaWiki\Parser\MagicWordArray;
use MediaWiki\Parser\MagicWordFactory;
/**
* @covers \MediaWiki\Parser\MagicWordFactory
* @covers \MediaWiki\Parser\MagicWord
*
* @author Derick N. Alangi
*/
class MagicWordFactoryTest extends MediaWikiIntegrationTestCase {
private function makeMagicWordFactory( Language $contLang = null ) {
$services = $this->getServiceContainer();
return new MagicWordFactory( $contLang ?:
$services->getLanguageFactory()->getLanguage( 'en' ),
$services->getHookContainer()
);
}
public function testGetContentLanguage() {
$contLang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );
$magicWordFactory = $this->makeMagicWordFactory( $contLang );
$magicWordContLang = $magicWordFactory->getContentLanguage();
$this->assertSame( $contLang, $magicWordContLang );
}
public function testGetMagicWord() {
$magicWordIdValid = 'pageid';
$magicWordFactory = $this->makeMagicWordFactory();
$mwActual = $magicWordFactory->get( $magicWordIdValid );
$contLang = $magicWordFactory->getContentLanguage();
$expected = new MagicWord( $magicWordIdValid, [ 'PAGEID' ], false, $contLang );
$this->assertEquals( $expected, $mwActual );
}
public function testGetInvalidMagicWord() {
$magicWordFactory = $this->makeMagicWordFactory();
$this->expectException( UnexpectedValueException::class );
@$magicWordFactory->get( 'invalid magic word' );
}
public function testGetVariableIDs() {
$magicWordFactory = $this->makeMagicWordFactory();
$varIds = $magicWordFactory->getVariableIDs();
$this->assertIsArray( $varIds );
$this->assertNotEmpty( $varIds );
$this->assertContainsOnly( 'string', $varIds );
}
public function testGetSubstArray() {
$magicWordFactory = $this->makeMagicWordFactory();
$substArray = $magicWordFactory->getSubstArray();
$text = 'SafeSubst:x';
$this->assertSame( 'safesubst', $substArray->matchStartAndRemove( $text ) );
$this->assertSame( 'x', $text );
}
public function testGetDoubleUnderscoreArray() {
$magicWordFactory = $this->makeMagicWordFactory();
$actual = $magicWordFactory->getDoubleUnderscoreArray();
$this->assertInstanceOf( MagicWordArray::class, $actual );
}
}