Merge "Added a few more trx sanity checks to DatabaseBase"
[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 protected function getLang() {
39 return $this->languageObject;
40 }
41
42 /**
43 * Create a new language object before each test.
44 */
45 protected function setUp() {
46 parent::setUp();
47 $found = preg_match( '/Language(.+)Test/', get_called_class(), $m );
48 if ( $found ) {
49 # Normalize language code since classes uses underscores
50 $m[1] = str_replace( '_', '-', $m[1] );
51 } else {
52 # Fallback to english language
53 $m[1] = 'en';
54 wfDebug(
55 __METHOD__ . " could not extract a language name "
56 . "out of " . get_called_class() . " failling back to 'en'\n"
57 );
58 }
59 // TODO: validate $m[1] which should be a valid language code
60 $this->languageObject = Language::factory( $m[1] );
61 }
62
63 /**
64 * Delete the internal language object so each test start
65 * out with a fresh language instance.
66 */
67 protected function tearDown() {
68 unset( $this->languageObject );
69 parent::tearDown();
70 }
71 }