wiki.techinc.nl/tests/phpunit/includes/MagicWordFactoryTest.php
Timo Tijhof 128debb64b tests: Change use of AtEase to at operator
Follows-up I361fde0de7f4406bce6ed075ed397effa5be3359.

Per T253461, not mass-changing source code, but the use of the native
error silencing operator (@) is especially useful in tests because:

1. It requires any/all statements to be explicitly marked. The
   suppressWarnings/restoreWarnings sections encourage developers to
   be "lazy" and thus encapsulate more than needed if there are multiple
   ones near each other, which would ignore potentially important
   warnings in a test case, which is generally exactly the time when
   it is really useful to get warnings etc.

2. It avoids leaking state, for example in LBFactoryTest the
   assertFalse call would throw a PHPUnit assertion error (not meant
   to be caught by the local catch), and thus won't reach
   AtEase::restoreWarnings. This then causes later code to end up
   in a mismatching state and creates a confusing error_reporting
   state.

See .phpcs.xml, where the at operator is allowed for all test code.

Change-Id: I68d1725d685e0a7586468bc9de6dc29ceea31b8a
2022-02-24 21:29:51 +00:00

83 lines
2.4 KiB
PHP

<?php
/**
* @covers \MagicWordFactory
*
* @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( MWException::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 );
}
/**
* Test both valid and invalid caching hints paths
*/
public function testGetCacheTTL() {
$magicWordFactory = $this->makeMagicWordFactory();
$actual = $magicWordFactory->getCacheTTL( 'localday' );
$this->assertSame( 3600, $actual );
$actual = $magicWordFactory->getCacheTTL( 'currentmonth' );
$this->assertSame( 86400, $actual );
$actual = $magicWordFactory->getCacheTTL( 'invalid' );
$this->assertSame( -1, $actual );
}
public function testGetDoubleUnderscoreArray() {
$magicWordFactory = $this->makeMagicWordFactory();
$actual = $magicWordFactory->getDoubleUnderscoreArray();
$this->assertInstanceOf( MagicWordArray::class, $actual );
}
}