+ /**
+ * @covers ApiMessageTrait
+ */
+ public function testCodeDefaults() {
+ $msg = new ApiMessage( 'foo' );
+ $this->assertSame( 'foo', $msg->getApiCode() );
+
+ $msg = new ApiMessage( 'apierror-bar' );
+ $this->assertSame( 'bar', $msg->getApiCode() );
+
+ $msg = new ApiMessage( 'apiwarn-baz' );
+ $this->assertSame( 'baz', $msg->getApiCode() );
+
+ // BC case
+ $msg = new ApiMessage( 'actionthrottledtext' );
+ $this->assertSame( 'ratelimited', $msg->getApiCode() );
+
+ $msg = new ApiMessage( [ 'apierror-missingparam', 'param' ] );
+ $this->assertSame( 'noparam', $msg->getApiCode() );
+ }
+
+ /**
+ * @covers ApiMessageTrait
+ * @dataProvider provideInvalidCode
+ * @param mixed $code
+ */
+ public function testInvalidCode( $code ) {
+ $msg = new ApiMessage( 'foo' );
+ try {
+ $msg->setApiCode( $code );
+ $this->fail( 'Expected exception not thrown' );
+ } catch ( InvalidArgumentException $ex ) {
+ $this->assertTrue( true );
+ }
+
+ try {
+ new ApiMessage( 'foo', $code );
+ $this->fail( 'Expected exception not thrown' );
+ } catch ( InvalidArgumentException $ex ) {
+ $this->assertTrue( true );
+ }
+ }
+
+ public static function provideInvalidCode() {
+ return [
+ [ '' ],
+ [ 42 ],
+ ];
+ }
+