From 018b5db9f927d0ad61546b2731c562980e50e24b Mon Sep 17 00:00:00 2001 From: Aaron Rickard Date: Wed, 25 May 2016 13:35:05 +0100 Subject: [PATCH 1/8] 48: Apply grumphp changes to library files, not finished --- src/Carrier.php | 170 ++++++++++++++++++++++++++------------- src/CarrierInterface.php | 34 ++++++-- src/Data.php | 138 +++++++++++++++++++++---------- src/Method.php | 25 +++++- tests/CarrierTest.php | 25 +++++- 5 files changed, 283 insertions(+), 109 deletions(-) diff --git a/src/Carrier.php b/src/Carrier.php index 29b2479..609c45a 100644 --- a/src/Carrier.php +++ b/src/Carrier.php @@ -1,49 +1,74 @@ - + * @copyright 2016 Meanbee Limited (http://www.meanbee.com) + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ +namespace Meanbee\Royalmail; + +/** + * Class Carrier + * Provides methods to get rates from the csv files, interacts with the data class + * to return arrays of methods. + * + * @category Meanbee + * @author Meanbee Limited + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + * @package Meanbee\Royalmail + */ class Carrier implements CarrierInterface { /** * CSV file location for CountryCodes + * * @var string */ - protected $_csvCountryCode; + protected $csvCountryCodeDefault; /** * CSV file location for zone to methods * * @var string */ - protected $_csvZoneToDeliveryMethod; + protected $csvZoneToDeliveryMethodDefault; /** * CSV file location for method meta info * * @var string */ - protected $_csvDeliveryMethodMeta; + protected $csvDeliveryMethodMetaDefault; /** * CSV file location for method to price * * @var string */ - protected $_csvDeliveryToPrice; + protected $csvDeliveryToPriceDefault; /** * CSV file location for method codes to user-friendly label. * * @var string */ - protected $_csvCleanNameToMethod; + protected $csvCleanNameToMethodDefault; /** * CSV file location for mapping of method to method group * * @var string */ - protected $_csvCleanNameMethodGroup; - + protected $csvCleanNameMethodGroupDefault; /** * Data resource class @@ -55,39 +80,61 @@ class Carrier implements CarrierInterface /** * Carrier constructor. * - * @param string|null $csvCountryCode - * @param string|null $csvZoneToDeliveryMethod - * @param string|null $csvDeliveryMethodMeta - * @param string|null $csvDeliveryToPrice - * @param string|null $csvCleanNameToMethod - * @param string|null $csvCleanNameMethodGroup + * @param string|null $csvCountryCode - csv for Country Code + * @param string|null $csvZoneToDeliveryMethod - csv for ZoneToDeliveyMethod + * @param string|null $csvDeliveryMethodMeta - csv for DeliveryMethodMeta + * @param string|null $csvDeliveryToPrice - csv for DeliveryToPrice + * @param string|null $csvCleanNameToMethod - csv for CleanNameToMethod + * @param string|null $csvCleanNameMethodGroup - csv for $csvCleanNameMethodGroup */ - public function __construct($csvCountryCode = null, $csvZoneToDeliveryMethod = null, $csvDeliveryMethodMeta = null, - $csvDeliveryToPrice = null, $csvCleanNameToMethod = null, - $csvCleanNameMethodGroup = null) - { + public function __construct( + $csvCountryCode = null, + $csvZoneToDeliveryMethod = null, + $csvDeliveryMethodMeta = null, + $csvDeliveryToPrice = null, + $csvCleanNameToMethod = null, + $csvCleanNameMethodGroup = null + ) { $dir = dirname(realpath(__FILE__)) . '/'; // Set the default csv values - $this->_csvCountryCode = isset($csvCountryCode) ? $csvCountryCode : $dir . '../data/1_countryToZone.csv'; - $this->_csvZoneToDeliveryMethod = isset($csvZoneToDeliveryMethod) ? $csvZoneToDeliveryMethod : - $dir . '../data/2_zoneToDeliveryMethod.csv'; - $this->_csvDeliveryMethodMeta = isset($csvDeliveryMethodMeta) ? $csvDeliveryMethodMeta : - $dir . '../data/3_deliveryMethodMeta.csv'; - $this->_csvDeliveryToPrice = isset($csvDeliveryToPrice) ? $csvDeliveryToPrice : - $dir . '../data/4_deliveryToPrice.csv'; - $this->_csvCleanNameToMethod = isset($csvCleanNameToMethod) ? $csvCleanNameToMethod : - $dir . '../data/5_cleanNameToMethod.csv'; - $this->_csvCleanNameMethodGroup = isset($csvCleanNameMethodGroup) ? $csvCleanNameMethodGroup : - $dir . '../data/6_cleanNameMethodGroup.csv'; + $this->csvCountryCodeDefault = "$dir../data/1_countryToZone.csv"; + if ($csvCountryCode) { + $this->csvCountryCodeDefault = $csvCountryCode; + } + + $this->csvZoneToDeliveryMethodDefault = "$dir../data/2_zoneToDeliveryMethod.csv"; + if ($csvZoneToDeliveryMethod) { + $this->csvZoneToDeliveryMethodDefault = $csvZoneToDeliveryMethod; + } + + $this->csvDeliveryMethodMetaDefault = "$dir../data/3_deliveryMethodMeta.csv"; + if ($csvDeliveryMethodMeta) { + $this->csvDeliveryMethodMetaDefault = $csvDeliveryMethodMeta; + } + + $this->csvDeliveryToPriceDefault = "$dir../data/4_deliveryToPrice.csv"; + if ($csvDeliveryToPrice) { + $this->csvDeliveryToPriceDefault = $csvDeliveryToPrice; + } + + $this->csvCleanNameToMethodDefault = "$dir../data/5_cleanNameToMethod.csv"; + if ($csvCleanNameToMethod) { + $this->csvCleanNameToMethodDefault = $csvCleanNameToMethod; + } + + $this->csvCleanNameMethodGroupDefault = "$dir../data/6_cleanNameMethodGroup.csv"; + if ($csvCleanNameMethodGroup) { + $this->csvCleanNameMethodGroupDefault = $csvCleanNameMethodGroup; + } $this->data = isset($data) ? $data : new Data( - $this->_csvCountryCode, - $this->_csvZoneToDeliveryMethod, - $this->_csvDeliveryMethodMeta, - $this->_csvDeliveryToPrice, - $this->_csvCleanNameToMethod, - $this->_csvCleanNameMethodGroup + $this->csvCountryCodeDefault, + $this->csvZoneToDeliveryMethodDefault, + $this->csvDeliveryMethodMetaDefault, + $this->csvDeliveryToPriceDefault, + $this->csvCleanNameToMethodDefault, + $this->csvCleanNameMethodGroupDefault ); } @@ -104,17 +151,28 @@ public function __construct($csvCountryCode = null, $csvZoneToDeliveryMethod = n * wants to get all available methods for the country code * and weight, ignoring the price of the package. * - * @param $country_code - * @param $package_value - * @param $package_weight - * @param $ignore_package_value + * @param string $country_code - The country code being shipped to + * @param int $package_value - The total package value + * @param int $package_weight - The total package weight + * @param bool $ignore_package_value - Flag to allow ignoring the + * package weight * - * @return array + * @return array - Array of all methods returned */ - public function getRates($country_code, $package_value, $package_weight, $ignore_package_value = false) - { - - $sortedDeliveryMethods = [$this->data->calculateMethods($country_code, $package_value, $package_weight, $ignore_package_value)]; + public function getRates( + $country_code, + $package_value, + $package_weight, + $ignore_package_value = false + ) { + $sortedDeliveryMethods = [ + $this->data->calculateMethods( + $country_code, + $package_value, + $package_weight, + $ignore_package_value + ) + ]; $results = []; @@ -155,60 +213,60 @@ public function getAllMethods() /** * CSV file location for CountryCodes * - * @return string + * @return string - default csv */ public function getCsvCountryCode() { - return $this->_csvCountryCode; + return $this->csvCountryCodeDefault; } /** * CSV file location for zone to methods * - * @return string + * @return string - default csv */ public function getCsvZoneToDeliveryMethod() { - return $this->_csvZoneToDeliveryMethod; + return $this->csvZoneToDeliveryMethodDefault; } /** * CSV file location for method meta info * - * @return string + * @return string - default csv */ public function getCsvDeliveryMethodMeta() { - return $this->_csvDeliveryMethodMeta; + return $this->csvDeliveryMethodMetaDefault; } /** * CSV file location for method to price * - * @return string + * @return string - default csv */ public function getCsvDeliveryToPrice() { - return $this->_csvDeliveryToPrice; + return $this->csvDeliveryToPriceDefault; } /** * CSV file location for method codes to user-friendly label. * - * @return string + * @return string - default csv */ public function getCsvCleanNameToMethod() { - return $this->_csvCleanNameToMethod; + return $this->csvCleanNameToMethodDefault; } /** * CSV file location for mapping of method to method group * - * @return string + * @return string - default csv */ public function getCsvCleanNameMethodGroup() { - return $this->_csvCleanNameMethodGroup; + return $this->csvCleanNameMethodGroupDefault; } } diff --git a/src/CarrierInterface.php b/src/CarrierInterface.php index 2059e1f..8020568 100644 --- a/src/CarrierInterface.php +++ b/src/CarrierInterface.php @@ -1,19 +1,43 @@ + * @copyright 2016 Meanbee Limited (http://www.meanbee.com) + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ namespace Meanbee\Royalmail; +/** + * Interface CarrierInterface + * Allows for a generic interface of carrier to account for any future carriers + * to be added. + * + * @package Meanbee\Royalmail + */ interface CarrierInterface { /** * Get methods and rates based on package conditions * - * @param $country_code - * @param $package_value - * @param $package_weight - * @param $ignore_package_value + * @param string $country_code - Country code package is going to + * @param int $package_value - Package value + * @param int $package_weight - Package weight + * @param bool $ignore_package_value - Whether to ignore package value or not * * @return array */ - public function getRates($country_code, $package_value, $package_weight, $ignore_package_value = false); + public function getRates( + $country_code, + $package_value, + $package_weight, + $ignore_package_value = false + ); } diff --git a/src/Data.php b/src/Data.php index 26be146..7d3cdaa 100644 --- a/src/Data.php +++ b/src/Data.php @@ -1,5 +1,28 @@ - + * @copyright 2016 Meanbee Limited (http://www.meanbee.com) + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ + +namespace Meanbee\Royalmail; + +/** + * Class Data + * Data layer class. Interacts with the csv files and creates sorted mapped arrays + * out of them. Provides methods to interact with the csv files and return sorted + * method rates of several formats. + * + * @author Meanbee Limited + * @package Meanbee\Royalmail + */ class Data { @@ -24,7 +47,8 @@ class Data protected $mappingCleanNameToMethod = []; /** - * Maps the method group name to the clean name, to allow for printing just the clean names to the user + * Maps the method group name to the clean name, to allow for printing + * just the clean names to the user * * @var array */ @@ -45,7 +69,8 @@ class Data protected $mappingZoneToMethod = []; /** - * Map methods to meta information. This includes the insurance amount, and the corresponding price levels + * Map methods to meta information. This includes the insurance + * amount, and the corresponding price levels * * @var array */ @@ -58,6 +83,16 @@ class Data */ protected $mappingDeliveryToPrice = []; + /** + * Data constructor. + * + * @param string $_csvCountryCode - country code csv path + * @param string $_csvZoneToDeliveryMethod - zone to method csv path + * @param string $_csvDeliveryMethodMeta - delivery method meta csv path + * @param string $_csvDeliveryToPrice - delivery to price csv path + * @param string $_csvCleanNameToMethod - clean name to method csv path + * @param string $_csvCleanNameMethodGroup - clean name method group csv path + */ public function __construct( $_csvCountryCode, $_csvZoneToDeliveryMethod, @@ -85,15 +120,19 @@ public function __construct( * value of the packages to be ignored in the calculation * at the users discretion. * - * @param $country_code - * @param $package_value - * @param $package_weight - * @param $ignore_package_value + * @param string $country_code - Country code being shipped to + * @param int $package_value - Package value + * @param int $package_weight - Package weight + * @param bool $ignore_package_value - ignore weight bool * * @return array */ - public function calculateMethods($country_code, $package_value, $package_weight, $ignore_package_value = false) - { + public function calculateMethods( + $country_code, + $package_value, + $package_weight, + $ignore_package_value = false + ) { $sortedCountryCodeMethods = [ $this->getCountryCodeData( $country_code, @@ -134,12 +173,11 @@ public function calculateMethods($country_code, $package_value, $package_weight, } /** - * * Method to return a 2d array of world zones a country * (by its country code) is located in. * - * @param $country_code - * @param $mappingCountryToZone + * @param string $country_code - Country code to filter on + * @param array $mappingCountryToZone - Array for mapped countries to zones * * @return array */ @@ -148,7 +186,10 @@ private function getCountryCodeData($country_code, $mappingCountryToZone) // Get All array items that match the country code $countryCodeData = []; foreach ($mappingCountryToZone as $item) { - if (isset($item[self::COUNTRY_CODE]) && $item[self::COUNTRY_CODE] == $country_code) { + if (isset( + $item[self::COUNTRY_CODE]) + && $item[self::COUNTRY_CODE] == $country_code + ) { foreach ($item as $keys) { $countryCodeData[] = $keys; } @@ -171,8 +212,8 @@ private function getCountryCodeData($country_code, $mappingCountryToZone) * Method to return a 2d array of possible delivery methods based * on the given world zones a country is in. * - * @param $sortedCountryCodeMethods - * @param $mappingZoneToMethod + * @param array $sortedCountryCodeMethods - Methods to filter on + * @param array $mappingZoneToMethod - ZonesToMethods to filter on * * @return array */ @@ -182,11 +223,12 @@ private function getZoneToMethod($sortedCountryCodeMethods, $mappingZoneToMethod foreach ($sortedCountryCodeMethods as $key => $value) { foreach ($value as $zone) { foreach ($mappingZoneToMethod as $item) { - if (isset($item[self::WORLD_ZONE]) && $item[self::WORLD_ZONE] == $zone) { + if (isset($item[self::WORLD_ZONE]) + && $item[self::WORLD_ZONE] == $zone + ) { foreach ($item as $keys) { $mappingZoneData[] = $keys; } - } } } @@ -201,7 +243,6 @@ private function getZoneToMethod($sortedCountryCodeMethods, $mappingZoneToMethod } } } - } $mappingZoneData = array_values($mappingZoneData); @@ -214,23 +255,29 @@ private function getZoneToMethod($sortedCountryCodeMethods, $mappingZoneToMethod * given allowed shipping method and the given package * value. * - * @param $packageValue - * @param $sortedZoneToMethods - * @param $mappingMethodToMeta + * @param int $packageValue - Package value to filter methods on + * @param array $sortedZoneToMethods - SortedZoneToMethods to filter with + * @param array $mappingMethodToMeta - MethodToMeta to filter on * * @return array */ - private function getMethodToMeta($packageValue, $sortedZoneToMethods, $mappingMethodToMeta) - { + private function getMethodToMeta( + $packageValue, + $sortedZoneToMethods, + $mappingMethodToMeta + ) { $mappingZoneMethodData = []; foreach ($sortedZoneToMethods as $key => $value) { foreach ($value as $method) { foreach ($mappingMethodToMeta as $item) { - if (isset($item[self::SHIPPING_METHOD]) && $item[self::SHIPPING_METHOD] == $method) { - if ($packageValue >= $item[self::METHOD_MIN_VALUE] && $packageValue <= $item[self::METHOD_MAX_VALUE]) { + if (isset($item[self::SHIPPING_METHOD]) + && $item[self::SHIPPING_METHOD] == $method + ) { + if ($packageValue >= $item[self::METHOD_MIN_VALUE] + && $packageValue <= $item[self::METHOD_MAX_VALUE] + ) { $mappingZoneMethodData[] = [$item]; } - } } } @@ -255,8 +302,11 @@ private function getMethodToMeta($packageValue, $sortedZoneToMethods, $mappingMe * * @return array */ - private function getMethodToPrice($package_weight, $sortedMethodToMeta, $mappingDeliveryToPrice) - { + private function getMethodToPrice( + $package_weight, + $sortedMethodToMeta, + $mappingDeliveryToPrice + ) { $mappingDeliveryToPriceData = []; foreach ($sortedMethodToMeta as $method) { foreach ($method as $meta) { @@ -267,10 +317,10 @@ private function getMethodToPrice($package_weight, $sortedMethodToMeta, $mapping if ($package_weight >= $item[self::METHOD_MIN_WEIGHT] && $package_weight <= $item[self::METHOD_MAX_WEIGHT]) { $resultArray = [ 'shippingMethodName' => $item[self::SHIPPING_METHOD], - 'minimumWeight' => (double)$item[self::METHOD_MIN_WEIGHT], - 'maximumWeight' => (double)$item[self::METHOD_MAX_WEIGHT], - 'methodPrice' => (double)$item[self::METHOD_PRICE], - 'insuranceValue' => (int)$item[self::METHOD_INSURANCE_VALUE], + 'minimumWeight' => (double) $item[self::METHOD_MIN_WEIGHT], + 'maximumWeight' => (double) $item[self::METHOD_MAX_WEIGHT], + 'methodPrice' => (double) $item[self::METHOD_PRICE], + 'insuranceValue' => (int) $item[self::METHOD_INSURANCE_VALUE], 'shippingMethodNameClean' => $value[self::METHOD_NAME_CLEAN] ]; @@ -279,7 +329,6 @@ private function getMethodToPrice($package_weight, $sortedMethodToMeta, $mapping } $mappingDeliveryToPriceData[] = $resultArray; - } } } @@ -310,7 +359,9 @@ private function getMethodToMetaAll($sortedZoneToMethods, $mappingMethodToMeta) foreach ($sortedZoneToMethods as $key => $value) { foreach ($value as $method) { foreach ($mappingMethodToMeta as $item) { - if (isset($item[self::SHIPPING_METHOD]) && $item[self::SHIPPING_METHOD] == $method) { + if (isset($item[self::SHIPPING_METHOD]) + && $item[self::SHIPPING_METHOD] == $method + ) { $mappingZoneMethodData[] = [$item]; } } @@ -325,8 +376,8 @@ private function getMethodToMetaAll($sortedZoneToMethods, $mappingMethodToMeta) /** * Reads the given csv in to a 2d array * - * @param string $filename - * @param string $delimiter + * @param string $filename - The filename of the csv + * @param string $delimiter - The delimiter * * @return array * @throws \Exception @@ -334,8 +385,9 @@ private function getMethodToMetaAll($sortedZoneToMethods, $mappingMethodToMeta) private function csvToArray($filename = '', $delimiter = ',') { if (!file_exists($filename) || !is_readable($filename)) { - throw new \Exception("Unable to load the Royal Mail price data csv for '$filename'. - Ensure that the data folder contains all the necessary csvs."); + throw new \Exception("Unable to load the Royal Mail price data csv for + '$filename'. Ensure that the data folder contains all the necessary + csvs."); } $header = null; @@ -360,7 +412,8 @@ public function getMappingCleanNameToMethod() } /** - * Maps the method group name to the clean name, to allow for printing just the clean names to the user + * Maps the method group name to the clean name, to allow + * for printing just the clean names to the user * * @return array */ @@ -390,7 +443,8 @@ public function getMappingZoneToMethod() } /** - * Map methods to meta information. This includes the insurance amount, and the corresponding price levels + * Map methods to meta information. This includes the insurance + * amount, and the corresponding price levels * * @return array */ @@ -408,4 +462,4 @@ public function getMappingDeliveryToPrice() { return $this->mappingDeliveryToPrice; } -} \ No newline at end of file +} diff --git a/src/Method.php b/src/Method.php index c954b34..b16053e 100644 --- a/src/Method.php +++ b/src/Method.php @@ -1,5 +1,26 @@ - + * @copyright 2016 Meanbee Limited (http://www.meanbee.com) + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ + +namespace Meanbee\Royalmail; + +/** + * Class Method + * Object class to represent the method object and allow for + * creation of method objects by the other classes. + * + * @package Meanbee\Royalmail + */ class Method { /** diff --git a/tests/CarrierTest.php b/tests/CarrierTest.php index 962afa0..979c90a 100644 --- a/tests/CarrierTest.php +++ b/tests/CarrierTest.php @@ -1,5 +1,25 @@ - + * @copyright 2016 Meanbee Limited (http://www.meanbee.com) + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ +namespace Meanbee\Royalmail; + +/** + * Class CarrierTest + * Test class to run phpunit tests on the library. + * + * @package Meanbee\Royalmail + */ class CarrierTest extends \PHPUnit_Framework_TestCase { @@ -118,7 +138,6 @@ public function testRoyalmailMethodRealValues() gettype($arrayContents['shippingMethodNameClean']), "shippingMethodNameClean array value not equal to correct type."); $this->assertEquals(gettype($this->testDataClassArray['size']), gettype($arrayContents['size']), "size array value not equal to correct type."); - } } @@ -206,7 +225,6 @@ public function testMethodToMetaVsCleanName() sprintf("Clean names %s and %s were not equal", $data[self::METHOD_CLEAN_NAME_ROW_META_CSV], $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV])); } - } $this->assertTrue($methodNotExist, sprintf("%s was not found in CleanNameToMethod csv$.", $data[self::METHOD_NAME_ROW_META_CSV])); @@ -245,7 +263,6 @@ public function testInsuranceValue() } } } - } } From c2325867b1c5565e7252b1a83d2ac3e0f30ed384 Mon Sep 17 00:00:00 2001 From: Aaron Rickard Date: Thu, 16 Jun 2016 08:53:29 +0100 Subject: [PATCH 2/8] 48: Apply grumphp changes to Carrier.php Includes refactoring, adding of comments and renaming --- src/Carrier.php | 70 ++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Carrier.php b/src/Carrier.php index 609c45a..3631791 100644 --- a/src/Carrier.php +++ b/src/Carrier.php @@ -20,10 +20,10 @@ * to return arrays of methods. * * @category Meanbee + * @package Meanbee\Royalmail * @author Meanbee Limited * @license OSL v. 3.0 * @link http://github.com/meanbee/royalmail-php-library - * @package Meanbee\Royalmail */ class Carrier implements CarrierInterface { @@ -33,42 +33,42 @@ class Carrier implements CarrierInterface * * @var string */ - protected $csvCountryCodeDefault; + protected $csvCountryCodeDef; /** * CSV file location for zone to methods * * @var string */ - protected $csvZoneToDeliveryMethodDefault; + protected $csvZoneToDeliveryMethodDef; /** * CSV file location for method meta info * * @var string */ - protected $csvDeliveryMethodMetaDefault; + protected $csvDeliveryMethodMetaDef; /** * CSV file location for method to price * * @var string */ - protected $csvDeliveryToPriceDefault; + protected $csvDeliveryToPriceDef; /** * CSV file location for method codes to user-friendly label. * * @var string */ - protected $csvCleanNameToMethodDefault; + protected $csvCleanNameToMethodDef; /** * CSV file location for mapping of method to method group * * @var string */ - protected $csvCleanNameMethodGroupDefault; + protected $csvCleanNameMethodGroupDef; /** * Data resource class @@ -98,43 +98,43 @@ public function __construct( $dir = dirname(realpath(__FILE__)) . '/'; // Set the default csv values - $this->csvCountryCodeDefault = "$dir../data/1_countryToZone.csv"; + $this->csvCountryCodeDef = "$dir../data/1_countryToZone.csv"; if ($csvCountryCode) { - $this->csvCountryCodeDefault = $csvCountryCode; + $this->csvCountryCodeDef = $csvCountryCode; } - $this->csvZoneToDeliveryMethodDefault = "$dir../data/2_zoneToDeliveryMethod.csv"; + $this->csvZoneToDeliveryMethodDef = "$dir../data/2_zoneToDeliveryMethod.csv"; if ($csvZoneToDeliveryMethod) { - $this->csvZoneToDeliveryMethodDefault = $csvZoneToDeliveryMethod; + $this->csvZoneToDeliveryMethodDef = $csvZoneToDeliveryMethod; } - $this->csvDeliveryMethodMetaDefault = "$dir../data/3_deliveryMethodMeta.csv"; + $this->csvDeliveryMethodMetaDef = "$dir../data/3_deliveryMethodMeta.csv"; if ($csvDeliveryMethodMeta) { - $this->csvDeliveryMethodMetaDefault = $csvDeliveryMethodMeta; + $this->csvDeliveryMethodMetaDef = $csvDeliveryMethodMeta; } - $this->csvDeliveryToPriceDefault = "$dir../data/4_deliveryToPrice.csv"; + $this->csvDeliveryToPriceDef = "$dir../data/4_deliveryToPrice.csv"; if ($csvDeliveryToPrice) { - $this->csvDeliveryToPriceDefault = $csvDeliveryToPrice; + $this->csvDeliveryToPriceDef = $csvDeliveryToPrice; } - $this->csvCleanNameToMethodDefault = "$dir../data/5_cleanNameToMethod.csv"; + $this->csvCleanNameToMethodDef = "$dir../data/5_cleanNameToMethod.csv"; if ($csvCleanNameToMethod) { - $this->csvCleanNameToMethodDefault = $csvCleanNameToMethod; + $this->csvCleanNameToMethodDef = $csvCleanNameToMethod; } - $this->csvCleanNameMethodGroupDefault = "$dir../data/6_cleanNameMethodGroup.csv"; + $this->csvCleanNameMethodGroupDef = "$dir../data/6_cleanNameMethodGroup.csv"; if ($csvCleanNameMethodGroup) { - $this->csvCleanNameMethodGroupDefault = $csvCleanNameMethodGroup; + $this->csvCleanNameMethodGroupDef = $csvCleanNameMethodGroup; } $this->data = isset($data) ? $data : new Data( - $this->csvCountryCodeDefault, - $this->csvZoneToDeliveryMethodDefault, - $this->csvDeliveryMethodMetaDefault, - $this->csvDeliveryToPriceDefault, - $this->csvCleanNameToMethodDefault, - $this->csvCleanNameMethodGroupDefault + $this->csvCountryCodeDef, + $this->csvZoneToDeliveryMethodDef, + $this->csvDeliveryMethodMetaDef, + $this->csvDeliveryToPriceDef, + $this->csvCleanNameToMethodDef, + $this->csvCleanNameMethodGroupDef ); } @@ -151,10 +151,10 @@ public function __construct( * wants to get all available methods for the country code * and weight, ignoring the price of the package. * - * @param string $country_code - The country code being shipped to - * @param int $package_value - The total package value - * @param int $package_weight - The total package weight - * @param bool $ignore_package_value - Flag to allow ignoring the + * @param string $country_code - The country code being shipped to + * @param int $package_value - The total package value + * @param int $package_weight - The total package weight + * @param bool $ignore_package_value - Flag to allow ignoring the * package weight * * @return array - Array of all methods returned @@ -217,7 +217,7 @@ public function getAllMethods() */ public function getCsvCountryCode() { - return $this->csvCountryCodeDefault; + return $this->csvCountryCodeDef; } /** @@ -227,7 +227,7 @@ public function getCsvCountryCode() */ public function getCsvZoneToDeliveryMethod() { - return $this->csvZoneToDeliveryMethodDefault; + return $this->csvZoneToDeliveryMethodDef; } /** @@ -237,7 +237,7 @@ public function getCsvZoneToDeliveryMethod() */ public function getCsvDeliveryMethodMeta() { - return $this->csvDeliveryMethodMetaDefault; + return $this->csvDeliveryMethodMetaDef; } /** @@ -247,7 +247,7 @@ public function getCsvDeliveryMethodMeta() */ public function getCsvDeliveryToPrice() { - return $this->csvDeliveryToPriceDefault; + return $this->csvDeliveryToPriceDef; } /** @@ -257,7 +257,7 @@ public function getCsvDeliveryToPrice() */ public function getCsvCleanNameToMethod() { - return $this->csvCleanNameToMethodDefault; + return $this->csvCleanNameToMethodDef; } /** @@ -267,6 +267,6 @@ public function getCsvCleanNameToMethod() */ public function getCsvCleanNameMethodGroup() { - return $this->csvCleanNameMethodGroupDefault; + return $this->csvCleanNameMethodGroupDef; } } From 69f02f71f8818d31c3e0ba4fee0a210490cbd4c0 Mon Sep 17 00:00:00 2001 From: Aaron Rickard Date: Thu, 16 Jun 2016 08:53:59 +0100 Subject: [PATCH 3/8] 48: Apply grumphp refactoring to carrier interface --- src/CarrierInterface.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/CarrierInterface.php b/src/CarrierInterface.php index 8020568..4f2e838 100644 --- a/src/CarrierInterface.php +++ b/src/CarrierInterface.php @@ -19,7 +19,11 @@ * Allows for a generic interface of carrier to account for any future carriers * to be added. * - * @package Meanbee\Royalmail + * @category Meanbee + * @package Meanbee\Royalmail + * @author Meanbee Limited + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library */ interface CarrierInterface { From dd57ef8255b307d052aa9b065b41ce5824a30eaf Mon Sep 17 00:00:00 2001 From: Aaron Rickard Date: Thu, 16 Jun 2016 09:00:17 +0100 Subject: [PATCH 4/8] 48: Apply grumphp changes to data class --- src/Data.php | 70 ++++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/src/Data.php b/src/Data.php index 7d3cdaa..d45e822 100644 --- a/src/Data.php +++ b/src/Data.php @@ -20,8 +20,11 @@ * out of them. Provides methods to interact with the csv files and return sorted * method rates of several formats. * - * @author Meanbee Limited - * @package Meanbee\Royalmail + * @category Meanbee + * @package Meanbee\Royalmail + * @author Meanbee Limited + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library */ class Data { @@ -101,12 +104,14 @@ public function __construct( $_csvCleanNameToMethod, $_csvCleanNameMethodGroup ) { - $this->mappingCountryToZone = $this->csvToArray($_csvCountryCode); - $this->mappingZoneToMethod = $this->csvToArray($_csvZoneToDeliveryMethod); - $this->mappingMethodToMeta = $this->csvToArray($_csvDeliveryMethodMeta); - $this->mappingDeliveryToPrice = $this->csvToArray($_csvDeliveryToPrice); - $this->mappingCleanNameToMethod = $this->csvToArray($_csvCleanNameToMethod); - $this->mappingCleanNameMethodGroup = $this->csvToArray($_csvCleanNameMethodGroup); + $this->mappingCountryToZone = $this->_csvToArray($_csvCountryCode); + $this->mappingZoneToMethod = $this->_csvToArray($_csvZoneToDeliveryMethod); + $this->mappingMethodToMeta = $this->_csvToArray($_csvDeliveryMethodMeta); + $this->mappingDeliveryToPrice = $this->_csvToArray($_csvDeliveryToPrice); + $this->mappingCleanNameToMethod = $this->_csvToArray($_csvCleanNameToMethod); + $this->mappingCleanNameMethodGroup = $this->_csvToArray( + $_csvCleanNameMethodGroup + ); } /** @@ -134,14 +139,14 @@ public function calculateMethods( $ignore_package_value = false ) { $sortedCountryCodeMethods = [ - $this->getCountryCodeData( + $this->_getCountryCodeData( $country_code, $this->mappingCountryToZone ) ]; $sortedZoneToMethods = [ - $this->getZoneToMethod( + $this->_getZoneToMethod( $sortedCountryCodeMethods, $this->mappingZoneToMethod ) @@ -149,14 +154,14 @@ public function calculateMethods( if ($ignore_package_value) { $sortedMethodToMeta = [ - $this->getMethodToMetaAll( + $this->_getMethodToMetaAll( $sortedZoneToMethods, $this->mappingMethodToMeta ) ]; } else { $sortedMethodToMeta = [ - $this->getMethodToMeta( + $this->_getMethodToMeta( $package_value, $sortedZoneToMethods, $this->mappingMethodToMeta @@ -164,11 +169,10 @@ public function calculateMethods( ]; } - return $this->getMethodToPrice( + return $this->_getMethodToPrice( $package_weight, $sortedMethodToMeta, $this->mappingDeliveryToPrice - ); } @@ -181,13 +185,12 @@ public function calculateMethods( * * @return array */ - private function getCountryCodeData($country_code, $mappingCountryToZone) + private function _getCountryCodeData($country_code, $mappingCountryToZone) { // Get All array items that match the country code $countryCodeData = []; foreach ($mappingCountryToZone as $item) { - if (isset( - $item[self::COUNTRY_CODE]) + if (isset($item[self::COUNTRY_CODE]) && $item[self::COUNTRY_CODE] == $country_code ) { foreach ($item as $keys) { @@ -202,7 +205,6 @@ private function getCountryCodeData($country_code, $mappingCountryToZone) unset($countryCodeData[$key]); } } - $countryCodeData = array_values($countryCodeData); return $countryCodeData; @@ -217,8 +219,10 @@ private function getCountryCodeData($country_code, $mappingCountryToZone) * * @return array */ - private function getZoneToMethod($sortedCountryCodeMethods, $mappingZoneToMethod) - { + private function _getZoneToMethod( + $sortedCountryCodeMethods, + $mappingZoneToMethod + ) { $mappingZoneData = []; foreach ($sortedCountryCodeMethods as $key => $value) { foreach ($value as $zone) { @@ -261,7 +265,7 @@ private function getZoneToMethod($sortedCountryCodeMethods, $mappingZoneToMethod * * @return array */ - private function getMethodToMeta( + private function _getMethodToMeta( $packageValue, $sortedZoneToMethods, $mappingMethodToMeta @@ -296,13 +300,13 @@ private function getMethodToMeta( * correct shipping method, to allow for less text in the delivery * to price csv. * - * @param $package_weight - * @param $sortedMethodToMeta - * @param $mappingDeliveryToPrice + * @param int $package_weight - The weight of the package + * @param array $sortedMethodToMeta - Sorted methods to meta + * @param array $mappingDeliveryToPrice - Sorted delivery to price * * @return array */ - private function getMethodToPrice( + private function _getMethodToPrice( $package_weight, $sortedMethodToMeta, $mappingDeliveryToPrice @@ -348,12 +352,12 @@ private function getMethodToPrice( * of the item. Returns all possible available methods * that are available. * - * @param $sortedZoneToMethods - * @param $mappingMethodToMeta + * @param array $sortedZoneToMethods - Sorted array of zone to methods + * @param array $mappingMethodToMeta - Sorted array of methods to the meta * * @return array */ - private function getMethodToMetaAll($sortedZoneToMethods, $mappingMethodToMeta) + private function _getMethodToMetaAll($sortedZoneToMethods, $mappingMethodToMeta) { $mappingZoneMethodData = []; foreach ($sortedZoneToMethods as $key => $value) { @@ -382,12 +386,14 @@ private function getMethodToMetaAll($sortedZoneToMethods, $mappingMethodToMeta) * @return array * @throws \Exception */ - private function csvToArray($filename = '', $delimiter = ',') + private function _csvToArray($filename = '', $delimiter = ',') { if (!file_exists($filename) || !is_readable($filename)) { - throw new \Exception("Unable to load the Royal Mail price data csv for - '$filename'. Ensure that the data folder contains all the necessary - csvs."); + throw new \Exception( + "Unable to load the Royal Mail price data csv for + '$filename'. Ensure that the data folder contains all the necessary + csvs." + ); } $header = null; From b5b4ef7418c16fa9a3f8a4f62af2f803b20c5ff9 Mon Sep 17 00:00:00 2001 From: Aaron Rickard Date: Thu, 16 Jun 2016 09:02:14 +0100 Subject: [PATCH 5/8] 48: Apply grumphp refactoring, comments to Method class --- src/Method.php | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Method.php b/src/Method.php index b16053e..c56fda4 100644 --- a/src/Method.php +++ b/src/Method.php @@ -19,7 +19,11 @@ * Object class to represent the method object and allow for * creation of method objects by the other classes. * - * @package Meanbee\Royalmail + * @category Meanbee + * @package Meanbee\Royalmail + * @author Meanbee Limited + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library */ class Method { @@ -79,9 +83,28 @@ class Method */ protected $size; - public function __construct($code, $name, $countryCode, $price, - $insuranceValue, $minimumWeight, $maximumWeight, $size = null) - { + /** + * Method constructor. + * + * @param string $code - Country code of method + * @param string $name - Clean shipping code of method + * @param string $countryCode - Country code of method + * @param string $price - Price of method + * @param string $insuranceValue - Insurance value of method + * @param string $minimumWeight - Minimum weight the method can have + * @param string $maximumWeight - Maximum weight the method can have + * @param null $size - Parcel size, only applies to sm and md parcels + */ + public function __construct( + $code, + $name, + $countryCode, + $price, + $insuranceValue, + $minimumWeight, + $maximumWeight, + $size = null + ) { $this->code = $code; $this->name = $name; $this->countryCode = $countryCode; From 052b2e7a15b6932808c617bf5bd61e40b1480f7f Mon Sep 17 00:00:00 2001 From: Aaron Rickard Date: Thu, 16 Jun 2016 14:41:07 +0100 Subject: [PATCH 6/8] 48: Apply grumphp refactoring to data and test class --- src/Data.php | 81 ++++---- tests/CarrierTest.php | 437 ++++++++++++++++++++++++++++++++---------- 2 files changed, 367 insertions(+), 151 deletions(-) diff --git a/src/Data.php b/src/Data.php index d45e822..08e880e 100644 --- a/src/Data.php +++ b/src/Data.php @@ -153,20 +153,16 @@ public function calculateMethods( ]; if ($ignore_package_value) { - $sortedMethodToMeta = [ - $this->_getMethodToMetaAll( - $sortedZoneToMethods, - $this->mappingMethodToMeta - ) - ]; + $sortedMethodToMeta = $this->_getMethodToMetaAll( + $sortedZoneToMethods, + $this->mappingMethodToMeta + ); } else { - $sortedMethodToMeta = [ - $this->_getMethodToMeta( - $package_value, - $sortedZoneToMethods, - $this->mappingMethodToMeta - ) - ]; + $sortedMethodToMeta = $this->_getMethodToMeta( + $package_value, + $sortedZoneToMethods, + $this->mappingMethodToMeta + ); } return $this->_getMethodToPrice( @@ -270,7 +266,7 @@ private function _getMethodToMeta( $sortedZoneToMethods, $mappingMethodToMeta ) { - $mappingZoneMethodData = []; + $mappingZoneMethodData = array(); foreach ($sortedZoneToMethods as $key => $value) { foreach ($value as $method) { foreach ($mappingMethodToMeta as $item) { @@ -280,15 +276,13 @@ private function _getMethodToMeta( if ($packageValue >= $item[self::METHOD_MIN_VALUE] && $packageValue <= $item[self::METHOD_MAX_VALUE] ) { - $mappingZoneMethodData[] = [$item]; + $mappingZoneMethodData[$item[0]] = $item; } } } } } - $mappingZoneMethodData = array_values($mappingZoneMethodData); - return $mappingZoneMethodData; } @@ -312,37 +306,30 @@ private function _getMethodToPrice( $mappingDeliveryToPrice ) { $mappingDeliveryToPriceData = []; - foreach ($sortedMethodToMeta as $method) { - foreach ($method as $meta) { - foreach ($meta as $key => $value) { - foreach ($value as $methodData) { - foreach ($mappingDeliveryToPrice as $item) { - if (isset($item[self::SHIPPING_METHOD]) && $item[self::SHIPPING_METHOD] == $methodData) { - if ($package_weight >= $item[self::METHOD_MIN_WEIGHT] && $package_weight <= $item[self::METHOD_MAX_WEIGHT]) { - $resultArray = [ - 'shippingMethodName' => $item[self::SHIPPING_METHOD], - 'minimumWeight' => (double) $item[self::METHOD_MIN_WEIGHT], - 'maximumWeight' => (double) $item[self::METHOD_MAX_WEIGHT], - 'methodPrice' => (double) $item[self::METHOD_PRICE], - 'insuranceValue' => (int) $item[self::METHOD_INSURANCE_VALUE], - 'shippingMethodNameClean' => $value[self::METHOD_NAME_CLEAN] - ]; - - if (isset($item[self::METHOD_SIZE])) { - $resultArray['size'] = $item[self::METHOD_SIZE]; - } - - $mappingDeliveryToPriceData[] = $resultArray; - } - } - } - } + foreach ($mappingDeliveryToPrice as $item) { + if (isset($item[self::SHIPPING_METHOD]) + && isset($sortedMethodToMeta[$item[self::SHIPPING_METHOD]]) + && $package_weight >= $item[self::METHOD_MIN_WEIGHT] + && $package_weight <= $item[self::METHOD_MAX_WEIGHT] + ) { + $data = $sortedMethodToMeta[$item[self::SHIPPING_METHOD]]; + $resultArray = [ + 'shippingMethodName' => $item[self::SHIPPING_METHOD], + 'minimumWeight' => (double) $item[self::METHOD_MIN_WEIGHT], + 'maximumWeight' => (double) $item[self::METHOD_MAX_WEIGHT], + 'methodPrice' => (double) $item[self::METHOD_PRICE], + 'insuranceValue' => (int) $item[self::METHOD_INSURANCE_VALUE], + 'shippingMethodNameClean' => $data[self::METHOD_NAME_CLEAN] + ]; + + if (isset($item[self::METHOD_SIZE])) { + $resultArray['size'] = $item[self::METHOD_SIZE]; } + + $mappingDeliveryToPriceData[] = $resultArray; } } - $mappingDeliveryToPriceData = array_values($mappingDeliveryToPriceData); - return $mappingDeliveryToPriceData; } @@ -359,21 +346,19 @@ private function _getMethodToPrice( */ private function _getMethodToMetaAll($sortedZoneToMethods, $mappingMethodToMeta) { - $mappingZoneMethodData = []; + $mappingZoneMethodData = array(); foreach ($sortedZoneToMethods as $key => $value) { foreach ($value as $method) { foreach ($mappingMethodToMeta as $item) { if (isset($item[self::SHIPPING_METHOD]) && $item[self::SHIPPING_METHOD] == $method ) { - $mappingZoneMethodData[] = [$item]; + $mappingZoneMethodData[$item[0]] = $item; } } } } - $mappingZoneMethodData = array_values($mappingZoneMethodData); - return $mappingZoneMethodData; } diff --git a/tests/CarrierTest.php b/tests/CarrierTest.php index 979c90a..7031c81 100644 --- a/tests/CarrierTest.php +++ b/tests/CarrierTest.php @@ -18,7 +18,11 @@ * Class CarrierTest * Test class to run phpunit tests on the library. * - * @package Meanbee\Royalmail + * @category Meanbee + * @package Meanbee\Royalmail + * @author Meanbee Limited + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library */ class CarrierTest extends \PHPUnit_Framework_TestCase { @@ -30,37 +34,52 @@ class CarrierTest extends \PHPUnit_Framework_TestCase const METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV = 1; const METHOD_CLEAN_NAME_ROW_CLEANNAMEGROUP_CSV = 1; const METHOD_CLEAN_NAME_GROUP_CLEANNAME_CSV = 0; - const METHOD_CLEAN_NAME_GROUP_CLEANNAMEGROUP_CSV = 0; + const METHOD_CLEAN_NAME_GROUP_CLNNAMEGRP_CSV = 0; const INSURANCE_ROW_META_CSV = 3; const INSURANCE_ROW_PRICE_CSV = 4; const INSURANCE_ROW_CLEANNAME_CSV = 5; - /** @var Carrier $carrier */ - private $carrier; + /** + * Variable for carrier class + * + * @var Carrier $_carrier + */ + private $_carrier; - /** @var Data $dataClass */ - private $dataClass; - private $emptyArray; - private $testDataClassArray; + /** + * Variable for data class + * + * @var Data $_dataClass + */ + private $_dataClass; + + private $_emptyArray; + private $_testDataClassArray; /** * Setup the necessary classes and data + * + * @return null */ public function setUp() { - /** @var Carrier */ - $this->carrier = new Carrier(); - $this->dataClass = new Data( - $this->carrier->getCsvCountryCode(), - $this->carrier->getCsvZoneToDeliveryMethod(), - $this->carrier->getCsvDeliveryMethodMeta(), - $this->carrier->getCsvDeliveryToPrice(), - $this->carrier->getCsvCleanNameToMethod(), - $this->carrier->getCsvCleanNameMethodGroup() + /** + * Set the carrier class + * + * @var Carrier + */ + $this->_carrier = new Carrier(); + $this->_dataClass = new Data( + $this->_carrier->getCsvCountryCode(), + $this->_carrier->getCsvZoneToDeliveryMethod(), + $this->_carrier->getCsvDeliveryMethodMeta(), + $this->_carrier->getCsvDeliveryToPrice(), + $this->_carrier->getCsvCleanNameToMethod(), + $this->_carrier->getCsvCleanNameMethodGroup() ); - $this->emptyArray = []; - $this->testDataClassArray = array( + $this->_emptyArray = []; + $this->_testDataClassArray = array( 'shippingMethodName' => 'test', 'minimumWeight' => 1.00, 'maximumWeight' => 5.00, @@ -73,31 +92,37 @@ public function setUp() /** * Cleans up the used classes + * + * @return null */ public function tearDown() { - $this->carrier = null; - $this->dataClass = null; + $this->_carrier = null; + $this->_dataClass = null; } /** * Test to ensure that the calculate method class is returning + * + * @return null */ public function testRoyalmailClassRealValues() { - $this->assertNotEmpty($this->carrier->getRates('GB', 20, 0.050)); + $this->assertNotEmpty($this->_carrier->getRates('GB', 20, 0.050)); } /** * Test to ensure that the calculate method class is returning rates with * the ignore insurance flag set to true. 37 methods are expected to be * returned. + * + * @return null */ public function testRoyalmailClassRealValuesAll() { $this->assertCount( 37, - $this->carrier->getRates('GB', 20, 0.050, true), + $this->_carrier->getRates('GB', 20, 0.050, true), "Array size from getRates did not match on the expected size of 37 methods returned." ); @@ -107,159 +132,349 @@ public function testRoyalmailClassRealValuesAll() * Test to ensure that the calculate method class is returning rates with * the ignore insurance flag set to false. 30 methods are expected to be * returned. + * + * @return null */ public function testRoyalmailClassRealValuesAllFalse() { $this->assertCount( 30, - $this->carrier->getRates('GB', 20, 0.050, false), + $this->_carrier->getRates('GB', 20, 0.050, false), "Array size from getRates did not match on - the expected size of 37 methods returned."); + the expected size of 37 methods returned." + ); } /** * Test to compare the returned data from the Data class to expected values + * + * @return null */ public function testRoyalmailMethodRealValues() { - $calculatedMethods = $this->dataClass->calculateMethods('GB', 19.99, 0.050); + $calculatedMethods = $this->_dataClass->calculateMethods('GB', 19.99, 0.050); foreach ($calculatedMethods as $calculatedMethod => $arrayContents) { - $this->assertEquals(gettype($this->testDataClassArray['shippingMethodName']), - gettype($arrayContents['shippingMethodName']), "shippingMethodName array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['minimumWeight']), - gettype($arrayContents['minimumWeight']), "minimumWeight array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['maximumWeight']), - gettype($arrayContents['maximumWeight']), "maximumWeight array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['methodPrice']), - gettype($arrayContents['methodPrice']), "methodPrice array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['insuranceValue']), - gettype($arrayContents['insuranceValue']), "insuranceValue array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['shippingMethodNameClean']), - gettype($arrayContents['shippingMethodNameClean']), "shippingMethodNameClean array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['size']), gettype($arrayContents['size']), - "size array value not equal to correct type."); + $this->assertEquals( + gettype( + $this->_testDataClassArray['shippingMethodName'] + ), + gettype($arrayContents['shippingMethodName']), + "shippingMethodName array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['minimumWeight'] + ), + gettype($arrayContents['minimumWeight']), + "minimumWeight array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['maximumWeight'] + ), + gettype($arrayContents['maximumWeight']), + "maximumWeight array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['methodPrice'] + ), + gettype($arrayContents['methodPrice']), + "methodPrice array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['insuranceValue'] + ), + gettype($arrayContents['insuranceValue']), + "insuranceValue array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['shippingMethodNameClean'] + ), + gettype($arrayContents['shippingMethodNameClean']), + "shippingMethodNameClean array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['size'] + ), + gettype($arrayContents['size']), + "size array value not equal to correct type." + ); } } /** - * Test to ensure the only the expected empty array is returned from incorrect data to the data class + * Test to ensure the only the expected empty array + * is returned from incorrect data to the data class + * + * @return null */ public function testRoyalmailMethodFake() { - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GASD', "aSDASD", "ASDASD")); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(123123123, "asdasd", "asdadasd")); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(123123, 123123, "ASDASD")); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(123123123, 123123123, 123123123)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GB', "aSD!!ASD", 0.100)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GB', 123123123123, 0.100)); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GASD', "aSDASD", "ASDASD" + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 123123123, "asdasd", "asdadasd" + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 123123, 123123, "ASDASD" + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 123123123, 123123123, 123123123 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GB', "aSD!!ASD", 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GB', 123123123123, 0.100 + ) + ); } /** - * Test to ensure that only the expected empty array is returned from null and incorrect data + * Test to ensure that only the expected empty array + * is returned from null and incorrect data * from the Data class + * + * @return null */ public function testRoyalmailMethodNull() { - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(null, 123123123123, 0.100)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(null, null, 0.100)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GB', null, 0.100)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GB', null, null)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GB', 123123123123, null)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(null, null, null)); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + null, 123123123123, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + null, null, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GB', null, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GB', null, null + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GB', 123123123123, null + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + null, null, null + ) + ); } /** * Test to ensure that only the expected empty array is returned from incorrect * data from the CalculateMethod class + * + * @return null */ public function testRoyalmailClassFake() { $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates('GASD', "aSDASD", "ASDASD")); + $this->_emptyArray, + $this->_carrier->getRates( + 'GASD', "aSDASD", "ASDASD" + ) + ); $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates(123123123, "asdasd", "asdadasd")); + $this->_emptyArray, + $this->_carrier->getRates( + 123123123, "asdasd", "asdadasd" + ) + ); $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates(123123, 123123, "ASDASD")); + $this->_emptyArray, + $this->_carrier->getRates( + 123123, 123123, "ASDASD" + ) + ); $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates(123123123, 123123123, 123123123)); + $this->_emptyArray, + $this->_carrier->getRates( + 123123123, 123123123, 123123123 + ) + ); $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates('GB', "aSD!!ASD", 0.100)); + $this->_emptyArray, + $this->_carrier->getRates( + 'GB', "aSD!!ASD", 0.100 + ) + ); $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates('GB', 123123123123, 0.100)); + $this->_emptyArray, + $this->_carrier->getRates( + 'GB', 123123123123, 0.100 + ) + ); } /** * Test to ensure that only the expected empty array is returned from null * and incorrect data from the CalculateMethod class + * + * @return null */ public function testRoyalmailClassNull() { - $this->assertEquals($this->emptyArray, $this->carrier->getRates(null, 123123123123, 0.100)); - $this->assertEquals($this->emptyArray, $this->carrier->getRates(null, null, 0.100)); - $this->assertEquals($this->emptyArray, $this->carrier->getRates('GB', null, 0.100)); - $this->assertEquals($this->emptyArray, $this->carrier->getRates('GB', null, null)); - $this->assertEquals($this->emptyArray, $this->carrier->getRates('GB', 123123123123, null)); - $this->assertEquals($this->emptyArray, $this->carrier->getRates(null, null, null)); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + null, 123123123123, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + null, null, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + 'GB', null, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + 'GB', null, null + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + 'GB', 123123123123, null + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + null, null, null + ) + ); } /** - * Test co compare the meta names vs the clean name to check that the correct value - * exists and is being used. + * Test co compare the meta names vs the clean name to check that + * the correct value exists and is being used. + * + * @return null */ public function testMethodToMetaVsCleanName() { - foreach ($this->dataClass->getMappingMethodToMeta() as $array => $data) { + foreach ($this->_dataClass->getMappingMethodToMeta() + as $array => $data) { $methodNotExist = false; - foreach ($this->dataClass->getMappingCleanNameToMethod() as $method => $methodData) { + foreach ($this->_dataClass->getMappingCleanNameToMethod() + as $method => $methodData) { // Check the the names are equal - if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_CLEANNAME_CSV]) { + if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_CLEANNAME_CSV] + ) { $methodNotExist = true; - $this->assertEquals($data[self::METHOD_CLEAN_NAME_ROW_META_CSV], + $this->assertEquals( + $data[self::METHOD_CLEAN_NAME_ROW_META_CSV], $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV], - sprintf("Clean names %s and %s were not equal", $data[self::METHOD_CLEAN_NAME_ROW_META_CSV], - $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV])); + sprintf( + "Clean names %s and %s were not equal", + $data[self::METHOD_CLEAN_NAME_ROW_META_CSV], + $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV] + ) + ); } } - $this->assertTrue($methodNotExist, - sprintf("%s was not found in CleanNameToMethod csv$.", $data[self::METHOD_NAME_ROW_META_CSV])); + + $this->assertTrue( + $methodNotExist, + sprintf( + "%s was not found in CleanNameToMethod csv$.", + $data[self::METHOD_NAME_ROW_META_CSV] + ) + ); } } /** * Test for insurance value checking that the correct insurance value is being * used in the CSV files + * + * @return null */ public function testInsuranceValue() { - foreach ($this->dataClass->getMappingMethodToMeta() as $array => $data) { - foreach ($this->dataClass->getMappingDeliveryToPrice() as $method => $methodData) { - if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_PRICE_CSV]) { + foreach ($this->_dataClass->getMappingMethodToMeta() as $array => $data) { + foreach ($this->_dataClass->getMappingDeliveryToPrice() + as $method => $methodData) { + if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_PRICE_CSV] + ) { if ($methodData[self::INSURANCE_ROW_PRICE_CSV] != "") { - $this->assertEquals($data[self::INSURANCE_ROW_META_CSV], + $this->assertEquals( + $data[self::INSURANCE_ROW_META_CSV], $methodData[self::INSURANCE_ROW_PRICE_CSV], - sprintf("Insurance values %s from mappingMethodToMeta and %s from mappingDeliveryToPrice were not equal.", + sprintf( + "Insurance values %s from mappingMethodToMeta and + %s from mappingDeliveryToPrice were not equal.", $data[self::INSURANCE_ROW_META_CSV], - $methodData[self::INSURANCE_ROW_PRICE_CSV])); + $methodData[self::INSURANCE_ROW_PRICE_CSV] + ) + ); } } } } - foreach ($this->dataClass->getMappingMethodToMeta() as $array => $data) { - foreach ($this->dataClass->getMappingCleanNameToMethod() as $method => $methodData) { - if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_CLEANNAME_CSV]) { + foreach ($this->_dataClass->getMappingMethodToMeta() as $array => $data) { + foreach ($this->_dataClass->getMappingCleanNameToMethod() + as $method => $methodData) { + if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_CLEANNAME_CSV] + ) { if ($methodData[self::INSURANCE_ROW_CLEANNAME_CSV] != "") { - $this->assertEquals($data[self::INSURANCE_ROW_META_CSV], + $this->assertEquals( + $data[self::INSURANCE_ROW_META_CSV], $methodData[self::INSURANCE_ROW_CLEANNAME_CSV], - sprintf("Insurance values %s from mappingMethodToMeta and %s from mappingCleanNameToMethod were not equal.", + sprintf( + "Insurance values %s from mappingMethodToMeta and + %s from mappingCleanNameToMethod were not equal.", $data[self::INSURANCE_ROW_META_CSV], - $methodData[self::INSURANCE_ROW_CLEANNAME_CSV])); + $methodData[self::INSURANCE_ROW_CLEANNAME_CSV] + ) + ); } } } @@ -269,17 +484,27 @@ public function testInsuranceValue() /** * Test to compare the method clean name vs the method group, ensuring that * the clean names are correct and exists. + * + * @return null */ public function testCleanNameVsMethodGroup() { - foreach ($this->dataClass->getMappingCleanNameToMethod() as $array => $data) { - foreach ($this->dataClass->getMappingCleanNameMethodGroup() as $method => $methodData) { - if ($data[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV] == $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAMEGROUP_CSV]) { - $this->assertEquals($data[self::METHOD_CLEAN_NAME_GROUP_CLEANNAME_CSV], - $methodData[self::METHOD_CLEAN_NAME_GROUP_CLEANNAMEGROUP_CSV], - sprintf("Clean names %s from mappingCleanNameToMethod and %s from mappingCleanNameToMethodGroup were not equal.", + foreach ($this->_dataClass->getMappingCleanNameToMethod() + as $array => $data) { + foreach ($this->_dataClass->getMappingCleanNameMethodGroup() + as $method => $methodData) { + if ($data[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV] == $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAMEGROUP_CSV] + ) { + $this->assertEquals( + $data[self::METHOD_CLEAN_NAME_GROUP_CLEANNAME_CSV], + $methodData[self::METHOD_CLEAN_NAME_GROUP_CLNNAMEGRP_CSV], + sprintf( + "Clean names %s from mappingCleanNameToMethod and + %s from mappingCleanNameToMethodGroup were not equal.", $data[self::METHOD_CLEAN_NAME_GROUP_CLEANNAME_CSV], - $methodData[self::METHOD_CLEAN_NAME_GROUP_CLEANNAMEGROUP_CSV])); + $methodData[self::METHOD_CLEAN_NAME_GROUP_CLNNAMEGRP_CSV] + ) + ); } } } @@ -287,11 +512,17 @@ public function testCleanNameVsMethodGroup() /** * Test get a full list of royal mail methods + * + * @return null */ public function testGetAllMethods() { - /** @var array $methods */ - $methods = $this->carrier->getAllMethods(); + /** + * All methods from the carrier class + * + * @var array $methods + */ + $methods = $this->_carrier->getAllMethods(); $this->assertInternalType('array', $methods); $this->assertTrue(count($methods) > 0); From d7bd6d40d5d26970301e2a8731daff820403e3d9 Mon Sep 17 00:00:00 2001 From: Aaron Rickard Date: Thu, 16 Jun 2016 15:26:15 +0100 Subject: [PATCH 7/8] 48: Apply final grumphp refactoring to data and test class --- src/Data.php | 80 ++++++++++++++++++++++--------------------- tests/CarrierTest.php | 6 ++-- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/Data.php b/src/Data.php index 08e880e..2c7e006 100644 --- a/src/Data.php +++ b/src/Data.php @@ -184,7 +184,7 @@ public function calculateMethods( private function _getCountryCodeData($country_code, $mappingCountryToZone) { // Get All array items that match the country code - $countryCodeData = []; + $countryCodeData = array(); foreach ($mappingCountryToZone as $item) { if (isset($item[self::COUNTRY_CODE]) && $item[self::COUNTRY_CODE] == $country_code @@ -219,7 +219,7 @@ private function _getZoneToMethod( $sortedCountryCodeMethods, $mappingZoneToMethod ) { - $mappingZoneData = []; + $mappingZoneData = array(); foreach ($sortedCountryCodeMethods as $key => $value) { foreach ($value as $zone) { foreach ($mappingZoneToMethod as $item) { @@ -250,42 +250,6 @@ private function _getZoneToMethod( return $mappingZoneData; } - /** - * Method to return a 2d array of the meta data for each - * given allowed shipping method and the given package - * value. - * - * @param int $packageValue - Package value to filter methods on - * @param array $sortedZoneToMethods - SortedZoneToMethods to filter with - * @param array $mappingMethodToMeta - MethodToMeta to filter on - * - * @return array - */ - private function _getMethodToMeta( - $packageValue, - $sortedZoneToMethods, - $mappingMethodToMeta - ) { - $mappingZoneMethodData = array(); - foreach ($sortedZoneToMethods as $key => $value) { - foreach ($value as $method) { - foreach ($mappingMethodToMeta as $item) { - if (isset($item[self::SHIPPING_METHOD]) - && $item[self::SHIPPING_METHOD] == $method - ) { - if ($packageValue >= $item[self::METHOD_MIN_VALUE] - && $packageValue <= $item[self::METHOD_MAX_VALUE] - ) { - $mappingZoneMethodData[$item[0]] = $item; - } - } - } - } - } - - return $mappingZoneMethodData; - } - /** * Method to return a 2d array of sorted shipping methods based on * the weight of the item and the allowed shipping methods. Returns @@ -305,7 +269,7 @@ private function _getMethodToPrice( $sortedMethodToMeta, $mappingDeliveryToPrice ) { - $mappingDeliveryToPriceData = []; + $mappingDeliveryToPriceData = array(); foreach ($mappingDeliveryToPrice as $item) { if (isset($item[self::SHIPPING_METHOD]) && isset($sortedMethodToMeta[$item[self::SHIPPING_METHOD]]) @@ -330,9 +294,47 @@ private function _getMethodToPrice( } } + $mappingDeliveryToPriceData = array_values($mappingDeliveryToPriceData); + return $mappingDeliveryToPriceData; } + /** + * Method to return a 2d array of the meta data for each + * given allowed shipping method and the given package + * value. + * + * @param int $packageValue - Package value to filter methods on + * @param array $sortedZoneToMethods - SortedZoneToMethods to filter with + * @param array $mappingMethodToMeta - MethodToMeta to filter on + * + * @return array + */ + private function _getMethodToMeta( + $packageValue, + $sortedZoneToMethods, + $mappingMethodToMeta + ) { + $mappingZoneMethodData = array(); + foreach ($sortedZoneToMethods as $key => $value) { + foreach ($value as $method) { + foreach ($mappingMethodToMeta as $item) { + if (isset($item[self::SHIPPING_METHOD]) + && $item[self::SHIPPING_METHOD] == $method + ) { + if ($packageValue >= $item[self::METHOD_MIN_VALUE] + && $packageValue <= $item[self::METHOD_MAX_VALUE] + ) { + $mappingZoneMethodData[$item[0]] = $item; + } + } + } + } + } + + return $mappingZoneMethodData; + } + /** * Method to return a 2d array of the meta data for each * given allowed shipping method, not based on the price diff --git a/tests/CarrierTest.php b/tests/CarrierTest.php index 7031c81..edfd037 100644 --- a/tests/CarrierTest.php +++ b/tests/CarrierTest.php @@ -34,7 +34,7 @@ class CarrierTest extends \PHPUnit_Framework_TestCase const METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV = 1; const METHOD_CLEAN_NAME_ROW_CLEANNAMEGROUP_CSV = 1; const METHOD_CLEAN_NAME_GROUP_CLEANNAME_CSV = 0; - const METHOD_CLEAN_NAME_GROUP_CLNNAMEGRP_CSV = 0; + const METHOD_CLEAN_NAME_GROUP_CLEANNAMEGROUP_CSV = 0; const INSURANCE_ROW_META_CSV = 3; const INSURANCE_ROW_PRICE_CSV = 4; const INSURANCE_ROW_CLEANNAME_CSV = 5; @@ -497,12 +497,12 @@ public function testCleanNameVsMethodGroup() ) { $this->assertEquals( $data[self::METHOD_CLEAN_NAME_GROUP_CLEANNAME_CSV], - $methodData[self::METHOD_CLEAN_NAME_GROUP_CLNNAMEGRP_CSV], + $methodData[self::METHOD_CLEAN_NAME_GROUP_CLEANNAMEGROUP_CSV], sprintf( "Clean names %s from mappingCleanNameToMethod and %s from mappingCleanNameToMethodGroup were not equal.", $data[self::METHOD_CLEAN_NAME_GROUP_CLEANNAME_CSV], - $methodData[self::METHOD_CLEAN_NAME_GROUP_CLNNAMEGRP_CSV] + $methodData[self::METHOD_CLEAN_NAME_GROUP_CLEANNAMEGROUP_CSV] ) ); } From 0f91a7fcbd2ae680c675235bec8210129e18b0c8 Mon Sep 17 00:00:00 2001 From: Aaron Rickard Date: Thu, 16 Jun 2016 15:28:46 +0100 Subject: [PATCH 8/8] 48: Swap array() for [] shorthand --- src/Data.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Data.php b/src/Data.php index 2c7e006..348d03f 100644 --- a/src/Data.php +++ b/src/Data.php @@ -184,7 +184,7 @@ public function calculateMethods( private function _getCountryCodeData($country_code, $mappingCountryToZone) { // Get All array items that match the country code - $countryCodeData = array(); + $countryCodeData = []; foreach ($mappingCountryToZone as $item) { if (isset($item[self::COUNTRY_CODE]) && $item[self::COUNTRY_CODE] == $country_code @@ -219,7 +219,7 @@ private function _getZoneToMethod( $sortedCountryCodeMethods, $mappingZoneToMethod ) { - $mappingZoneData = array(); + $mappingZoneData = []; foreach ($sortedCountryCodeMethods as $key => $value) { foreach ($value as $zone) { foreach ($mappingZoneToMethod as $item) { @@ -269,7 +269,7 @@ private function _getMethodToPrice( $sortedMethodToMeta, $mappingDeliveryToPrice ) { - $mappingDeliveryToPriceData = array(); + $mappingDeliveryToPriceData = []; foreach ($mappingDeliveryToPrice as $item) { if (isset($item[self::SHIPPING_METHOD]) && isset($sortedMethodToMeta[$item[self::SHIPPING_METHOD]]) @@ -315,7 +315,7 @@ private function _getMethodToMeta( $sortedZoneToMethods, $mappingMethodToMeta ) { - $mappingZoneMethodData = array(); + $mappingZoneMethodData = []; foreach ($sortedZoneToMethods as $key => $value) { foreach ($value as $method) { foreach ($mappingMethodToMeta as $item) { @@ -348,7 +348,7 @@ private function _getMethodToMeta( */ private function _getMethodToMetaAll($sortedZoneToMethods, $mappingMethodToMeta) { - $mappingZoneMethodData = array(); + $mappingZoneMethodData = []; foreach ($sortedZoneToMethods as $key => $value) { foreach ($value as $method) { foreach ($mappingMethodToMeta as $item) {