Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / languages / LanguageClassesTestCase.php
1 <?php
2 /**
3 * Helping class to run tests using a clean language instance.
4 *
5 * This is intended for the MediaWiki language class tests under
6 * tests/phpunit/languages.
7 *
8 * Before each tests, a new language object is build which you
9 * can retrieve in your test using the $this->getLang() method:
10 *
11 * @par Using the crafted language object:
12 * @code
13 * function testHasLanguageObject() {
14 * $langObject = $this->getLang();
15 * $this->assertInstanceOf( 'LanguageFoo',
16 * $langObject
17 * );
18 * }
19 * @endcode
20 */
21 abstract class LanguageClassesTestCase extends MediaWikiTestCase {
22 /**
23 * Internal language object
24 *
25 * A new object is created before each tests thanks to PHPUnit
26 * setUp() method, it is deleted after each test too. To get
27 * this object you simply use the getLang method.
28 *
29 * You must have setup a language code first. See $LanguageClassCode
30 * @code
31 * function testWeAreTheChampions() {
32 * $this->getLang(); # language object
33 * }
34 * @endcode
35 */
36 private $languageObject;
37
38 /**
39 * @return Language
40 */
41 protected function getLang() {
42 return $this->languageObject;
43 }
44
45 /**
46 * Create a new language object before each test.
47 */
48 protected function setUp() {
49 parent::setUp();
50 $found = preg_match( '/Language(.+)Test/', static::class, $m );
51 if ( $found ) {
52 # Normalize language code since classes uses underscores
53 $m[1] = strtolower( str_replace( '_', '-', $m[1] ) );
54 } else {
55 # Fallback to english language
56 $m[1] = 'en';
57 wfDebug(
58 __METHOD__ . ' could not extract a language name '
59 . 'out of ' . static::class . " failling back to 'en'\n"
60 );
61 }
62 // @todo validate $m[1] which should be a valid language code
63 $this->languageObject = Language::factory( $m[1] );
64 }
65
66 /**
67 * Delete the internal language object so each test start
68 * out with a fresh language instance.
69 */
70 protected function tearDown() {
71 unset( $this->languageObject );
72 parent::tearDown();
73 }
74 }