From 42f822a949ea549751f4de810fd76bc9f4f5db01 Mon Sep 17 00:00:00 2001 From: "Aleksey Bekh-Ivanov (WMDE)" Date: Tue, 26 Sep 2017 14:01:30 +0200 Subject: [PATCH] Fix inability of fetching message object Fatal error was happening due to the fact that ApiUsageException was trying to call `getMessage()` on StatusValue which doesn't have this method. Change-Id: Idd9c7d47d9e24a6a32db6daf75a827bf958c9b76 --- includes/api/ApiUsageException.php | 2 +- .../includes/api/ApiUsageExceptionTest.php | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/includes/api/ApiUsageExceptionTest.php diff --git a/includes/api/ApiUsageException.php b/includes/api/ApiUsageException.php index 47902a75b0..4196add2c3 100644 --- a/includes/api/ApiUsageException.php +++ b/includes/api/ApiUsageException.php @@ -213,7 +213,7 @@ class ApiUsageException extends UsageException implements ILocalizedException { * @inheritDoc */ public function getMessageObject() { - return $this->status->getMessage(); + return Status::wrap( $this->status )->getMessage(); } /** diff --git a/tests/phpunit/includes/api/ApiUsageExceptionTest.php b/tests/phpunit/includes/api/ApiUsageExceptionTest.php new file mode 100644 index 0000000000..f901cf2d1c --- /dev/null +++ b/tests/phpunit/includes/api/ApiUsageExceptionTest.php @@ -0,0 +1,41 @@ +fatal( $messageKey, $messageParameter ); + + $apiUsageException = new ApiUsageException( null, $statusValue ); + /** @var \Message $gotMessage */ + $gotMessage = $apiUsageException->getMessageObject(); + + $this->assertInstanceOf( \Message::class, $gotMessage ); + $this->assertEquals( $messageKey, $gotMessage->getKey() ); + $this->assertEquals( [ $messageParameter ], $gotMessage->getParams() ); + } + + public function testNewWithMessage_ThenGetMessageObject_ReturnsApiMessageWithProvidedData() { + $expectedMessage = new Message( 'some-message-key', [ 'some message parameter' ] ); + $expectedCode = 'some-error-code'; + $expectedData = [ 'some-error-data' ]; + + $apiUsageException = ApiUsageException::newWithMessage( + null, + $expectedMessage, + $expectedCode, + $expectedData + ); + /** @var \ApiMessage $gotMessage */ + $gotMessage = $apiUsageException->getMessageObject(); + + $this->assertInstanceOf( \ApiMessage::class, $gotMessage ); + $this->assertEquals( $expectedMessage->getKey(), $gotMessage->getKey() ); + $this->assertEquals( $expectedMessage->getParams(), $gotMessage->getParams() ); + $this->assertEquals( $expectedCode, $gotMessage->getApiCode() ); + $this->assertEquals( $expectedData, $gotMessage->getApiData() ); + } + +} -- 2.20.1