SampleTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @coversNothing Just a sample
  4. */
  5. class SampleTest extends MediaWikiLangTestCase {
  6. /**
  7. * Anything that needs to happen before your tests should go here.
  8. */
  9. protected function setUp() {
  10. // Be sure to do call the parent setup and teardown functions.
  11. // This makes sure that all the various cleanup and restorations
  12. // happen as they should (including the restoration for setMwGlobals).
  13. parent::setUp();
  14. // This sets the globals and will restore them automatically
  15. // after each test.
  16. $this->setContentLang( 'en' );
  17. $this->setMwGlobals( [
  18. 'wgCapitalLinks' => true,
  19. ] );
  20. }
  21. /**
  22. * Anything cleanup you need to do should go here.
  23. */
  24. protected function tearDown() {
  25. parent::tearDown();
  26. }
  27. /**
  28. * Name tests so that PHPUnit can turn them into sentences when
  29. * they run. While MediaWiki isn't strictly an Agile Programming
  30. * project, you are encouraged to use the naming described under
  31. * "Agile Documentation" at
  32. * https://www.phpunit.de/manual/3.4/en/other-uses-for-tests.html
  33. */
  34. public function testTitleObjectStringConversion() {
  35. $title = Title::newFromText( "text" );
  36. $this->assertInstanceOf( Title::class, $title, "Title creation" );
  37. $this->assertEquals( "Text", $title, "Automatic string conversion" );
  38. $title = Title::newFromText( "text", NS_MEDIA );
  39. $this->assertEquals( "Media:Text", $title, "Title creation with namespace" );
  40. }
  41. /**
  42. * If you want to run a the same test with a variety of data, use a data provider.
  43. * see: https://www.phpunit.de/manual/3.4/en/writing-tests-for-phpunit.html
  44. */
  45. public static function provideTitles() {
  46. return [
  47. [ 'Text', NS_MEDIA, 'Media:Text' ],
  48. [ 'Text', null, 'Text' ],
  49. [ 'text', null, 'Text' ],
  50. [ 'Text', NS_USER, 'User:Text' ],
  51. [ 'Photo.jpg', NS_FILE, 'File:Photo.jpg' ]
  52. ];
  53. }
  54. /**
  55. * phpcs:disable Generic.Files.LineLength
  56. * @dataProvider provideTitles
  57. * See https://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.dataProvider
  58. * phpcs:enable
  59. */
  60. public function testCreateBasicListOfTitles( $titleName, $ns, $text ) {
  61. $title = Title::newFromText( $titleName, $ns );
  62. $this->assertEquals( $text, "$title", "see if '$titleName' matches '$text'" );
  63. }
  64. public function testSetUpMainPageTitleForNextTest() {
  65. $title = Title::newMainPage();
  66. $this->assertEquals( "Main Page", "$title", "Test initial creation of a title" );
  67. return $title;
  68. }
  69. /**
  70. * Instead of putting a bunch of tests in a single test method,
  71. * you should put only one or two tests in each test method. This
  72. * way, the test method names can remain descriptive.
  73. *
  74. * If you want to make tests depend on data created in another
  75. * method, you can create dependencies feed whatever you return
  76. * from the dependant method (e.g. testInitialCreation in this
  77. * example) as arguments to the next method (e.g. $title in
  78. * testTitleDepends is whatever testInitialCreatiion returned.)
  79. */
  80. /**
  81. * @depends testSetUpMainPageTitleForNextTest
  82. * See https://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.depends
  83. */
  84. public function testCheckMainPageTitleIsConsideredLocal( $title ) {
  85. $this->assertTrue( $title->isLocal() );
  86. }
  87. /**
  88. * @expectedException InvalidArgumentException
  89. * See https://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.expectedException
  90. */
  91. public function testTitleObjectFromObject() {
  92. $title = Title::newFromText( Title::newFromText( "test" ) );
  93. $this->assertEquals( "Test", $title->isLocal() );
  94. }
  95. }