Compare commits

..

No commits in common. "master" and "1.0.0" have entirely different histories.

26 changed files with 122 additions and 214 deletions

1
.gitignore vendored
View File

@ -5,4 +5,3 @@ coverage_report
!.idea/runConfigurations/
.DS_Store
live_phpunit.sh
/nbproject/private/

View File

@ -1,6 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All Unit Tests" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
<TestRunner class="pdt256\Shipping\PackageDimensionsValidationTrait" configuration_file="$PROJECT_DIR$/phpunit.xml" directory="$PROJECT_DIR$" file="$PROJECT_DIR$/tests/PackageDimensionsValidationTrait.php" scope="XML" use_alternative_configuration_file="true" />
<method v="2" />
</configuration>
</component>

View File

@ -1,9 +1,6 @@
language: php
php:
- 7.1
- 7.2
- 7.3
- 7.4
before_script:
- composer install --prefer-dist

View File

@ -1,7 +1,6 @@
The MIT License (MIT)
Copyright (c) 2014 Jamie Isaacs <pdt256@gmail.com>
Copyright (c) 2020 Netsyms Technologies <opensource@netsyms.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -15,6 +15,19 @@ Multiple packages can be added to get additional rates.
All code (including tests) conform to the PSR-2 coding standards.
The namespace and autoloader are using the PSR-4 standard.
All pull requests are processed by Travis CI to conform to PSR-2 and to verify all unit tests pass.
## Installation
Add the following lines to your ``composer.json`` file.
```JSON
{
"require": {
"pdt256/shipping": "1.0.*"
}
}
```
## Example

View File

