Skip to content

Commit

Permalink
fix zone calculators, add few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moriony committed Nov 19, 2015
1 parent 56ad958 commit 2ced302
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/CalculatorHandler/Asendia/ZoneCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function calculate($weight)
$math = $this->getMath();

foreach ($this->options['weight_prices'] as $w => $p) {
if ($math->lessOrEqualThan($currentWeight, $weight) && $math->lessOrEqualThan($weight, $w)) {
if ($math->lessOrEqualThan($weight, $w) && $math->greaterThan($weight, $currentWeight)) {
$currentWeight = $w;
$price = $p;
}
Expand Down
2 changes: 1 addition & 1 deletion src/CalculatorHandler/Dhl/ZoneCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function calculate($weight)
$math = $this->getMath();

foreach ($this->options['weight_prices'] as $w => $p) {
if ($math->lessOrEqualThan($currentWeight, $weight) && $math->lessOrEqualThan($weight, $w)) {
if ($math->lessOrEqualThan($weight, $w) && $math->greaterThan($weight, $currentWeight)) {
$currentWeight = $w;
$price = $p;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/CalculatorHandler/Asendia/ZoneCalculatorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace EsteIt\ShippingCalculator\Tests\Calculator;
namespace EsteIt\ShippingCalculator\Tests\CalculatorHandler\Asendia;

use EsteIt\ShippingCalculator\CalculatorHandler\Asendia\ZoneCalculator;

Expand Down Expand Up @@ -59,6 +59,7 @@ public function provideCalculate()
{
return [
[0.5, 31.3],
[4, 42.00],
[4.5, 45.55],
[10, 62.03],
];
Expand Down
96 changes: 96 additions & 0 deletions tests/CalculatorHandler/Dhl/ZoneCalculatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace EsteIt\ShippingCalculator\Tests\CalculatorHandler\Dhl;

use EsteIt\ShippingCalculator\CalculatorHandler\Dhl\ZoneCalculator;

/**
* @group unit
*/
class ZoneCalculatorTest extends \PHPUnit_Framework_TestCase
{
protected $fixtures;
/**
* @var ZoneCalculator
*/
protected $zoneCalculator;

public function setUp()
{
$this->fixtures = null;

$zoneCalculator = new ZoneCalculator([
'name' => 1,
'overweight_rate_factor' => 10,
'weight_prices' => [
['weight' => 1, 'price' => 31.3],
['weight' => 2, 'price' => 34.90],
['weight' => 3, 'price' => 38.45],
['weight' => 4, 'price' => 42.00],
['weight' => 5, 'price' => 45.55],
['weight' => 6, 'price' => 48.84],
['weight' => 7, 'price' => 52.15],
['weight' => 8, 'price' => 55.44],
['weight' => 9, 'price' => 58.73],
['weight' => 10,'price' => 62.03]
],
]);

$this->zoneCalculator = $zoneCalculator;
}

/**
* @dataProvider provideCalculate
*/
public function testCalculate($weight, $expectedPrice)
{
$this->assertEquals($expectedPrice, $this->zoneCalculator->calculate($weight));
}

/**
* @dataProvider provideCalculateException
*/
public function testGetPriceException($exceptionClass, $exceptionMessage, $weight)
{
$this->setExpectedException($exceptionClass, $exceptionMessage);

$zoneCalculator = new ZoneCalculator([
'name' => 1,
'weight_prices' => [],
]);
$zoneCalculator->calculate($weight);
}

public function provideCalculate()
{
return [
[0.5, 31.3],
[4, 42.00],
[4.5, 45.55],
[10, 62.03],
[20, 200],
[20.5, 210],
];
}

public function provideCalculateException()
{
return [
[
'EsteIt\ShippingCalculator\Exception\InvalidWeightException',
'Weight should be a scalar value.',
array(),
],
[
'EsteIt\ShippingCalculator\Exception\InvalidWeightException',
'Can not calculate shipping for this weight.',
100,
],
[
'EsteIt\ShippingCalculator\Exception\InvalidWeightException',
'Weight should be greater than zero.',
-10,
],
];
}
}

0 comments on commit 2ced302

Please sign in to comment.