test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / languages / LanguageClassesTestCase.php
blob4274335df5b56f5ccec5adb40bec36fa6c77263c
1 <?php
2 /**
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:
10 * @code
11 * class LanguageFooTest extends LanguageClassesTestCase {
12 * public static function setUpBeforeClass() {
13 * self::setLang( 'Foo' );
14 * }
15 * @endcode
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:
21 * @code
22 * function testHasLanguageObject() {
23 * $langObject = $this->getLang();
24 * $this->assertInstanceOf( 'LanguageFoo',
25 * $langObject
26 * );
27 * }
28 * @endcode
30 abstract class LanguageClassesTestCase extends MediaWikiTestCase {
32 /**
33 * Regex used to find out the language code out of the class name
34 * used by setUpBeforeClass
36 private static $reExtractLangFromClass = '/Language(.*)Test/';
38 /**
39 * Hold the language code we are going to use. This is extracted
40 * directly from the extending class.
42 private static $LanguageClassCode;
44 /**
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
52 * @code
53 * function testWeAreTheChampions() {
54 * $this->getLang(); # language object
55 * }
56 * @endcode
58 private $languageObject;
60 public static function setUpBeforeClass() {
61 $found = preg_match( self::$reExtractLangFromClass,
62 get_called_class(), $m );
63 if ( $found ) {
64 # Normalize language code since classes uses underscores
65 $m[1] = str_replace( '_', '-', $m[1] );
66 } else {
67 # Fallback to english language
68 $m[1] = 'en';
69 wfDebug(
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;
82 /**
83 * Create a new language object before each test.
85 protected function setUp() {
86 parent::setUp();
87 $this->languageObject = Language::factory(
88 self::$LanguageClassCode );
91 /**
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 );
97 parent::tearDown();