X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fapi%2FApiMessageTest.php;h=e405b3b89592ad57d7e69446857465c9a529b8e1;hb=4e6810e4a2c1d821d8d108c7974ac16917561764;hp=8764b4194f5b3e311a3d4f4f25ecc6a9967259e6;hpb=8b0e022a8c543a0cb2e515ae5a2e3ff35fd96b6f;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/api/ApiMessageTest.php b/tests/phpunit/includes/api/ApiMessageTest.php index 8764b4194f..e405b3b895 100644 --- a/tests/phpunit/includes/api/ApiMessageTest.php +++ b/tests/phpunit/includes/api/ApiMessageTest.php @@ -23,6 +23,56 @@ class ApiMessageTest extends MediaWikiTestCase { ); } + /** + * @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 ], + ]; + } + /** * @covers ApiMessage * @covers ApiMessageTrait @@ -105,14 +155,32 @@ class ApiMessageTest extends MediaWikiTestCase { * @covers ApiMessage::create */ public function testApiMessageCreate() { - $this->assertInstanceOf( 'ApiMessage', ApiMessage::create( new Message( 'mainpage' ) ) ); - $this->assertInstanceOf( 'ApiRawMessage', ApiMessage::create( new RawMessage( 'mainpage' ) ) ); - $this->assertInstanceOf( 'ApiMessage', ApiMessage::create( 'mainpage' ) ); + $this->assertInstanceOf( ApiMessage::class, ApiMessage::create( new Message( 'mainpage' ) ) ); + $this->assertInstanceOf( + ApiRawMessage::class, ApiMessage::create( new RawMessage( 'mainpage' ) ) + ); + $this->assertInstanceOf( ApiMessage::class, ApiMessage::create( 'mainpage' ) ); + + $msg = new ApiMessage( [ 'parentheses', 'foobar' ] ); + $msg2 = new Message( 'parentheses', [ 'foobar' ] ); - $msg = new ApiMessage( 'mainpage' ); $this->assertSame( $msg, ApiMessage::create( $msg ) ); + $this->assertEquals( $msg, ApiMessage::create( $msg2 ) ); + $this->assertEquals( $msg, ApiMessage::create( [ 'parentheses', 'foobar' ] ) ); + $this->assertEquals( $msg, + ApiMessage::create( [ 'message' => 'parentheses', 'params' => [ 'foobar' ] ] ) + ); + $this->assertSame( $msg, + ApiMessage::create( [ 'message' => $msg, 'params' => [ 'xxx' ] ] ) + ); + $this->assertEquals( $msg, + ApiMessage::create( [ 'message' => $msg2, 'params' => [ 'xxx' ] ] ) + ); + $this->assertSame( $msg, + ApiMessage::create( [ 'message' => $msg ] ) + ); - $msg = new ApiRawMessage( 'mainpage' ); + $msg = new ApiRawMessage( [ 'parentheses', 'foobar' ] ); $this->assertSame( $msg, ApiMessage::create( $msg ) ); }