@ -1,5 +1,5 @@
{
"name": "netsyms/shippingrates",
"name": "pdt256/shipping",
"description": "Shipping Rate API",
"license": "MIT",
"keywords": ["ship", "ups", "usps", "fedex"],
@ -7,20 +7,16 @@
{
"name": "Jamie Isaacs",
"email": "pdt256@gmail.com"
},
{
"name": "Netsyms Technologies",
"email": "opensource@netsyms.com"
}
],
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.0.0"
}
},
"require-dev": {
"phpunit/phpunit": "7.0.*",
"squizlabs/php_codesniffer": "3.5.*"
"phpunit/phpunit": "4.0.*",
"squizlabs/php_codesniffer": "1.5.5"
},
"require": {
"php": ">=7.1"

View File

@ -1,22 +0,0 @@
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_create_2e_tests=false
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.customSuite_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.customSuite_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.test_2e_groups_2e_ask=false
auxiliary.org-netbeans-modules-php-phpunit.test_2e_run_2e_all=false
auxiliary.org-netbeans-modules-php-phpunit.test_2e_run_2e_phpunit_2e_only=false
file.reference.ShippingRates-tests=tests
include.path=${php.global.include.path}
php.version=PHP_73
source.encoding=UTF-8
src.dir=src
tags.asp=false
tags.short=false
test.src.dir=${file.reference.ShippingRates-tests}
testing.providers=PhpUnit
web.root=.

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>ShippingRates</name>
</data>
</configuration>
</project>

View File

@ -14,9 +14,4 @@
<directory suffix=".php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>

View File

@ -1,63 +1,35 @@
<?php
<?php namespace pdt256\Shipping;
namespace pdt256\Shipping;
class Package {
protected $pounds;
protected $ounces;
class Package
{
protected $weight;
protected $width;
protected $length;
protected $height;
public function getWeight() {
$weight = 0;
if (!empty($this->getPounds())) {
$weight += $this->getPounds();
}
if (!empty($this->getOunces())) {
$weight += ($this->getOunces() / 16);
}
if ($weight == 0) {
return null;
}
return $weight;
}
public function getPounds() {
return $this->pounds ?? 0;
}
public function getOunces() {
return $this->ounces ?? 0;
}
public function setWeight($pounds) {
return $this->setPounds($pounds)->setOunces(0);
/**
* @return mixed
*/
public function getWeight()
{
return $this->weight;
}
/**
* @param mixed $pounds
* @param mixed $weight
* @return $this
*/
public function setPounds($pounds) {
$this->pounds = $pounds;
return $this;
}
/**
* @param mixed $ounces
* @return $this
*/
public function setOunces($ounces) {
$this->ounces = $ounces;
public function setWeight($weight)
{
$this->weight = $weight;
return $this;
}
/**
* @return mixed
*/
public function getWidth() {
public function getWidth()
{
return $this->width;
}
@ -65,7 +37,8 @@ class Package {
* @param mixed $width
* @return $this
*/
public function setWidth($width) {
public function setWidth($width)
{
$this->width = $width;
return $this;
}
@ -73,7 +46,8 @@ class Package {
/**
* @return mixed
*/
public function getLength() {
public function getLength()
{
return $this->length;
}
@ -81,7 +55,8 @@ class Package {
* @param mixed $length
* @return $this
*/
public function setLength($length) {
public function setLength($length)
{
$this->length = $length;
return $this;
}
@ -89,7 +64,8 @@ class Package {
/**
* @return mixed
*/
public function getHeight() {
public function getHeight()
{
return $this->height;
}
@ -97,9 +73,9 @@ class Package {
* @param mixed $height
* @return $this
*/
public function setHeight($height) {
public function setHeight($height)
{
$this->height = $height;
return $this;
}
}

View File

@ -1,6 +1,8 @@
<?php
namespace pdt256\Shipping;
use Exception;
abstract class RateAdapter
{
protected $isProduction;
@ -16,25 +18,20 @@ abstract class RateAdapter
/**
* Make sure all necessary fields are set
* @return self
*/
abstract protected function validate();
/**
* Prepare XML
* @return self
*/
abstract protected function prepare();
/**
* Curl Request
* @return self
*/
abstract protected function execute();
/**
* Convert to shipping rates array
* @return self
*/
abstract protected function process();
@ -74,20 +71,17 @@ abstract class RateAdapter
return $this->shipment;
}
public function setIsProduction($isProduction): void
public function setIsProduction($isProduction)
{
$this->isProduction = $isProduction;
}
public function getIsProduction(): bool
public function getIsProduction()
{
return $this->isProduction;
}
/**
* @return Quote[]
*/
public function getRates(): array
public function getRates()
{
$this
->validate()
@ -99,10 +93,8 @@ abstract class RateAdapter
return array_values($this->rates);
}
protected function sortByCost(): void
protected function sortByCost()
{
uasort($this->rates, static function (Quote $a, Quote $b) {
return ($a->getCost() > $b->getCost());
});
uasort($this->rates, create_function('$a, $b', 'return ($a->getCost() > $b->getCost());'));
}
}

View File

@ -3,8 +3,8 @@ namespace pdt256\Shipping\RateRequest;
abstract class Adapter
{
protected $curlConnectTimeoutInMilliseconds = 1500;
protected $curlDownloadTimeoutInSeconds = 50;
protected $curlConnectTimeoutInMilliseconds = 1000;
protected $curlDownloadTimeoutInSeconds = 11;
abstract public function execute($url, $data = null);
}

View File

@ -139,10 +139,8 @@ class Ship
return $display_rates;
}
protected function sortByCost(&$rates)
protected function sortByCost(& $rates)
{
uasort($rates, function ($a, $b) {
return ($a->getCost() > $b->getCost());
});
uasort($rates, create_function('$a, $b', 'return ($a->getCost() > $b->getCost());'));
}
}

View File

@ -95,8 +95,8 @@ class Rate extends RateAdapter
]);
$this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post()));
}
}
protected function validate()
{
$this->validatePackages();

View File

@ -1,5 +1,4 @@
<?php
namespace pdt256\Shipping\USPS;
use pdt256\Shipping;
@ -11,17 +10,18 @@ use pdt256\Shipping\Validator;
use DOMDocument;
use Exception;
class Rate extends RateAdapter {
class Rate extends RateAdapter
{
private $urlDev = 'http://production.shippingapis.com/ShippingAPI.dll';
private $urlProd = 'http://production.shippingapis.com/ShippingAPI.dll';
private $username;
private $password;
/**
* Codes of appropriate shipping types. Default value is specified in __construct.
*/
public $approvedCodes;
private $shipping_codes = [
'domestic' => [
'00' => 'First-Class Mail Parcel',
@ -67,7 +67,8 @@ class Rate extends RateAdapter {
],
];
public function __construct($options = []) {
public function __construct($options = [])
{
parent::__construct($options);
$this->username = Arr::get($options, 'username');
@ -78,8 +79,8 @@ class Rate extends RateAdapter {
]);
$this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Get()));
}
protected function validate() {
protected function validate()
{
$this->validatePackages();
Validator::checkIfNull($this->username, 'username');
Validator::checkIfNull($this->password, 'password');
@ -88,8 +89,8 @@ class Rate extends RateAdapter {
return $this;
}
protected function prepare() {
protected function prepare()
{
$packages = '';
$sequence_number = 0;
foreach ($this->shipment->getPackages() as $p) {
@ -118,12 +119,13 @@ class Rate extends RateAdapter {
$container = 'VARIABLE';
}
$packages .= '<Package ID="' . $sequence_number . '">' .
$packages .=
'<Package ID="' . $sequence_number . '">' .
'<Service>ALL</Service>' .
'<ZipOrigination>' . $this->shipment->getFromPostalCode() . '</ZipOrigination>' .
'<ZipDestination>' . $this->shipment->getToPostalCode() . '</ZipDestination>' .
'<Pounds>' . $p->getPounds() . '</Pounds>' .
'<Ounces>' . $p->getOunces() . '</Ounces>' .
'<Pounds>' . $p->getWeight() . '</Pounds>' .
'<Ounces>0</Ounces>' .
'<Container>' . $container . '</Container>' .
'<Size>' . $size . '</Size>' .
'<Width>' . $p->getWidth() . '</Width>' .
@ -133,7 +135,8 @@ class Rate extends RateAdapter {
'</Package>';
}
$this->data = '<RateV4Request USERID="' . $this->username . '">' .
$this->data =
'<RateV4Request USERID="' . $this->username . '">' .
'<Revision/>' .
$packages .
'</RateV4Request>';
@ -141,7 +144,8 @@ class Rate extends RateAdapter {
return $this;
}
protected function execute() {
protected function execute()
{
if ($this->isProduction) {
$url = $this->urlProd;
} else {
@ -155,7 +159,8 @@ class Rate extends RateAdapter {
return $this;
}
protected function process() {
protected function process()
{
try {
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadXml($this->response);
@ -202,5 +207,4 @@ class Rate extends RateAdapter {
return $this;
}
}

View File

@ -1,9 +1,7 @@
<?php
namespace pdt256\Shipping;
use PHPUnit\Framework\TestCase;
class ArrTest extends TestCase
class ArrTest extends \PHPUnit_Framework_TestCase
{
public function providerGet()
{

View File

@ -7,9 +7,8 @@ use pdt256\Shipping\Package;
use pdt256\Shipping\Shipment;
use pdt256\Shipping\Quote;
use DateTime;
use PHPUnit\Framework\TestCase;
class RateTest extends TestCase
class RateTest extends \PHPUnit_Framework_TestCase
{
/** @var Shipment */
protected $shipment;
@ -183,5 +182,6 @@ class RateTest extends TestCase
'requestAdapter' => new StubFedex,
]);
$rateAdapter->getRates();
}
}

View File

@ -5,9 +5,8 @@ namespace pdt256\Shipping;
use pdt256\Shipping\RateRequest\StubFedex;
use pdt256\Shipping\RateRequest\StubUPS;
use pdt256\Shipping\RateRequest\StubUSPS;
use PHPUnit\Framework\TestCase;
class PackageDimensionsValidationTrait extends TestCase
class PackageDimensionsValidationTrait extends \PHPUnit_Framework_TestCase
{
protected function getNormalPackage()
{
@ -108,15 +107,12 @@ class PackageDimensionsValidationTrait extends TestCase
->addPackage($package);
$adapter->setShipment($shipment);
$adapter->getRates();
$this->assertEquals($shipment, $adapter->getShipment());
}
public function testNormalUSPS()
{
$this->validatePackage($this->getNormalPackage(), $this->getUSPSAdapter());
}
/**
* @expectedException \LogicException
*/
@ -124,7 +120,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNoHeightPackage(), $this->getUSPSAdapter());
}
/**
* @expectedException \LogicException
*/
@ -132,7 +127,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNoLengthPackage(), $this->getUSPSAdapter());
}
/**
* @expectedException \LogicException
*/
@ -140,7 +134,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNoWidthPackage(), $this->getUSPSAdapter());
}
/**
* @expectedException \LogicException
*/
@ -154,7 +147,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNormalPackage(), $this->getUPSAdapter());
}
/**
* @expectedException \LogicException
*/
@ -162,7 +154,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNoHeightPackage(), $this->getUPSAdapter());
}
/**
* @expectedException \LogicException
*/
@ -170,7 +161,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNoLengthPackage(), $this->getUPSAdapter());
}
/**
* @expectedException \LogicException
*/
@ -178,7 +168,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNoWidthPackage(), $this->getUPSAdapter());
}
/**
* @expectedException \LogicException
*/
@ -192,7 +181,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNormalPackage(), $this->getFedexAdapter());
}
/**
* @expectedException \LogicException
*/
@ -200,7 +188,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNoHeightPackage(), $this->getFedexAdapter());
}
/**
* @expectedException \LogicException
*/
@ -208,7 +195,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNoLengthPackage(), $this->getFedexAdapter());
}
/**
* @expectedException \LogicException
*/
@ -216,7 +202,6 @@ class PackageDimensionsValidationTrait extends TestCase
{
$this->validatePackage($this->getNoWidthPackage(), $this->getFedexAdapter());
}
/**
* @expectedException \LogicException
*/

