wiki.techinc.nl/tests/phpunit/unit/languages/LanguageTest.php
Aryeh Gregor 251f4811be Cache nonexistence of grammar transformations
Previously every call to Language::getGrammarTransformations() for a
language without grammar transformations (i.e., almost all) would look
for a transformations file using is_readable(). This is not going to be
especially expensive, but it's presumably still more expensive than
caching the result.

The change is not actually testable right now without resorting to
TestingAccessWrapper or somehow mocking the file access, but I threw in
some basic tests for the method anyway. If later we decide how best to
mock the file access, they can be updated to check that the cache is
working properly.

Change-Id: I76e0fb7dbb1602df4f353722aacbd5adfff3c053
2019-10-29 09:12:21 +00:00

50 lines
1.3 KiB
PHP

<?php
use MediaWiki\Languages\LanguageNameUtils;
use MediaWiki\Languages\LanguageFallback;
/**
* @coversDefaultClass Language
*/
class LanguageTest extends MediaWikiUnitTestCase {
/**
* @param array $options Valid keys:
* 'code'
* 'grammarTransformCache'
*/
private function getObj( array $options = [] ) {
return new Language(
$options['code'] ?? 'en',
$this->createNoOpMock( LocalisationCache::class ),
$this->createNoOpMock( LanguageNameUtils::class ),
$this->createNoOpMock( LanguageFallback::class )
);
}
/**
* @covers ::getGrammarTransformations
* @todo Test the exception case
*/
public function testGetGrammarTransformations() {
global $IP;
// XXX Inject a value here instead of reading the filesystem (T231159)
$expected = FormatJson::decode(
file_get_contents( "$IP/languages/data/grammarTransformations/he.json" ), true );
$lang = $this->getObj( [ 'code' => 'he' ] );
$this->assertSame( $expected, $lang->getGrammarTransformations() );
$this->assertSame( $expected, $lang->getGrammarTransformations() );
}
/**
* @covers ::getGrammarTransformations
*/
public function testGetGrammarTransformations_empty() {
$lang = $this->getObj();
$this->assertSame( [], $lang->getGrammarTransformations() );
$this->assertSame( [], $lang->getGrammarTransformations() );
}
}