/** * Test handling exception during dispatch. */ public function testDispatchWithException() { $this->_appStateMock->expects($this->any())->method('isInstalled')->will($this->returnValue(true)); $exceptionMessage = 'some error message'; $exception = new \Magento\Webapi\Exception($exceptionMessage); $this->_soapServerMock->expects($this->any())->method('handle')->will($this->throwException($exception)); $this->_errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnValue($exception)); $encoding = "utf-8"; $this->_soapServerMock->expects($this->any())->method('getApiCharset')->will($this->returnValue($encoding)); $this->_soapController->dispatch($this->_requestMock); $expectedMessage = <<<EXPECTED_MESSAGE <?xml version="1.0" encoding="{$encoding}"?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" > <env:Body> <env:Fault> <env:Code> <env:Value>env:Sender</env:Value> </env:Code> <env:Reason> <env:Text xml:lang="en">some error message</env:Text> </env:Reason> </env:Fault> </env:Body> </env:Envelope> EXPECTED_MESSAGE; $this->assertXmlStringEqualsXmlString($expectedMessage, $this->_responseMock->getBody()); }
/** * Test sendResponse method with exception rendering. */ public function testSendResponseWithException() { /** Mock all required objects. */ $this->_rendererMock->expects($this->any())->method('getMimeType')->will($this->returnValue('application/json')); $this->_rendererMock->expects($this->any())->method('render')->will($this->returnCallback([$this, 'callbackForSendResponseTest'], $this->returnArgument(0))); $exceptionMessage = 'Message'; $exceptionHttpCode = \Magento\Webapi\Exception::HTTP_BAD_REQUEST; $exception = new \Magento\Webapi\Exception($exceptionMessage, 0, $exceptionHttpCode); $this->_errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnValue($exception)); $this->_responseRest->setException($exception); /** Start output buffering. */ ob_start(); $this->_responseRest->sendResponse(); /** Clear output buffering. */ ob_end_clean(); $actualResponse = $this->_responseRest->getBody(); $expectedResult = '{"message":"' . $exceptionMessage . '"}'; $this->assertStringStartsWith($expectedResult, $actualResponse, 'Response body is invalid'); }