Skip to content

Commit

Permalink
Further implementation of error checks in ResponseHandler - do not th…
Browse files Browse the repository at this point in the history
…row soapfaults anymore - provide message version in Session Handler SendResult - #2
  • Loading branch information
DerMika committed May 31, 2016
1 parent 1c542fb commit fe20c14
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 10 deletions.
44 changes: 42 additions & 2 deletions src/Amadeus/Client/ResponseHandler/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public function analyzeResponse($sendResult, $messageName)
{
$methodName = 'analyze' . str_replace('_', '', ucfirst($messageName)).'Response';

if (method_exists($this, $methodName)) {
if (!empty($sendResult->exception)) {
return $this->makeResultForException($sendResult);
} elseif (method_exists($this, $methodName)) {
return $this->$methodName(
$sendResult
);
Expand Down Expand Up @@ -252,7 +254,9 @@ protected function analyzeSimpleResponseErrorCodeAndMessage($response)
$errorCodeNode = $domDoc->getElementsByTagName("errorCode")->item(0);

if (!is_null($errorCodeNode)) {
$analyzeResponse->status = Result::STATUS_WARN;
$errorCatNode = $domDoc->getElementsByTagName("errorCategory")->item(0);
$analyzeResponse->status = $this->makeStatusFromErrorQualifier($errorCatNode->nodeValue);

$errorCode = $errorCodeNode->nodeValue;
$errorTextNodeList = $domDoc->getElementsByTagName("freeText");

Expand Down Expand Up @@ -388,4 +392,40 @@ function($item) {
)
);
}

/**
* @param SendResult $sendResult
* @return Result
*/
protected function makeResultForException($sendResult)
{
$result = new Result($sendResult, Result::STATUS_FATAL);

$result->messages[] = $this->makeMessageFromException($sendResult->exception);

return $result;
}

/**
* @param \Exception $exception
* @return Result\NotOk
* @throws Exception
*/
protected function makeMessageFromException(\Exception $exception)
{
$message = new Result\NotOk();

if ($exception instanceof \SoapFault) {
$info = explode('|', $exception->getMessage());
$message->code = $info[0];
if (count($info) === 3) {
$message->level = $info[1];
$message->text = $info[2];
}
} else {
throw new Exception('Did not implement other exceptions than soapfaults');
}

return $message;
}
}
4 changes: 4 additions & 0 deletions src/Amadeus/Client/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Result
* Status indicator for an error response.
*/
const STATUS_ERROR = 'ERR';
/**
* Status indicator for a FATAL error response.
*/
const STATUS_FATAL = 'FATAL';
/**
* Status indicator for a response which could not be checked for warnings/errors.
*/
Expand Down
45 changes: 42 additions & 3 deletions src/Amadeus/Client/Session/Handler/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ abstract class Base implements HandlerInterface, LoggerAwareInterface
*/
protected $wsdlDomXpath;

/**
* @var array
*/
protected $messagesAndVersions;

/**
* Get the session parameters of the active session
*
Expand Down Expand Up @@ -197,7 +202,9 @@ public function __construct(SessionHandlerParams $params)
*/
public function sendMessage($messageName, Client\Struct\BaseWsMessage $messageBody, $messageOptions = [])
{
$result = new SendResult();
$result = new SendResult(
$this->getActiveVersionFor($messageName)
);

$this->prepareForNextMessage($messageName, $messageOptions);

Expand All @@ -216,9 +223,10 @@ public function sendMessage($messageName, Client\Struct\BaseWsMessage $messageBo
" line " . $ex->getLine() . ": \n" . $ex->getTraceAsString()
);
$this->logRequestAndResponse($messageName);
//TODO We must be able to handle certain soapfaults inside the client, so maybe pass through after logging?
throw $ex;
$result->exception = $ex;
} catch (\Exception $ex) {
// We should only come here when the XSL extension is not enabled or the XSLT transformation file
// is unreadable
$this->log(
LogLevel::ERROR,
"EXCEPTION while sending message " . $messageName . ": " .
Expand Down Expand Up @@ -294,6 +302,20 @@ public function getOriginatorOffice()
* @return array
*/
public function getMessagesAndVersions()
{
if (empty($this->messagesAndVersions)) {
$this->messagesAndVersions = $this->loadMessagesAndVersions();
}

return $this->messagesAndVersions;
}

/**
* Loads messages & versions from WSDL.
*
* @return array
*/
protected function loadMessagesAndVersions()
{
$this->loadWsdlXpath($this->params->wsdl);

Expand Down Expand Up @@ -355,6 +377,23 @@ protected function loadWsdlXpath($wsdlFilePath)
}
}

/**
* Get the version number active in the WSDL for the given message
*
* @param $messageName
* @return float|string|null
*/
protected function getActiveVersionFor($messageName)
{
$msgAndVer = $this->getMessagesAndVersions();

if (isset($msgAndVer[$messageName])) {
return $msgAndVer[$messageName];
}

return null;
}


/**
* @return \SoapClient
Expand Down
18 changes: 18 additions & 0 deletions src/Amadeus/Client/Session/Handler/SendResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
/**
* SendResult
*
* An object used to return the result of the Session Handler sendMessage()
*
* @package Amadeus\Client\Session\Handler
* @author Dieter Devlieghere <dieter.devlieghere@benelux.amadeus.com>
*/
Expand All @@ -50,4 +52,20 @@ class SendResult
* @var string
*/
public $messageVersion;

/**
* Exception that occurred while sending
* @var \Exception
*/
public $exception;

/**
* SendResult constructor.
*
* @param string|float|null $messageVersion
*/
public function __construct($messageVersion = null)
{
$this->messageVersion = $messageVersion;
}
}
67 changes: 67 additions & 0 deletions tests/Amadeus/Client/ResponseHandler/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,71 @@ public function testCanFindAirFlightInfoError()
$this->assertEquals('AUE', $result->messages[0]->code);
$this->assertEquals("FLIGHT CANCELLED", $result->messages[0]->text);
}

