3 * Helping class to run tests using a clean language instance.
5 * This is intended for the MediaWiki language class tests under
6 * tests/phpunit/languages. You simply need to extends this test
7 * and set it up with a language code using setUpBeforeClass:
9 * @par Setting up a language:
11 * class LanguageFooTest extends LanguageClassesTestCase {
12 * public static function setUpBeforeClass() {
13 * self::setLang( 'Foo' );
17 * Before each tests, a new language object is build which you
18 * can retrieve in your test using the $this->getLang() method:
20 * @par Using the crafted language object:
22 * function testHasLanguageObject() {
23 * $langObject = $this->getLang();
24 * $this->assertInstanceOf( 'LanguageFoo',
30 abstract class LanguageClassesTestCase
extends MediaWikiTestCase
{
33 * Regex used to find out the language code out of the class name
34 * used by setUpBeforeClass
36 private static $reExtractLangFromClass = '/Language(.*)Test/';
39 * Hold the language code we are going to use. This is extracted
40 * directly from the extending class.
42 private static $LanguageClassCode;
45 * Internal language object
47 * A new object is created before each tests thanks to PHPUnit
48 * setUp() method, it is deleted after each test too. To get
49 * this object you simply use the getLang method.
51 * You must have setup a language code first. See $LanguageClassCode
53 * function testWeAreTheChampions() {
54 * $this->getLang(); # language object
58 private $languageObject;
60 public static function setUpBeforeClass() {
61 $found = preg_match( self
::$reExtractLangFromClass,
62 get_called_class(), $m );
64 # Normalize language code since classes uses underscores
65 $m[1] = str_replace( '_', '-', $m[1] );
67 # Fallback to english language
70 __METHOD__
. " could not extract a language name "
71 . "out of " . get_called_class() . " failling back to 'en'\n"
74 // TODO: validate $m[1] which should be a valid language code
75 self
::$LanguageClassCode = $m[1];
78 protected function getLang() {
79 return $this->languageObject
;
83 * Create a new language object before each test.
85 protected function setUp() {
87 $this->languageObject
= Language
::factory(
88 self
::$LanguageClassCode );
92 * Delete the internal language object so each test start
93 * out with a fresh language instance.
95 protected function tearDown() {
96 unset( $this->languageObject
);