View File

@ -1,9 +1,7 @@
<?php
namespace pdt256\Shipping;
use PHPUnit\Framework\TestCase;
class PackageTest extends TestCase
class PackageTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()
{
@ -14,16 +12,8 @@ class PackageTest extends TestCase
$package->setHeight(8);
$this->assertEquals(5, $package->getWeight());
$this->assertEquals(5, $package->getPounds());
$this->assertEquals(0, $package->getOunces());
$this->assertEquals(6, $package->getWidth());
$this->assertEquals(7, $package->getLength());
$this->assertEquals(8, $package->getHeight());
$package->setPounds(3);
$package->setOunces(4);
$this->assertEquals(3, $package->getPounds());
$this->assertEquals(4, $package->getOunces());
$this->assertEquals(3.25, $package->getWeight());
}
}

View File

@ -2,9 +2,8 @@
namespace pdt256\Shipping;
use DateTime;
use PHPUnit\Framework\TestCase;
class QuoteTest extends TestCase
class QuoteTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()
{

13
tests/RateAdapterTest.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace pdt256\Shipping;
class RateAdapterTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()
{
/* @var RateAdapter $mock */
$mock = $this->getMockForAbstractClass('pdt256\Shipping\RateAdapter');
$mockRequestAdapter = $this->getMockForAbstractClass('pdt256\Shipping\RateRequest\Adapter');
$mock->setRequestAdapter($mockRequestAdapter);
}
}