public function testCanParseSecurityAuthenticateReplyOk()
{
$respHandler = new ResponseHandler\Base();

$sendResult = new SendResult();
$sendResult->responseXml = $this->getTestFile('dummySecurityAuthenticateReply.txt');
$sendResult->messageVersion = '6.1';

$result = $respHandler->analyzeResponse($sendResult, 'Security_Authenticate');

$this->assertEquals(Result::STATUS_OK, $result->status);
$this->assertEquals(0, count($result->messages));
}

public function testCanParseSecurityAuthenticateReplyErr()
{
$respHandler = new ResponseHandler\Base();

$sendResult = new SendResult();
$sendResult->responseXml = $this->getTestFile('dummySecurityAuthenticateReplyError.txt');
$sendResult->messageVersion = '6.1';

$result = $respHandler->analyzeResponse($sendResult, 'Security_Authenticate');

$this->assertEquals(Result::STATUS_ERROR, $result->status);
$this->assertEquals(1, count($result->messages));
$this->assertEquals('You are not authorized to login in this area.', $result->messages[0]->text);
$this->assertEquals('', $result->messages[0]->level);
$this->assertEquals('16199', $result->messages[0]->code);
}

public function testCanHandleSoapFault()
{
$respHandler = new ResponseHandler\Base();

$sendResult = new SendResult();
$sendResult->responseXml = $this->getTestFile('dummySoapFault.txt');
$sendResult->messageVersion = '14.2';
$sendResult->exception = new \SoapFault("Sender", "1929|Application|INVALID RECORD LOCATOR");

$result = $respHandler->analyzeResponse($sendResult, 'PNR_Retrieve');

$this->assertEquals(Result::STATUS_FATAL, $result->status);
$this->assertEquals(1, count($result->messages));
$this->assertEquals('1929', $result->messages[0]->code);
$this->assertEquals("INVALID RECORD LOCATOR", $result->messages[0]->text);
$this->assertEquals("Application", $result->messages[0]->level);
}

