MediaWiki  1.28.0
ApiMessageTest.php
Go to the documentation of this file.
1 <?php
2 
7 
8  private function compareMessages( $msg, $msg2 ) {
9  $this->assertSame( $msg->getKey(), $msg2->getKey(), 'getKey' );
10  $this->assertSame( $msg->getKeysToTry(), $msg2->getKeysToTry(), 'getKeysToTry' );
11  $this->assertSame( $msg->getParams(), $msg2->getParams(), 'getParams' );
12  $this->assertSame( $msg->getFormat(), $msg2->getFormat(), 'getFormat' );
13  $this->assertSame( $msg->getLanguage(), $msg2->getLanguage(), 'getLanguage' );
14 
16  $msg2 = TestingAccessWrapper::newFromObject( $msg2 );
17  $this->assertSame( $msg->interface, $msg2->interface, 'interface' );
18  $this->assertSame( $msg->useDatabase, $msg2->useDatabase, 'useDatabase' );
19  $this->assertSame(
20  $msg->title ? $msg->title->getFullText() : null,
21  $msg2->title ? $msg2->title->getFullText() : null,
22  'title'
23  );
24  }
25 
30  public function testApiMessage() {
31  $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] );
32  $msg->inLanguage( 'de' )->title( Title::newMainPage() );
33  $msg2 = new ApiMessage( $msg, 'code', [ 'data' ] );
34  $this->compareMessages( $msg, $msg2 );
35  $this->assertEquals( 'code', $msg2->getApiCode() );
36  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
37 
38  $msg2 = unserialize( serialize( $msg2 ) );
39  $this->compareMessages( $msg, $msg2 );
40  $this->assertEquals( 'code', $msg2->getApiCode() );
41  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
42 
43  $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] );
44  $msg2 = new ApiMessage( [ [ 'foo', 'bar' ], 'baz' ], 'code', [ 'data' ] );
45  $this->compareMessages( $msg, $msg2 );
46  $this->assertEquals( 'code', $msg2->getApiCode() );
47  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
48 
49  $msg = new Message( 'foo' );
50  $msg2 = new ApiMessage( 'foo' );
51  $this->compareMessages( $msg, $msg2 );
52  $this->assertEquals( 'foo', $msg2->getApiCode() );
53  $this->assertEquals( [], $msg2->getApiData() );
54 
55  $msg2->setApiCode( 'code', [ 'data' ] );
56  $this->assertEquals( 'code', $msg2->getApiCode() );
57  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
58  $msg2->setApiCode( null );
59  $this->assertEquals( 'foo', $msg2->getApiCode() );
60  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
61  $msg2->setApiData( [ 'data2' ] );
62  $this->assertEquals( [ 'data2' ], $msg2->getApiData() );
63  }
64 
69  public function testApiRawMessage() {
70  $msg = new RawMessage( 'foo', [ 'baz' ] );
71  $msg->inLanguage( 'de' )->title( Title::newMainPage() );
72  $msg2 = new ApiRawMessage( $msg, 'code', [ 'data' ] );
73  $this->compareMessages( $msg, $msg2 );
74  $this->assertEquals( 'code', $msg2->getApiCode() );
75  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
76 
77  $msg2 = unserialize( serialize( $msg2 ) );
78  $this->compareMessages( $msg, $msg2 );
79  $this->assertEquals( 'code', $msg2->getApiCode() );
80  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
81 
82  $msg = new RawMessage( 'foo', [ 'baz' ] );
83  $msg2 = new ApiRawMessage( [ 'foo', 'baz' ], 'code', [ 'data' ] );
84  $this->compareMessages( $msg, $msg2 );
85  $this->assertEquals( 'code', $msg2->getApiCode() );
86  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
87 
88  $msg = new RawMessage( 'foo' );
89  $msg2 = new ApiRawMessage( 'foo', 'code', [ 'data' ] );
90  $this->compareMessages( $msg, $msg2 );
91  $this->assertEquals( 'code', $msg2->getApiCode() );
92  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
93 
94  $msg2->setApiCode( 'code', [ 'data' ] );
95  $this->assertEquals( 'code', $msg2->getApiCode() );
96  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
97  $msg2->setApiCode( null );
98  $this->assertEquals( 'foo', $msg2->getApiCode() );
99  $this->assertEquals( [ 'data' ], $msg2->getApiData() );
100  $msg2->setApiData( [ 'data2' ] );
101  $this->assertEquals( [ 'data2' ], $msg2->getApiData() );
102  }
103 
107  public function testApiMessageCreate() {
108  $this->assertInstanceOf( 'ApiMessage', ApiMessage::create( new Message( 'mainpage' ) ) );
109  $this->assertInstanceOf( 'ApiRawMessage', ApiMessage::create( new RawMessage( 'mainpage' ) ) );
110  $this->assertInstanceOf( 'ApiMessage', ApiMessage::create( 'mainpage' ) );
111 
112  $msg = new ApiMessage( 'mainpage' );
113  $this->assertSame( $msg, ApiMessage::create( $msg ) );
114 
115  $msg = new ApiRawMessage( 'mainpage' );
116  $this->assertSame( $msg, ApiMessage::create( $msg ) );
117  }
118 
119 }
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:556
testApiMessage()
ApiMessage ApiMessageTrait.
testApiMessageCreate()
ApiMessage::create.
unserialize($serialized)
Definition: ApiMessage.php:102
static create($msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:129
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:115
testApiRawMessage()
ApiRawMessage ApiMessageTrait.
compareMessages($msg, $msg2)
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Variant of the Message class.
Definition: Message.php:1242
Extension of RawMessage implementing IApiMessage.
Definition: ApiMessage.php:171
serialize()
Definition: ApiMessage.php:94
static newFromObject($object)
Return the same object, without access restrictions.