Debian Codegraph @

tests/phpunit/includes/SampleTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Title\Title;
5
6 /**
7 * @coversNothing Just a sample
8 */
9 class SampleTest extends MediaWikiLangTestCase {
10
11 /**
12 * Anything that needs to happen before your tests should go here.
13 */
14 protected function setUp(): void {
15 // Be sure to call the parent setup and teardown functions.
16 // This makes sure that all the various cleanup and restorations
17 // happen as they should (including the restoration for setMwGlobals).
18 parent::setUp();
19
20 // This sets the config settings, and will restore them automatically
21 // after each test.
22 $this->overrideConfigValues( [
23 MainConfigNames::LanguageCode => 'en',
24 MainConfigNames::CapitalLinks => true,
25 ] );
26 }
27
28 /**
29 * Anything cleanup you need to do should go here.
30 */
31 protected function tearDown(): void {
32 parent::tearDown();
33 }
34
35 /**
36 * Name tests so that PHPUnit can turn them into sentences when
37 * they run. You are encouraged to use the naming described at:
38 * https://phpunit.de/manual/6.5/en/other-uses-for-tests.html
39 */
40 public function testTitleObjectStringConversion() {
41 $title = Title::makeTitle( NS_MAIN, "Text" );
42 $this->assertInstanceOf( Title::class, $title, "Title creation" );
43 $this->assertEquals( "Text", $title, "Automatic string conversion" );
44
45 $title = Title::makeTitle( NS_MEDIA, "Text" );
46 $this->assertEquals( "Media:Text", $title, "Title creation with namespace" );
47 }
48
49 /**
50 * If you want to run the same test with a variety of data, use a data provider.
51 * See https://phpunit.de/manual/6.5/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 * @dataProvider provideTitles
65 * See https://phpunit.de/manual/6.5/en/appendixes.annotations.html#appendixes.annotations.dataProvider
66 */
67 public function testCreateBasicListOfTitles( $titleName, $ns, $text ) {
68 $title = Title::newFromText( $titleName, $ns );
69 $this->assertEquals( $text, "$title", "see if '$titleName' matches '$text'" );
70 }
71
72 /**
73 * Instead of putting a bunch of tests in a single test method,
74 * you should put only one or two tests in each test method. This
75 * way, the test method names can remain descriptive.
76 */
77
78 /**
79 * See https://phpunit.de/manual/6.5/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.exceptions
80 */
81 public function testTitleObjectFromObject() {
82 $this->expectException( InvalidArgumentException::class );
83 Title::newFromText( Title::makeTitle( NS_MAIN, 'Test' ) );
84 }
85 }