public function testCanHandleSoapFaultSession()
{
$respHandler = new ResponseHandler\Base();

$sendResult = new SendResult();
$sendResult->responseXml = $this->getTestFile('dummySoapFaultSession.txt');
$sendResult->messageVersion = '14.2';
$sendResult->exception = new \SoapFault("Sender", "11|Session|");

$result = $respHandler->analyzeResponse($sendResult, 'PNR_Retrieve');

$this->assertEquals(Result::STATUS_FATAL, $result->status);
$this->assertEquals(1, count($result->messages));
$this->assertEquals('11', $result->messages[0]->code);
$this->assertEquals("Session", $result->messages[0]->level);
$this->assertEquals('', $result->messages[0]->text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Security_AuthenticateReply xmlns="http://xml.amadeus.com/VLSSLR_06_1_1A"><processStatus><statusCode>P</statusCode></processStatus></Security_AuthenticateReply>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Security_AuthenticateReply xmlns="http://xml.amadeus.com/VLSSLR_06_1_1A">
<errorSection>
<applicationError>
<errorDetails>
<errorCode>16199</errorCode>
<errorCategory>EC</errorCategory>
<errorCodeOwner>APPLICATION1</errorCodeOwner>
</errorDetails>
</applicationError>
<interactiveFreeText>
<freeTextQualification>
<textSubjectQualifier>3</textSubjectQualifier>
<informationType>TXT</informationType>
<language>EN</language>
</freeTextQualification>
<freeText>You are not authorized to login in this area.</freeText>
</interactiveFreeText>
</errorSection>
</Security_AuthenticateReply>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<soap:Fault xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>soap:Server</faultcode><faultstring>1929|Application|INVALID RECORD LOCATOR</faultstring><faultactor>SI:Backend</faultactor></soap:Fault>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<soap:Fault><faultcode>soap:Client</faultcode><faultstring> 11|Session|</faultstring></soap:Fault>
1 change: 1 addition & 0 deletions tests/Amadeus/Client/Session/Handler/SoapHeader2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public function testCanSendAuthCallAndStartSession()
$messageResult = new SendResult();
$messageResult->responseObject = $wsResponse;
$messageResult->responseXml = Client\Util\MsgBodyExtractor::extract($dummyReply);
$messageResult->messageVersion = '6.1';


$overrideSoapClient
Expand Down
17 changes: 12 additions & 5 deletions tests/Amadeus/Client/Session/Handler/SoapHeader4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,14 @@ public function testCanSendMessage()
$expectedResult = new Client\Session\Handler\SendResult();
$expectedResult->responseXml = $dummyPnrReplyExtractedMessage;
$expectedResult->responseObject = new \stdClass();
$expectedResult->messageVersion = '11.3';


$this->assertEquals($expectedResult, $messageResponse);
}

public function testCanHandleMessageThrowingSoapFault()
public function testCanHandleMessageWithSoapFault()
{
$this->setExpectedException('\SoapFault');

$overrideSoapClient = $this->getMock(
'Amadeus\Client\SoapClient',
['__getLastRequest', '__getLastResponse', 'PNR_Retrieve'],
Expand All @@ -483,7 +482,7 @@ public function testCanHandleMessageThrowingSoapFault()
$overrideSoapClient
->expects($this->any())
->method('PNR_Retrieve')
->will($this->throwException(new \SoapFault("Sender", "SECURED PNR")));
->will($this->throwException(new \SoapFault("Sender", "284|Application|SECURED PNR")));

$sessionHandlerParams = $this->makeSessionHandlerParams($overrideSoapClient);
$sessionHandler = new SoapHeader4($sessionHandlerParams);
Expand All @@ -493,11 +492,17 @@ public function testCanHandleMessageThrowingSoapFault()
'ABC123'
);

$sessionHandler->sendMessage(
$sendResult = $sessionHandler->sendMessage(
'PNR_Retrieve',
$pnrRetrieveMessage,
['asString'=>true,'endSession'=>false]
);

$this->assertInstanceOf('Amadeus\Client\Session\Handler\SendResult', $sendResult);
$this->assertInstanceOf('\SoapFault', $sendResult->exception);
$this->assertEquals('284|Application|SECURED PNR', $sendResult->exception->getMessage());
$this->assertEquals('11.3', $sendResult->messageVersion);
$this->assertEquals(Client\Util\MsgBodyExtractor::extract($dummyPnrReply), $sendResult->responseXml);
}

public function testCanHandleMessageThrowingNonSoapFaultException()
Expand Down Expand Up @@ -590,6 +595,7 @@ public function testCanExtractSessionDataAfterCall()
$expectedResult = new Client\Session\Handler\SendResult();
$expectedResult->responseXml = $this->getTestFile('acspnrreply.xml');
$expectedResult->responseObject = $this->getTestFile('acspnr.xml');
$expectedResult->messageVersion = '11.3';

$this->assertEquals($expectedResult, $messageResponse);

Expand Down Expand Up @@ -689,6 +695,7 @@ public function testCanMakeSessionHandlerWithoutLogger()
$expectedResult = new Client\Session\Handler\SendResult();
$expectedResult->responseXml = $dummyPnrReplyExtractedMessage;
$expectedResult->responseObject = new \stdClass();
$expectedResult->messageVersion = '11.3';

$this->assertEquals($expectedResult, $messageResponse);
}
Expand Down

0 comments on commit fe20c14

Please sign in to comment.