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