Merge pull request #10 from perk11/validation
Added tests for Validation
This commit is contained in:
commit
71a7383b73
@ -74,11 +74,7 @@ class Rate extends RateAdapter
|
|||||||
}
|
}
|
||||||
protected function validate()
|
protected function validate()
|
||||||
{
|
{
|
||||||
foreach ($this->shipment->getPackages() as $package) {
|
$this->validatePackages();
|
||||||
Validator::checkIfNull($package->getWeight(), 'weight');
|
|
||||||
Validator::checkIfNull($package->getLength(), 'length');
|
|
||||||
Validator::checkIfNull($package->getHeight(), 'height');
|
|
||||||
}
|
|
||||||
Validator::checkIfNull($this->key, 'key');
|
Validator::checkIfNull($this->key, 'key');
|
||||||
Validator::checkIfNull($this->password, 'password');
|
Validator::checkIfNull($this->password, 'password');
|
||||||
Validator::checkIfNull($this->accountNumber, 'accountNumber');
|
Validator::checkIfNull($this->accountNumber, 'accountNumber');
|
||||||
|
@ -35,6 +35,19 @@ abstract class RateAdapter
|
|||||||
*/
|
*/
|
||||||
abstract protected function process();
|
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 = [])
|
public function __construct($options = [])
|
||||||
{
|
{
|
||||||
$this->rates = [];
|
$this->rates = [];
|
||||||
@ -48,6 +61,26 @@ abstract class RateAdapter
|
|||||||
$this->rateRequest = $rateRequest;
|
$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()
|
public function getRates()
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
|
@ -99,11 +99,7 @@ class Rate extends RateAdapter
|
|||||||
}
|
}
|
||||||
protected function validate()
|
protected function validate()
|
||||||
{
|
{
|
||||||
foreach ($this->shipment->getPackages() as $package) {
|
$this->validatePackages();
|
||||||
Validator::checkIfNull($package->getWeight(), 'weight');
|
|
||||||
Validator::checkIfNull($package->getLength(), 'length');
|
|
||||||
Validator::checkIfNull($package->getHeight(), 'height');
|
|
||||||
}
|
|
||||||
Validator::checkIfNull($this->accessKey, 'accessKey');
|
Validator::checkIfNull($this->accessKey, 'accessKey');
|
||||||
Validator::checkIfNull($this->userId, 'userId');
|
Validator::checkIfNull($this->userId, 'userId');
|
||||||
Validator::checkIfNull($this->password, 'password');
|
Validator::checkIfNull($this->password, 'password');
|
||||||
|
@ -81,11 +81,7 @@ class Rate extends RateAdapter
|
|||||||
}
|
}
|
||||||
protected function validate()
|
protected function validate()
|
||||||
{
|
{
|
||||||
foreach ($this->shipment->getPackages() as $package) {
|
$this->validatePackages();
|
||||||
Validator::checkIfNull($package->getWeight(), 'weight');
|
|
||||||
Validator::checkIfNull($package->getLength(), 'length');
|
|
||||||
Validator::checkIfNull($package->getHeight(), 'height');
|
|
||||||
}
|
|
||||||
Validator::checkIfNull($this->username, 'username');
|
Validator::checkIfNull($this->username, 'username');
|
||||||
Validator::checkIfNull($this->password, 'password');
|
Validator::checkIfNull($this->password, 'password');
|
||||||
Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode');
|
Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode');
|
||||||
|
@ -111,4 +111,77 @@ class RateTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertTrue(count($rates) > 0);
|
$this->assertTrue(count($rates) > 0);
|
||||||
$this->assertTrue($rates[0] instanceof Quote);
|
$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();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
212
tests/PackageDimensionsTest.php
Normal file
212
tests/PackageDimensionsTest.php
Normal file
@ -0,0 +1,212 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace pdt256\Shipping;
|
||||||
|
|
||||||
|
use pdt256\Shipping\RateRequest\StubFedex;
|
||||||
|
use pdt256\Shipping\RateRequest\StubUPS;
|
||||||
|
use pdt256\Shipping\RateRequest\StubUSPS;
|
||||||
|
|
||||||
|
class PackageDimensionsValidationTrait extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected function getNormalPackage()
|
||||||
|
{
|
||||||
|
$normalPackage = new Package();
|
||||||
|
$normalPackage
|
||||||
|
->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());
|
||||||
|
}
|
||||||
|
}
|
@ -101,4 +101,55 @@ class RateTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertTrue(count($rates) > 0);
|
$this->assertTrue(count($rates) > 0);
|
||||||
$this->assertTrue($rates[0] instanceof Quote);
|
$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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,4 +83,35 @@ class RateTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertTrue(count($rates) > 0);
|
$this->assertTrue(count($rates) > 0);
|
||||||
$this->assertTrue($rates[0] instanceof Quote);
|
$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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
22
tests/ValidatorTest.php
Normal file
22
tests/ValidatorTest.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
namespace pdt256\Shipping;
|
||||||
|
|
||||||
|
class ValidatorTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @expectedException \LogicException
|
||||||
|
*/
|
||||||
|
public function testNull()
|
||||||
|
{
|
||||||
|
Validator::checkIfNull(null, 'null');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNotNull()
|
||||||
|
{
|
||||||
|
Validator::checkIfNull('XXX', 'notNullValue');
|
||||||
|
Validator::checkIfNull([], 'notNullValue');
|
||||||
|
Validator::checkIfNull(new \stdClass(), 'notNullValue');
|
||||||
|
Validator::checkIfNull(function() {
|
||||||
|
}, 'notNullValue');
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user