View File

@ -2,9 +2,8 @@
namespace pdt256\Shipping;
use DateTime;
use PHPUnit\Framework\TestCase;
class ShipTest extends TestCase
class ShipTest extends \PHPUnit_Framework_TestCase
{
/** @var Shipment */
public $shipment;

View File

@ -1,9 +1,7 @@
<?php
namespace pdt256\Shipping;
use PHPUnit\Framework\TestCase;
class ShipmentTest extends TestCase
class ShipmentTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()
{

View File

@ -6,9 +6,8 @@ use pdt256\Shipping\Ship;
use pdt256\Shipping\Package;
use pdt256\Shipping\Shipment;
use pdt256\Shipping\Quote;
use PHPUnit\Framework\TestCase;
class RateTest extends TestCase
class RateTest extends \PHPUnit_Framework_TestCase
{
/** @var Shipment */
protected $shipment;

View File

@ -6,9 +6,8 @@ use pdt256\Shipping\Ship;
use pdt256\Shipping\Package;
use pdt256\Shipping\Shipment;
use pdt256\Shipping\Quote;
use PHPUnit\Framework\TestCase;
class RateTest extends TestCase
class RateTest extends \PHPUnit_Framework_TestCase
{
/** @var Shipment */
protected $shipment;

View File

@ -1,10 +1,7 @@
<?php
namespace pdt256\Shipping;
use PHPUnit\Framework\TestCase;
use stdClass;
class ValidatorTest extends TestCase
class ValidatorTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \LogicException
@ -18,9 +15,8 @@ class ValidatorTest extends TestCase
{
Validator::checkIfNull('XXX', 'notNullValue');
Validator::checkIfNull([], 'notNullValue');
Validator::checkIfNull(new stdClass(), 'notNullValue');
Validator::checkIfNull(new \stdClass(), 'notNullValue');
Validator::checkIfNull(function () {
}, 'notNullValue');
$this->assertTrue(true);
}
}