From 1c542fb3ef83929ba5acf4d25a1747c14739bb18 Mon Sep 17 00:00:00 2001 From: Dieter Devlieghere Date: Mon, 30 May 2016 23:55:44 +0200 Subject: [PATCH] Implementing more response handler message checks #2 --- src/Amadeus/Client/ResponseHandler/Base.php | 188 +++++++++++++++--- src/Amadeus/Client/Result.php | 15 +- .../Client/Session/Handler/SendResult.php | 5 + .../Client/ResponseHandler/BaseTest.php | 72 +++++-- .../testfiles/dummyAirFlightInoResponse.txt | 104 ++++++++++ .../testfiles/pnrCancelDemoError.txt | 182 +++++++++++++++++ tests/Amadeus/ClientTest.php | 13 ++ 7 files changed, 520 insertions(+), 59 deletions(-) create mode 100644 tests/Amadeus/Client/ResponseHandler/testfiles/dummyAirFlightInoResponse.txt create mode 100644 tests/Amadeus/Client/ResponseHandler/testfiles/pnrCancelDemoError.txt diff --git a/src/Amadeus/Client/ResponseHandler/Base.php b/src/Amadeus/Client/ResponseHandler/Base.php index aa8fbea26..17fb49155 100644 --- a/src/Amadeus/Client/ResponseHandler/Base.php +++ b/src/Amadeus/Client/ResponseHandler/Base.php @@ -72,16 +72,89 @@ public function analyzeResponse($sendResult, $messageName) */ protected function analyzeSecurityAuthenticateResponse($response) { - return new Result($response); //TODO + return $this->analyzeSimpleResponseErrorCodeAndMessage($response); } /** - * Analysing a PNR_Reply + * Analysing a Security_Authenticate + * + * @param SendResult $response Security_Authenticate result + * @return Result + */ + protected function analyzeSecuritySignOutResponse($response) + { + return $this->analyzeSimpleResponseErrorCodeAndMessage($response); + } + + /** + * @param SendResult $response + * @return Result + */ + protected function analyzeAirFlightInfoResponse($response) + { + $analyzeResponse = new Result($response); + + $code = null; + $message = null; + + $domXpath = $this->makeDomXpath($response->responseXml); + + $categoryNodes = $domXpath->query('//m:responseError/m:errorInfo/m:errorDetails/m:errorCategory'); + if ($categoryNodes->length > 0) { + $analyzeResponse->status = $this->makeStatusFromErrorQualifier($categoryNodes->item(0)->nodeValue); + } + + $codeNodes = $domXpath->query('//m:responseError/m:errorInfo/m:errorDetails/m:errorCode'); + if ($codeNodes->length > 0) { + $code = $codeNodes->item(0)->nodeValue; + } + + $messageNodes = $domXpath->query('//m:responseError/m:interactiveFreeText/m:freeText'); + if ($messageNodes->length > 0) { + $message = $this->makeMessageFromMessagesNodeList($messageNodes); + } + + $analyzeResponse->messages[] = new Result\NotOk($code, $message); + + return $analyzeResponse; + } + + /** + * Analysing a PNR_Retrieve response * * @param SendResult $response PNR_Retrieve result * @return Result */ protected function analyzePnrRetrieveResponse($response) + { + return $this->analyzePnrReply($response); + } + + /** + * @param SendResult $response PNR_AddMultiElements result + * @return Result + */ + protected function analyzePnrAddMultiElementsResponse($response) + { + return $this->analyzePnrReply($response); + } + + /** + * @param SendResult $response PNR_Cancel result + * @return Result + */ + protected function analyzePnrCancelResponse($response) + { + return $this->analyzePnrReply($response); + } + + /** + * Analysing a PNR_Reply + * + * @param SendResult $response PNR_Retrieve result + * @return Result + */ + protected function analyzePnrReply($response) { $analyzeResponse = new Result($response); @@ -100,7 +173,7 @@ protected function analyzePnrRetrieveResponse($response) $errorTextNodeList = $domXpath->query($queryAllErrorMsg); $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList); - $analyzeResponse->errors[] = new Result\NotOk($code, trim($message), 'general'); + $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'general'); } //Segment errors: @@ -116,7 +189,7 @@ protected function analyzePnrRetrieveResponse($response) $errorTextNodeList = $domXpath->query($querySegmentErrorMsg); $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList); - $analyzeResponse->errors[] = new Result\NotOk($code, trim($message), 'segment'); + $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'segment'); } //Element errors: @@ -133,22 +206,13 @@ protected function analyzePnrRetrieveResponse($response) $errorTextNodeList = $domXpath->query($queryElementErrorMsg); $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList); - $analyzeResponse->errors[] = new Result\NotOk($code, trim($message), 'element'); + $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'element'); } return $analyzeResponse; } - /** - * @param SendResult $response PNR_AddMultiElements result - * @return Result - */ - protected function analyzePnrAddMultiElementsResponse($response) - { - return $this->analyzePnrRetrieveResponse($response); - } - /** * @param SendResult $response Queue_List result * @return Result @@ -158,8 +222,7 @@ protected function analyzeQueueListResponse($response) { $analysisResponse = new Result($response); - $domDoc = new \DOMDocument('1.0', 'UTF-8'); - $domDoc->loadXML($response->responseXml); + $domDoc = $this->loadDomDocument($response->responseXml); $errorCodeNode = $domDoc->getElementsByTagName("errorCode")->item(0); @@ -169,12 +232,39 @@ protected function analyzeQueueListResponse($response) $errorCode = $errorCodeNode->nodeValue; $errorMessage = $this->getErrorTextFromQueueErrorCode($errorCode); - $analysisResponse->warnings[] = new Result\NotOk($errorCode, $errorMessage); + $analysisResponse->messages[] = new Result\NotOk($errorCode, $errorMessage); } return $analysisResponse; } + /** + * @param SendResult $response WebService message Send Result + * @return Result + * @throws Exception + */ + protected function analyzeSimpleResponseErrorCodeAndMessage($response) + { + $analyzeResponse = new Result($response); + + $domDoc = $this->loadDomDocument($response->responseXml); + + $errorCodeNode = $domDoc->getElementsByTagName("errorCode")->item(0); + + if (!is_null($errorCodeNode)) { + $analyzeResponse->status = Result::STATUS_WARN; + $errorCode = $errorCodeNode->nodeValue; + $errorTextNodeList = $domDoc->getElementsByTagName("freeText"); + + $analyzeResponse->messages[] = new Result\NotOk( + $errorCode, + $this->makeMessageFromMessagesNodeList($errorTextNodeList) + ); + } + + return $analyzeResponse; + } + /** * Returns the errortext from a Queue_*Reply errorcode * @@ -212,28 +302,70 @@ protected function getErrorTextFromQueueErrorCode($errorCode) return $errorMessage; } + /** + * @param string $response + * @return \DOMDocument + * @throws Exception when there's a problem loading the message + */ + protected function loadDomDocument($response) + { + $domDoc = new \DOMDocument('1.0', 'UTF-8'); + + $loadResult = $domDoc->loadXML($response); + if ($loadResult === false) { + throw new Exception('Could not load response message into DOMDocument'); + } + + return $domDoc; + } + + /** + * @param $qualifier + * @return string Result::STATUS_* + */ + protected function makeStatusFromErrorQualifier($qualifier) + { + $status = null; + + switch ($qualifier) { + case 'INF': + $status = Result::STATUS_INFO; + break; + case 'WEC': + case 'WZZ': //Mutually defined warning + $status = Result::STATUS_WARN; + break; + case 'EC': + $status = Result::STATUS_ERROR; + break; + case 'ZZZ': //Mutually defined + default: + $status = Result::STATUS_UNKNOWN; + break; + } + + return $status; + } + /** * Make a Xpath-queryable object for an XML string * + * registers TNS namespace with prefix self::XMLNS_PREFIX + * * @param string $response * @return \DOMXPath * @throws Exception when there's a problem loading the message */ protected function makeDomXpath($response) { - $domDoc = new \DOMDocument('1.0', 'UTF-8'); - $domXpath = null; - $loadResult = $domDoc->loadXML($response); - - if ($loadResult === true) { - $uri = $domDoc->documentElement->lookupNamespaceUri(null); + $domDoc = $this->loadDomDocument($response); + $domXpath = new \DOMXPath($domDoc); - $domXpath = new \DOMXPath($domDoc); - $domXpath->registerNamespace(self::XMLNS_PREFIX, $uri); - } else { - throw new Exception('Could not load response message into DOMDocument'); - } + $domXpath->registerNamespace( + self::XMLNS_PREFIX, + $domDoc->documentElement->lookupNamespaceUri(null) + ); return $domXpath; } diff --git a/src/Amadeus/Client/Result.php b/src/Amadeus/Client/Result.php index 636adb33f..2d32d5043 100644 --- a/src/Amadeus/Client/Result.php +++ b/src/Amadeus/Client/Result.php @@ -37,6 +37,10 @@ class Result * Status indicator for a success situation */ const STATUS_OK = 'OK'; + /** + * Status indicator for an informational message situation. + */ + const STATUS_INFO = 'INFO'; /** * Status indicator for a warning situation. */ @@ -60,18 +64,11 @@ class Result public $status; /** - * Array of warnings found - * - * @var NotOk[] - */ - public $warnings = []; - - /** - * Array of errors found + * Array of errors or warnings found * * @var NotOk[] */ - public $errors = []; + public $messages = []; /** * The actual result received after performing the web service call. diff --git a/src/Amadeus/Client/Session/Handler/SendResult.php b/src/Amadeus/Client/Session/Handler/SendResult.php index f90151de9..0e8a16f08 100644 --- a/src/Amadeus/Client/Session/Handler/SendResult.php +++ b/src/Amadeus/Client/Session/Handler/SendResult.php @@ -31,10 +31,15 @@ class SendResult { /** + * The response as an XML string + * * @var string */ public $responseXml; + /** + * The response as returned by PHP's \SoapClient + * * @var \stdClass|array */ public $responseObject; diff --git a/tests/Amadeus/Client/ResponseHandler/BaseTest.php b/tests/Amadeus/Client/ResponseHandler/BaseTest.php index ec2ef8db4..7d1193ba9 100644 --- a/tests/Amadeus/Client/ResponseHandler/BaseTest.php +++ b/tests/Amadeus/Client/ResponseHandler/BaseTest.php @@ -45,10 +45,10 @@ public function testCanFindSimultaneousChangesErrorMessageInPnrReply() $result = $respHandler->analyzeResponse($sendResult, 'PNR_AddMultiElements'); $this->assertEquals(Result::STATUS_ERROR, $result->status); - $this->assertEquals(1, count($result->errors)); - $this->assertEquals('8111', $result->errors[0]->code); - $this->assertEquals("SIMULTANEOUS CHANGES TO PNR - USE WRA/RT TO PRINT OR IGNORE", $result->errors[0]->text); - $this->assertEquals('general', $result->errors[0]->level); + $this->assertEquals(1, count($result->messages)); + $this->assertEquals('8111', $result->messages[0]->code); + $this->assertEquals("SIMULTANEOUS CHANGES TO PNR - USE WRA/RT TO PRINT OR IGNORE", $result->messages[0]->text); + $this->assertEquals('general', $result->messages[0]->level); } public function testCanFindTopLevelErrorMessageInPnrReply() @@ -61,10 +61,10 @@ public function testCanFindTopLevelErrorMessageInPnrReply() $result = $respHandler->analyzeResponse($sendResult, 'PNR_AddMultiElements'); $this->assertEquals(Result::STATUS_ERROR, $result->status); - $this->assertEquals(1, count($result->errors)); - $this->assertEquals('102', $result->errors[0]->code); - $this->assertEquals("CHECK DATE", $result->errors[0]->text); - $this->assertEquals('general', $result->errors[0]->level); + $this->assertEquals(1, count($result->messages)); + $this->assertEquals('102', $result->messages[0]->code); + $this->assertEquals("CHECK DATE", $result->messages[0]->text); + $this->assertEquals('general', $result->messages[0]->level); } public function testCanFindSegmentLevelErrorMessageInPnrReply() @@ -77,10 +77,10 @@ public function testCanFindSegmentLevelErrorMessageInPnrReply() $result = $respHandler->analyzeResponse($sendResult, 'PNR_AddMultiElements'); $this->assertEquals(Result::STATUS_ERROR, $result->status); - $this->assertEquals(1, count($result->errors)); - $this->assertEquals('102', $result->errors[0]->code); - $this->assertEquals("CHECK DATE", $result->errors[0]->text); - $this->assertEquals('segment', $result->errors[0]->level); + $this->assertEquals(1, count($result->messages)); + $this->assertEquals('102', $result->messages[0]->code); + $this->assertEquals("CHECK DATE", $result->messages[0]->text); + $this->assertEquals('segment', $result->messages[0]->level); } public function testCanFindElementLevelErrorMessageInPnrReply() @@ -93,10 +93,23 @@ public function testCanFindElementLevelErrorMessageInPnrReply() $result = $respHandler->analyzeResponse($sendResult, 'PNR_AddMultiElements'); $this->assertEquals(Result::STATUS_ERROR, $result->status); - $this->assertEquals(1, count($result->errors)); - $this->assertEquals('4498', $result->errors[0]->code); - $this->assertEquals("COMBINATION OF ELEMENTS NOT ALLOWED", $result->errors[0]->text); - $this->assertEquals('element', $result->errors[0]->level); + $this->assertEquals(1, count($result->messages)); + $this->assertEquals('4498', $result->messages[0]->code); + $this->assertEquals("COMBINATION OF ELEMENTS NOT ALLOWED", $result->messages[0]->text); + $this->assertEquals('element', $result->messages[0]->level); + } + + public function testCanFindErrorInPnrCancel() + { + $respHandler = new ResponseHandler\Base(); + + $sendResult = new SendResult(); + $sendResult->responseXml = $this->getTestFile('pnrCancelDemoError.txt'); + + $result = $respHandler->analyzeResponse($sendResult, 'PNR_Cancel'); + + $this->assertEquals(Result::STATUS_OK, $result->status); + $this->assertEquals(0, count($result->messages)); } public function testCanSetWarningStatusForEmptyQueue() @@ -109,9 +122,9 @@ public function testCanSetWarningStatusForEmptyQueue() $result = $respHandler->analyzeResponse($sendResult, 'Queue_List'); $this->assertEquals(Result::STATUS_WARN, $result->status); - $this->assertEquals(1, count($result->warnings)); - $this->assertEquals(926, $result->warnings[0]->code); - $this->assertEquals("Queue category empty", $result->warnings[0]->text); + $this->assertEquals(1, count($result->messages)); + $this->assertEquals(926, $result->messages[0]->code); + $this->assertEquals("Queue category empty", $result->messages[0]->text); } public function testWillSetGenericWarningForUnknownError() @@ -124,9 +137,9 @@ public function testWillSetGenericWarningForUnknownError() $result = $respHandler->analyzeResponse($sendResult, 'Queue_List'); $this->assertEquals(Result::STATUS_WARN, $result->status); - $this->assertEquals(1, count($result->warnings)); - $this->assertEquals(666, $result->warnings[0]->code); - $this->assertEquals("QUEUE ERROR '666' (Error message unavailable)", $result->warnings[0]->text); + $this->assertEquals(1, count($result->messages)); + $this->assertEquals(666, $result->messages[0]->code); + $this->assertEquals("QUEUE ERROR '666' (Error message unavailable)", $result->messages[0]->text); } public function testWillReturnUnknownStatusWhenHandlingResponseFromUnknownMessage() @@ -140,4 +153,19 @@ public function testWillReturnUnknownStatusWhenHandlingResponseFromUnknownMessag $this->assertEquals(Result::STATUS_UNKNOWN, $result->status); } + + public function testCanFindAirFlightInfoError() + { + $respHandler = new ResponseHandler\Base(); + + $sendResult = new SendResult(); + $sendResult->responseXml = $this->getTestFile('dummyAirFlightInoResponse.txt'); + + $result = $respHandler->analyzeResponse($sendResult, 'Air_FlightInfo'); + + $this->assertEquals(Result::STATUS_INFO, $result->status); + $this->assertEquals(1, count($result->messages)); + $this->assertEquals('AUE', $result->messages[0]->code); + $this->assertEquals("FLIGHT CANCELLED", $result->messages[0]->text); + } } diff --git a/tests/Amadeus/Client/ResponseHandler/testfiles/dummyAirFlightInoResponse.txt b/tests/Amadeus/Client/ResponseHandler/testfiles/dummyAirFlightInoResponse.txt new file mode 100644 index 000000000..f06cb67ff --- /dev/null +++ b/tests/Amadeus/Client/ResponseHandler/testfiles/dummyAirFlightInoResponse.txt @@ -0,0 +1,104 @@ + + + + 1 + 82 + 3 + + + + + + AUE + INF + 1A + + + + + 1 + + FLIGHT CANCELLED + + + + + + + 241014 + + + NCE + + + CDG + + + 6X + + + 121 + + + + + 0 + 5 + + + 0200 + + + + + SIM + + NCE CDG - ET/ ELECTRONIC TKT CANDIDATE + + + + + 241014 + 1200 + + + NCE + + + + + 320 + 0200 + + + + + J + + + D + + + I + + + U + + + Y + + + + + + + 241014 + 1400 + + + CDG + + + + + \ No newline at end of file diff --git a/tests/Amadeus/Client/ResponseHandler/testfiles/pnrCancelDemoError.txt b/tests/Amadeus/Client/ResponseHandler/testfiles/pnrCancelDemoError.txt new file mode 100644 index 000000000..94272efbb --- /dev/null +++ b/tests/Amadeus/Client/ResponseHandler/testfiles/pnrCancelDemoError.txt @@ -0,0 +1,182 @@ + + + + + 1A + YMUQL9 + 300516 + + + + + + + RP + WSSU + BRUXXXXXX + 00081900 + + + BRUXXXXXX + + EHD + + BRUXXXXXX + 9991WS + 300516 + 00081900 + 2133 + + + + + + 00081900 + BRUXXXXXX + + T + + + + 1A + BRU + + + + + BE + + + + + + + 00081900 + BRUXXXXXX + + T + + + + 1A + BRU + + + + + BE + + + + + + + 00081900 + BRUXXXXXX + + T + + + + 1A + BRU + + + + + BE + + + + + + + 2 + + + + 1 + + + + 2016 + 10 + 22 + + + + + + + PT + 2 + + NM + 1 + + + + + BOWIE + 1 + + + DAVID + ADT + + + + + + + 1 + ADT + + + UN + Y + Y + BOWIE + DAVID + + + + + + + + + + OT + 1 + + AP + 2 + + + + 3 + 7 + + +32222222222222 + + + + + + OT + 17 + + TK + 3 + + + + OK + 300516 + BRUXXXXXX + + + + + diff --git a/tests/Amadeus/ClientTest.php b/tests/Amadeus/ClientTest.php index 2495ef1df..29c424bcd 100644 --- a/tests/Amadeus/ClientTest.php +++ b/tests/Amadeus/ClientTest.php @@ -1830,6 +1830,19 @@ public function testCanGetSessionInfo() $this->assertEquals($mockedSession, $actual); } + public function testWillGetErrorOnInvalidSessionHandlerParams() + { + $this->setExpectedException('InvalidArgumentException', 'Invalid parameters'); + $par = new Params(); + $par->requestCreatorParams = new Params\RequestCreatorParams([ + 'receivedFrom' => 'some RF string', + 'originatorOfficeId' => 'BRUXXXXXX' + ]); + + $client = new Client($par); + + $client->airFlightInfo(); + } /** * @return array