pull/11/merge
Konstantin Pereiaslov 4 years ago committed by GitHub
commit 8338d96d92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,28 @@
<?php
namespace pdt256\Shipping\Fedex;
use pdt256\Shipping\RateRequest\RequestException;
class FedexRequestException extends RequestException
{
/**
* @var string
*/
protected $severity;
/**
* @return string
*/
public function getSeverity()
{
return $this->severity;
}
/**
* @param string $severity
*/
public function setSeverity($severity)
{
$this->severity = $severity;
}
}

@ -196,20 +196,68 @@ class Rate extends RateAdapter
return $this;
}
protected function process()
/**
* @param $response
* @return FedexRequestException
*/
protected function getIncorrectResponseException($response)
{
try {
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadXml($this->response);
$rate_reply = $dom->getElementsByTagName('RateReplyDetails');
return new FedexRequestException('Incorrect response received from FedEx: ' . $response);
}
if (empty($rate_reply->length)) {
throw new Exception('Unable to get FedEx Rates.');
/**
* @param string $response
* @return FedexRequestException
*/
protected function getExceptionFromResponse($response)
{
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadXml($response);
$notifications = $dom->getElementsByTagName('Notifications');
foreach ($notifications as $notification) {
$messages = $notification->getElementsByTagName('Message');
if ($messages->length === 0) {
break;
}
foreach ($messages as $message) {
$exceptionMessage = $message->textContent;
break;
}
$codes = $notification->getElementsByTagName('Code');
if ($codes->length === 0) {
break;
}
foreach ($codes as $code) {
$exceptionCode = $code->textContent;
break;
}
} catch (Exception $e) {
// StatsD::increment('error.shipping.get_fedex_rate');
// Kohana::$log->add(Log::ERROR, $e)->write();
throw $e;
$severities = $notification->getElementsByTagName('Severity');
if ($severities->length === 0) {
break;
}
foreach ($severities as $severity) {
$exceptionSeverity = $severity->textContent;
break;
}
if (!isset($exceptionMessage) || !isset($exceptionCode) || !isset($exceptionSeverity)) {
return $this->getIncorrectResponseException($response);
}
$exception = new FedexRequestException($exceptionMessage, $exceptionCode);
$exception->setSeverity($exceptionSeverity);
return $exception;
}
return $this->getIncorrectResponseException($response);
}
protected function process()
{
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadXml($this->response);
$rate_reply = $dom->getElementsByTagName('RateReplyDetails');
if (empty($rate_reply->length)) {
throw $this->getExceptionFromResponse($this->response);
}
foreach ($rate_reply as $rate) {

@ -0,0 +1 @@
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><RateReply xmlns="http://fedex.com/ws/rate/v13"><HighestSeverity>WARNING</HighestSeverity><Notifications><Severity>WARNING</Severity><Source>crs</Source><Code>556</Code><Message>There are no valid services available. </Message><LocalizedMessage>There are no valid services available. </LocalizedMessage></Notifications><Version><ServiceId>crs</ServiceId><Major>13</Major><Intermediate>0</Intermediate><Minor>0</Minor></Version></RateReply></SOAP-ENV:Body></SOAP-ENV:Envelope>

@ -0,0 +1,14 @@
<?php
namespace pdt256\Shipping\RateRequest;
class StubFailingFedex extends StubFedex
{
public function execute($url, $data = null)
{
if ($this->artificialDelay > 0) {
sleep($this->artificialDelay);
}
return file_get_contents(__DIR__ . '/FedexErrorResponse.xml');
}
}

@ -3,7 +3,7 @@ namespace pdt256\Shipping\RateRequest;
class StubFedex extends Adapter
{
private $artificialDelay = 0;
protected $artificialDelay = 0;
public function __construct($artificial_delay = 0)
{

@ -0,0 +1,21 @@
<?php
namespace pdt256\Shipping\RateRequest;
class StubIncorrectResponseAdapter extends Adapter
{
protected $artificialDelay = 0;
public function __construct($artificial_delay = 0)
{
$this->artificialDelay = $artificial_delay;
}
public function execute($url, $data = null)
{
if ($this->artificialDelay > 0) {
sleep($this->artificialDelay);
}
return '<html/>';
}
}

@ -1,7 +1,9 @@
<?php
namespace pdt256\Shipping\Fedex;
use pdt256\Shipping\RateRequest\StubFailingFedex;
use pdt256\Shipping\RateRequest\StubFedex;
use pdt256\Shipping\RateRequest\StubIncorrectResponseAdapter;
use pdt256\Shipping\Ship;
use pdt256\Shipping\Package;
use pdt256\Shipping\Shipment;
@ -89,6 +91,51 @@ class RateTest extends TestCase
$this->assertEquals($expected, $rates);
}
public function testFail()
{
$rateAdapter = new Rate([
'prod' => false,
'key' => 'XXX',
'password' => 'XXX',
'accountNumber' => 'XXX',
'meterNumber' => 'XXX',
'dropOffType' => 'BUSINESS_SERVICE_CENTER',
'shipment' => $this->shipment,
'approvedCodes' => $this->approvedCodes,
'requestAdapter' => new StubFailingFedex(),
]);
try {
$rateAdapter->getRates();
$this->fail('Getting error from fedex should throw an exception');
} catch (FedexRequestException $ex) {
$this->assertEquals('556', $ex->getCode());
$this->assertEquals('There are no valid services available. ', $ex->getMessage());
$this->assertEquals('WARNING', $ex->getSeverity());
}
}
public function testIncorrectResponse()
{
$rateAdapter = new Rate([
'prod' => false,
'key' => 'XXX',
'password' => 'XXX',
'accountNumber' => 'XXX',
'meterNumber' => 'XXX',
'dropOffType' => 'BUSINESS_SERVICE_CENTER',
'shipment' => $this->shipment,
'approvedCodes' => $this->approvedCodes,
'requestAdapter' => new StubIncorrectResponseAdapter(),
]);
try {
$rateAdapter->getRates();
$this->fail('Getting incorrect response should throw an exception');
} catch (FedexRequestException $ex) {
$this->assertEquals('Incorrect response received from FedEx: <html/>', $ex->getMessage());
}
}
public function testLiveRates()
{
if (getenv('FEDEX_KEY') === false) {

Loading…
Cancel
Save