wiki.techinc.nl/tests/phpunit/includes/parser/MagicWordFactoryTest.php
Timo Tijhof 08ddbf3465 parser: deprecate unused MagicWord::getId, improve docs and tests
* MagicWord::getId was added in r24808 (164bb322f2) but never used.
  At the time, access modifiers like 'private' were not yet in use.
  Deprecate the method with warnings, for removal in a future release.

* Fix zero coverage for MagicWord, due to constructor being
  internal, this is only intended to be created via array and
  factory classes. Let their tests cover this class.

* Remove redundant file-level description and ensure the class desc
  and ingroup tag are on the class block instead.
  Ref https://gerrit.wikimedia.org/r/q/owner:Krinkle+message:ingroup

* Mark constructor `@internal` (was already implied by
  stable interface policy), and explain where to get the object
  instead.

* Mark load() `@internal`. Method was introduced in 1.1 when the
  class (and PHP) did not yet use visibility modifiers for private
  methods. The only way to get an instance of MagicWord
  (MagicWordFactory::get) already calls load(), the method is not
  a no-op if called a second time, and (fortunately) there exist no
  callers to this outside this class that I could find.

* MagicWordArray::getBaseRegex was marked as internal
  in change I17f1b7207db8d2203c904508f3ab8a64b68736a8.

Change-Id: I4084f858bb356029c142fbdb699f91cf0d6ec56f
2023-10-26 16:07:20 +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 testGetSubstIDs() {
$magicWordFactory = $this->makeMagicWordFactory();
$substIds = $magicWordFactory->getSubstIDs();
$this->assertIsArray( $substIds );
$this->assertNotEmpty( $substIds );
$this->assertContainsOnly( 'string', $substIds );
}
public function testGetDoubleUnderscoreArray() {
$magicWordFactory = $this->makeMagicWordFactory();
$actual = $magicWordFactory->getDoubleUnderscoreArray();
$this->assertInstanceOf( MagicWordArray::class, $actual );
}
}