Skip to content

Commit

Permalink
Adding more unittests and fixing bug regarding Zip+4 style strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
alphazygma committed Nov 3, 2016
1 parent 4084339 commit ea9b305
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 20 deletions.
14 changes: 8 additions & 6 deletions src/Shipping/ZoneChart/Zip2ZoneDefinition.php
Expand Up @@ -2,12 +2,14 @@
namespace Shipping\ZoneChart;

/**
* The <kbd>Zip2ZoneDefinition</kbd>
* The <kbd>Zip2ZoneDefinition</kbd> class represents a definition between a ZipCode range to a Zone.
*
* @author Alejandro Salazar (alejandros@pley.com)
* @version 1.0
* @package
* @subpackage
* @author Alejandro Salazar (alejandros@pley.com)
* @version 1.0
* @license http://www.gnu.org/licenses/lgpl-3.0.en.html GNU LGPLv3
* @link https://github.com/alphazygma/usps-zonechart-php
* @package Shipping
* @subpackage ZoneChart
*/
class Zip2ZoneDefinition
{
Expand Down Expand Up @@ -130,7 +132,7 @@ public function isInRange($zipcode)
{
// Checking if the zipcode is a Zip+4 style, if so, we need to trim the `+4` part
if (is_string($zipcode) && strpos($zipcode, '-') !== FALSE) {
$zipcode = substr($zipcode, strpos($zipcode, '-'));
$zipcode = substr($zipcode, 0, strpos($zipcode, '-'));
}

$zipCodeInt = (int)$zipcode;
Expand Down
25 changes: 16 additions & 9 deletions src/Shipping/ZoneChart/ZoneChart.php
Expand Up @@ -2,12 +2,15 @@
namespace Shipping\ZoneChart;

/**
* The <kbd>ZoneChart</kbd>
* The <kbd>ZoneChart</kbd> class provides the functionality to find the Zone between a Source
* ZipCode and a destination ZipCode.
*
* @author Alejandro Salazar (alejandros@pley.com)
* @version 1.0
* @package
* @subpackage
* @author Alejandro Salazar (alejandros@pley.com)
* @version 1.0
* @license http://www.gnu.org/licenses/lgpl-3.0.en.html GNU LGPLv3
* @link https://github.com/alphazygma/usps-zonechart-php
* @package Shipping
* @subpackage ZoneChart
*/
class ZoneChart
{
Expand Down Expand Up @@ -70,19 +73,23 @@ public function getZoneFor($destinationZipCode)

/**
* Returns the JSON string confugration for the supplied zip code.
* @param string $szipCode
* @param string $zipCode
* @return string
* @throws \Exception If there is no configuration for the source Zipcode
*/
protected function _getJsonConfig($szipCode)
protected function _getJsonConfig($zipCode)
{
// Using the ConfigGenerator to retrieve the path to the configuration given a ZipCode
$configPath = ConfigGenerator::getDataPath($szipCode);
$configPath = ConfigGenerator::getDataPath($zipCode);

if (!file_exists($configPath)) {
throw new \Exception('No configuration found for supplied ZipCode ' . $zipCode);
}

// Obatining the JSON configuration string inside the file
$jsonConfig = file_get_contents($configPath);
if ($jsonConfig === false) {
throw new \Exception('No configuration found for supplied ZipCode ' . $szipCode);
throw new \Exception('Could not read contents of config for ZipCode ' . $zipCode);
}

return $jsonConfig;
Expand Down
127 changes: 127 additions & 0 deletions tests/Shipping/ZoneChart/Zip2ZoneDefinitionTest.php
@@ -0,0 +1,127 @@
<?php /** @copyright Alejandro Salazar (c) 2016 */
namespace Shipping\ZoneChart;

/**
* The <kbd>Zip2ZoneDefinitionTest</kbd> test suite for the <kbd>Zip2ZoneDefinition</kbd> entity class.
*
* @author Alejandro Salazar (alejandros@pley.com)
* @version 1.0
* @license http://www.gnu.org/licenses/lgpl-3.0.en.html GNU LGPLv3
* @link https://github.com/alphazygma/usps-zonechart-php
* @package Shipping
* @subpackage ZoneChart
*/
class Zip2ZoneDefinitionTest extends \PHPUnit_Framework_TestCase
{
public function testFromConfigDirect()
{
$config = ['r' => '005', 'z' => 2];

$z2z = Zip2ZoneDefinition::fromConfig($config);
$this->assertEquals('005', $z2z->getZipRangeStart());
$this->assertEquals('005', $z2z->getZipRangeEnd());
$this->assertEquals(2, $z2z->getZone());
$this->assertFalse($z2z->isException());

// Test inside range ------------------------------------------
// Test with regular zipcode
$this->assertTrue($z2z->isInRange('00510'));
// Test with Zip+4 format
$this->assertTrue($z2z->isInRange('00510-0332'));

// Test outside range ------------------------------------------
// Test with regular zipcode
$this->assertFalse($z2z->isInRange('00610'));
// Test with Zip+4 format
$this->assertFalse($z2z->isInRange('00610-0332'));
}

public function testFromConfigExceptionDirect()
{
$config = ['r' => '95055', 'z' => 2];

$z2z = Zip2ZoneDefinition::fromConfig($config, true);
$this->assertEquals('95055', $z2z->getZipRangeStart());
$this->assertEquals('95055', $z2z->getZipRangeEnd());
$this->assertEquals(2, $z2z->getZone());
$this->assertTrue($z2z->isException());

// Test inside range ------------------------------------------
// Test with regular zipcode
$this->assertTrue($z2z->isInRange('95055'));
// Test with Zip+4 format
$this->assertTrue($z2z->isInRange('95055-0332'));

// Test outside range ------------------------------------------
// Test with regular zipcode
$this->assertFalse($z2z->isInRange('95056'));
// Test with Zip+4 format
$this->assertFalse($z2z->isInRange('95056-0332'));
}

public function testFromConfigRange()
{
$config = ['r' => ['005', '027'], 'z' => 3];

$z2z = Zip2ZoneDefinition::fromConfig($config);
$this->assertEquals('005', $z2z->getZipRangeStart());
$this->assertEquals('027', $z2z->getZipRangeEnd());
$this->assertEquals(3, $z2z->getZone());
$this->assertFalse($z2z->isException());

// Test inside range ------------------------------------------
// Test with regular zipcode at origin
$this->assertTrue($z2z->isInRange('00510'));
// Test with Zip+4 format
$this->assertTrue($z2z->isInRange('00510-0332'));

// Test with regular zipcode in betweend
$this->assertTrue($z2z->isInRange('01510'));
// Test with Zip+4 format
$this->assertTrue($z2z->isInRange('01510-0332'));

// Test with regular zipcode at edge
$this->assertTrue($z2z->isInRange('02710'));
// Test with Zip+4 format
$this->assertTrue($z2z->isInRange('02710-0332'));

// Test outside range ------------------------------------------
// Test with regular zipcode
$this->assertFalse($z2z->isInRange('02810'));
// Test with Zip+4 format
$this->assertFalse($z2z->isInRange('02810-0332'));
}

public function testFromConfigExceptionRange()
{
$config = ['r' => ['95055', '95128'], 'z' => 3];

$z2z = Zip2ZoneDefinition::fromConfig($config, true);
$this->assertEquals('95055', $z2z->getZipRangeStart());
$this->assertEquals('95128', $z2z->getZipRangeEnd());
$this->assertEquals(3, $z2z->getZone());
$this->assertTrue($z2z->isException());

// Test inside range ------------------------------------------
// Test with regular zipcode at origin
$this->assertTrue($z2z->isInRange('95055'));
// Test with Zip+4 format
$this->assertTrue($z2z->isInRange('95055-0332'));

// Test with regular zipcode in betweend
$this->assertTrue($z2z->isInRange('95089'));
// Test with Zip+4 format
$this->assertTrue($z2z->isInRange('95089-0332'));

// Test with regular zipcode at edge
$this->assertTrue($z2z->isInRange('95128'));
// Test with Zip+4 format
$this->assertTrue($z2z->isInRange('95128-0332'));

// Test outside range ------------------------------------------
// Test with regular zipcode
$this->assertFalse($z2z->isInRange('95129'));
// Test with Zip+4 format
$this->assertFalse($z2z->isInRange('95129-0332'));
}
}
36 changes: 31 additions & 5 deletions tests/Shipping/ZoneChart/ZoneChartTest.php
Expand Up @@ -2,12 +2,14 @@
namespace Shipping\ZoneChart;

/**
* The <kbd>ZoneChartTest</kbd>
* The <kbd>ZoneChartTest</kbd> test suite for the <kbd>ZoneChart</kbd> class.
*
* @author Alejandro Salazar (alejandros@pley.com)
* @version 1.0
* @package
* @subpackage
* @author Alejandro Salazar (alejandros@pley.com)
* @version 1.0
* @license http://www.gnu.org/licenses/lgpl-3.0.en.html GNU LGPLv3
* @link https://github.com/alphazygma/usps-zonechart-php
* @package Shipping
* @subpackage ZoneChart
*/
class ZoneChartTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -34,4 +36,28 @@ public function testGetZoneForInvalidZone()
// to make sure that the code execution is performed and we retrieve FALSE for invalid zipcodes
$this->assertFalse($zone);
}

public function testGetZoneForExceptionZone()
{
$zoneChart = new ZoneChart('94040');

$zone = $zoneChart->getZoneFor('96938');

// Since we are not testing zones as these could potentially upon USPS choices, we just need
// to make sure that the code execution is performed and we retrieve an integer value for
// valid zipcodes
// BIG NOTE: If the data is updated upon USPS changes, there is a chance this test might
// fail if the destination zone is no longer considered an exception.
$this->assertNotNull($zone);
$this->assertTrue(is_int($zone));
}

/**
* @expectedException Exception
*/
public function testNoConfigException()
{
// Zone doesn't exist so it should throw an exception
new ZoneChart('00100');
}
}

0 comments on commit ea9b305

Please sign in to comment.