LanguageClassesTestCase.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * @return Language
  39. */
  40. protected function getLang() {
  41. return $this->languageObject;
  42. }
  43. /**
  44. * Create a new language object before each test.
  45. */
  46. protected function setUp() {
  47. parent::setUp();
  48. $found = preg_match( '/Language(.+)Test/', static::class, $m );
  49. if ( $found ) {
  50. # Normalize language code since classes uses underscores
  51. $m[1] = strtolower( str_replace( '_', '-', $m[1] ) );
  52. } else {
  53. # Fallback to english language
  54. $m[1] = 'en';
  55. wfDebug(
  56. __METHOD__ . ' could not extract a language name '
  57. . 'out of ' . static::class . " failling back to 'en'\n"
  58. );
  59. }
  60. // @todo validate $m[1] which should be a valid language code
  61. $this->languageObject = Language::factory( $m[1] );
  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. }