474746626d5d8a91fb9b8951fe75d9b22426fc16
[lhc/web/wiklou.git] / tests / phpunit / includes / SampleTest.php
1 <?php
2
3 /**
4 * @coversNothing Just a sample
5 */
6 class SampleTest extends MediaWikiLangTestCase {
7
8 /**
9 * Anything that needs to happen before your tests should go here.
10 */
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).
15 parent::setUp();
16
17 // This sets the globals and will restore them automatically
18 // after each test.
19 $this->setMwGlobals( [
20 'wgContLang' => Language::factory( 'en' ),
21 'wgLanguageCode' => 'en',
22 'wgCapitalLinks' => true,
23 ] );
24 }
25
26 /**
27 * Anything cleanup you need to do should go here.
28 */
29 protected function tearDown() {
30 parent::tearDown();
31 }
32
33 /**
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
39 */
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" );
44
45 $title = Title::newFromText( "text", NS_MEDIA );
46 $this->assertEquals( "Media:Text", $title, "Title creation with namespace" );
47 }
48
49 /**
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
52 */
53 public static function provideTitles() {
54 return [
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' ]
60 ];
61 }
62
63 /**
64 * phpcs:disable Generic.Files.LineLength
65 * @dataProvider provideTitles
66 * See https://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.dataProvider
67 * phpcs:enable
68 */
69 public function testCreateBasicListOfTitles( $titleName, $ns, $text ) {
70 $title = Title::newFromText( $titleName, $ns );
71 $this->assertEquals( $text, "$title", "see if '$titleName' matches '$text'" );
72 }
73
74 public function testSetUpMainPageTitleForNextTest() {
75 $title = Title::newMainPage();
76 $this->assertEquals( "Main Page", "$title", "Test initial creation of a title" );
77
78 return $title;
79 }
80
81 /**
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.
85 *
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.)
91 */
92
93 /**
94 * @depends testSetUpMainPageTitleForNextTest
95 * See https://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.depends
96 */
97 public function testCheckMainPageTitleIsConsideredLocal( $title ) {
98 $this->assertTrue( $title->isLocal() );
99 }
100
101 /**
102 * @expectedException InvalidArgumentException
103 * See https://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.expectedException
104 */
105 public function testTitleObjectFromObject() {
106 $title = Title::newFromText( Title::newFromText( "test" ) );
107 $this->assertEquals( "Test", $title->isLocal() );
108 }
109 }