diff --git a/src/Fedex/Rate.php b/src/Fedex/Rate.php index 82f8dad..20c2016 100644 --- a/src/Fedex/Rate.php +++ b/src/Fedex/Rate.php @@ -74,11 +74,7 @@ class Rate extends RateAdapter } protected function validate() { - foreach ($this->shipment->getPackages() as $package) { - Validator::checkIfNull($package->getWeight(), 'weight'); - Validator::checkIfNull($package->getLength(), 'length'); - Validator::checkIfNull($package->getHeight(), 'height'); - } + $this->validatePackages(); Validator::checkIfNull($this->key, 'key'); Validator::checkIfNull($this->password, 'password'); Validator::checkIfNull($this->accountNumber, 'accountNumber'); diff --git a/src/RateAdapter.php b/src/RateAdapter.php index b4b5995..6ed0a6f 100644 --- a/src/RateAdapter.php +++ b/src/RateAdapter.php @@ -35,6 +35,19 @@ abstract class RateAdapter */ abstract protected function process(); + /** + * @throws \LogicException + * To be called from validate() when packages have to have 3 dimensions and weight + */ + protected function validatePackages() + { + foreach ($this->shipment->getPackages() as $package) { + Validator::checkIfNull($package->getWeight(), 'weight'); + Validator::checkIfNull($package->getLength(), 'length'); + Validator::checkIfNull($package->getHeight(), 'height'); + Validator::checkIfNull($package->getWidth(), 'width'); + } + } public function __construct($options = []) { $this->rates = []; @@ -48,6 +61,26 @@ abstract class RateAdapter $this->rateRequest = $rateRequest; } + public function setShipment($shipment) + { + $this->shipment = $shipment; + } + + public function getShipment() + { + return $this->shipment; + } + + public function setIsProduction($isProduction) + { + $this->isProduction = $isProduction; + } + + public function getIsProduction() + { + return $this->isProduction; + } + public function getRates() { $this diff --git a/src/UPS/Rate.php b/src/UPS/Rate.php index c8ecf67..c1242e8 100644 --- a/src/UPS/Rate.php +++ b/src/UPS/Rate.php @@ -99,11 +99,7 @@ class Rate extends RateAdapter } protected function validate() { - foreach ($this->shipment->getPackages() as $package) { - Validator::checkIfNull($package->getWeight(), 'weight'); - Validator::checkIfNull($package->getLength(), 'length'); - Validator::checkIfNull($package->getHeight(), 'height'); - } + $this->validatePackages(); Validator::checkIfNull($this->accessKey, 'accessKey'); Validator::checkIfNull($this->userId, 'userId'); Validator::checkIfNull($this->password, 'password'); diff --git a/src/USPS/Rate.php b/src/USPS/Rate.php index 2ade0ec..b6c815a 100644 --- a/src/USPS/Rate.php +++ b/src/USPS/Rate.php @@ -81,11 +81,7 @@ class Rate extends RateAdapter } protected function validate() { - foreach ($this->shipment->getPackages() as $package) { - Validator::checkIfNull($package->getWeight(), 'weight'); - Validator::checkIfNull($package->getLength(), 'length'); - Validator::checkIfNull($package->getHeight(), 'height'); - } + $this->validatePackages(); Validator::checkIfNull($this->username, 'username'); Validator::checkIfNull($this->password, 'password'); Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode'); diff --git a/tests/Fedex/FedexTest.php b/tests/Fedex/FedexTest.php index e68b82f..e7d09d3 100644 --- a/tests/Fedex/FedexTest.php +++ b/tests/Fedex/FedexTest.php @@ -111,4 +111,77 @@ class RateTest extends \PHPUnit_Framework_TestCase $this->assertTrue(count($rates) > 0); $this->assertTrue($rates[0] instanceof Quote); } + /** + * @expectedException \LogicException + */ + public function testMissingKey() + { + $rateAdapter = new Rate([ + 'prod' => false, + 'password' => 'XXX', + 'accountNumber' => 'XXX', + 'meterNumber' => 'XXX', + 'dropOffType' => 'BUSINESS_SERVICE_CENTER', + 'shipment' => $this->shipment, + 'approvedCodes' => $this->approvedCodes, + 'requestAdapter' => new StubFedex, + ]); + + $rateAdapter->getRates(); + } + /** + * @expectedException \LogicException + */ + public function testMissingPassword() + { + $rateAdapter = new Rate([ + 'prod' => false, + 'key' => 'XXX', + 'accountNumber' => 'XXX', + 'meterNumber' => 'XXX', + 'dropOffType' => 'BUSINESS_SERVICE_CENTER', + 'shipment' => $this->shipment, + 'approvedCodes' => $this->approvedCodes, + 'requestAdapter' => new StubFedex, + ]); + + $rateAdapter->getRates(); + } + /** + * @expectedException \LogicException + */ + public function testMissingAccountNumber() + { + $rateAdapter = new Rate([ + 'prod' => false, + 'key' => 'XXX', + 'password' => 'XXX', + 'meterNumber' => 'XXX', + 'dropOffType' => 'BUSINESS_SERVICE_CENTER', + 'shipment' => $this->shipment, + 'approvedCodes' => $this->approvedCodes, + 'requestAdapter' => new StubFedex, + ]); + + $rateAdapter->getRates(); + } + /** + * @expectedException \LogicException + */ + public function testMissingMeterNumber() + { + + $rateAdapter = new Rate([ + 'prod' => false, + 'key' => 'XXX', + 'password' => 'XXX', + 'accountNumber' => 'XXX', + 'dropOffType' => 'BUSINESS_SERVICE_CENTER', + 'shipment' => $this->shipment, + 'approvedCodes' => $this->approvedCodes, + 'requestAdapter' => new StubFedex, + ]); + $rateAdapter->getRates(); + + } } diff --git a/tests/PackageDimensionsTest.php b/tests/PackageDimensionsTest.php new file mode 100644 index 0000000..a6451fe --- /dev/null +++ b/tests/PackageDimensionsTest.php @@ -0,0 +1,212 @@ +setHeight(10) + ->setWidth(6) + ->setLength(10) + ->setWeight(5) + ; + return $normalPackage; + } + protected function getNoHeightPackage() + { + $noHeightPackage = new Package(); + $noHeightPackage + ->setWidth(6) + ->setLength(10) + ->setWeight(5) + ; + return $noHeightPackage; + } + protected function getNoWidthPackage() + { + $noWidthPackage = new Package(); + $noWidthPackage + ->setHeight(10) + ->setLength(10) + ->setWeight(5) + ; + return $noWidthPackage; + } + protected function getNoLengthPackage() + { + $noLengthPackage = new Package(); + $noLengthPackage + ->setHeight(10) + ->setWidth(6) + ->setWeight(5) + ; + return $noLengthPackage; + } + protected function getNoWeightPackage() + { + $noWeightPackage = new Package(); + $noWeightPackage + ->setHeight(10) + ->setWidth(10) + ->setWidth(6) + ; + return $noWeightPackage; + } + protected function getUSPSAdapter() + { + return new USPS\Rate([ + 'prod' => false, + 'username' => 'XXXX', + 'password' => 'XXXX', + 'requestAdapter' => new StubUSPS(), + ]); + } + + protected function getUPSAdapter() + { + return new UPS\Rate([ + 'accessKey' => 'XXX', + 'userId' => 'XXX', + 'password' => 'XXX', + 'shipperNumber' => 'XXX', + 'prod' => false, + 'requestAdapter' => new StubUPS(), + ]); + } + + protected function getFedexAdapter() + { + return new Fedex\Rate([ + 'prod' => false, + 'key' => 'XXX', + 'password' => 'XXX', + 'accountNumber' => 'XXX', + 'meterNumber' => 'XXX', + 'dropOffType' => 'BUSINESS_SERVICE_CENTER', + 'requestAdapter' => new StubFedex(), + ]); + } + + protected function validatePackage(Package $package, RateAdapter $adapter) + { + $shipment = new Shipment(); + $shipment->setFromStateProvinceCode('CA') + ->setFromPostalCode('90401') + ->setFromCountryCode('US') + ->setFromIsResidential(true) + ->setToPostalCode('78703') + ->setToCountryCode('US') + ->setToIsResidential(true) + ->addPackage($package); + $adapter->setShipment($shipment); + $adapter->getRates(); + } + + public function testNormalUSPS() + { + $this->validatePackage($this->getNormalPackage(), $this->getUSPSAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoHeightPackageUSPS() + { + $this->validatePackage($this->getNoHeightPackage(), $this->getUSPSAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoLengthPackageUSPS() + { + $this->validatePackage($this->getNoLengthPackage(), $this->getUSPSAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoWidthPackageUSPS() + { + $this->validatePackage($this->getNoWidthPackage(), $this->getUSPSAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoWeightPackageUSPS() + { + $this->validatePackage($this->getNoWeightPackage(), $this->getUSPSAdapter()); + } + + + public function testNormalUPS() + { + $this->validatePackage($this->getNormalPackage(), $this->getUPSAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoHeightPackageUPS() + { + $this->validatePackage($this->getNoHeightPackage(), $this->getUPSAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoLengthPackageUPS() + { + $this->validatePackage($this->getNoLengthPackage(), $this->getUPSAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoWidthPackageUPS() + { + $this->validatePackage($this->getNoWidthPackage(), $this->getUPSAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoWeightPackageUPS() + { + $this->validatePackage($this->getNoWeightPackage(), $this->getUPSAdapter()); + } + + + public function testNormalFedex() + { + $this->validatePackage($this->getNormalPackage(), $this->getFedexAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoHeightPackageFedex() + { + $this->validatePackage($this->getNoHeightPackage(), $this->getFedexAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoLengthPackageFedex() + { + $this->validatePackage($this->getNoLengthPackage(), $this->getFedexAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoWidthPackageFedex() + { + $this->validatePackage($this->getNoWidthPackage(), $this->getFedexAdapter()); + } + /** + * @expectedException \LogicException + */ + public function testNoWeightPackageFedex() + { + $this->validatePackage($this->getNoWeightPackage(), $this->getFedexAdapter()); + } +} diff --git a/tests/UPS/RateTest.php b/tests/UPS/RateTest.php index 4edf4ad..c51bb5d 100644 --- a/tests/UPS/RateTest.php +++ b/tests/UPS/RateTest.php @@ -101,4 +101,55 @@ class RateTest extends \PHPUnit_Framework_TestCase $this->assertTrue(count($rates) > 0); $this->assertTrue($rates[0] instanceof Quote); } + /** + * @expectedException \LogicException + */ + public function testMissingAccessKey() + { + $rateAdapter = new Rate([ + 'userId' => 'XXX', + 'password' => 'XXX', + 'shipperNumber' => 'XXX', + 'prod' => false, + 'shipment' => $this->shipment, + 'approvedCodes' => $this->approvedCodes, + 'requestAdapter' => new StubUPS, + ]); + + $rateAdapter->getRates(); + } + /** + * @expectedException \LogicException + */ + public function testMissingPassword() + { + $rateAdapter = new Rate([ + 'accessKey' => 'XXX', + 'userId' => 'XXX', + 'shipperNumber' => 'XXX', + 'prod' => false, + 'shipment' => $this->shipment, + 'approvedCodes' => $this->approvedCodes, + 'requestAdapter' => new StubUPS, + ]); + + $rateAdapter->getRates(); + } + /** + * @expectedException \LogicException + */ + public function testMissingShipperNumber() + { + $rateAdapter = new Rate([ + 'accessKey' => 'XXX', + 'userId' => 'XXX', + 'password' => 'XXX', + 'prod' => false, + 'shipment' => $this->shipment, + 'approvedCodes' => $this->approvedCodes, + 'requestAdapter' => new StubUPS, + ]); + + $rateAdapter->getRates(); + } } diff --git a/tests/USPS/RateTest.php b/tests/USPS/RateTest.php index 1cf9f67..b88897f 100644 --- a/tests/USPS/RateTest.php +++ b/tests/USPS/RateTest.php @@ -83,4 +83,35 @@ class RateTest extends \PHPUnit_Framework_TestCase $this->assertTrue(count($rates) > 0); $this->assertTrue($rates[0] instanceof Quote); } + + /** + * @expectedException \LogicException + */ + public function testMissingUserName() + { + $rateAdapter = new Rate([ + 'prod' => false, + 'password' => 'XXXX', + 'shipment' => $this->shipment, + 'approvedCodes' => $this->approvedCodes, + 'requestAdapter' => new StubUSPS, + ]); + + $rateAdapter->getRates(); + } + /** + * @expectedException \LogicException + */ + public function testMissingPassword() + { + $rateAdapter = new Rate([ + 'prod' => false, + 'username' => 'XXX', + 'shipment' => $this->shipment, + 'approvedCodes' => $this->approvedCodes, + 'requestAdapter' => new StubUSPS, + ]); + + $rateAdapter->getRates(); + } } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php new file mode 100644 index 0000000..70f165f --- /dev/null +++ b/tests/ValidatorTest.php @@ -0,0 +1,22 @@ +