Skip to content

Commit

Permalink
fix dimensions normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
moriony committed Mar 3, 2016
1 parent b362772 commit 84a7605
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/Tool/DimensionsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,8 @@ public function __construct(MathInterface $math)
*/
public function normalize(DimensionsInterface $dimensions)
{
$values = [$dimensions->getLength()];

if ($this->math->greaterThan($dimensions->getWidth(), reset($values))) {
array_unshift($values, $dimensions->getWidth());
} else {
$values[] = $dimensions->getWidth();
}

if ($this->math->greaterThan($dimensions->getHeight(), reset($values))) {
array_unshift($values, $dimensions->getHeight());
} else {
$values[] = $dimensions->getHeight();
}
$values = [$dimensions->getLength(), $dimensions->getWidth(), $dimensions->getHeight()];
usort($values, [$this, 'sort']);

$normalized = new Dimensions();
$normalized->setUnit($dimensions->getUnit());
Expand All @@ -46,4 +35,17 @@ public function normalize(DimensionsInterface $dimensions)

return $normalized;
}

protected function sort($a, $b)
{
if ($this->math->eq($a, $b)) {
return 0;
}

if ($this->math->lessThan($a, $b)) {
return 1;
}

return -1;
}
}
70 changes: 70 additions & 0 deletions tests/Tool/DimensionsNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace EsteIt\ShippingCalculator\Tests\Tool;

use EsteIt\ShippingCalculator\Model\DimensionsInterface;
use EsteIt\ShippingCalculator\Tool\DimensionsNormalizer;
use Moriony\Trivial\Math\NativeMath;

/**
* @group unit
*/
class DimensionsNormalizerTest extends \PHPUnit_Framework_TestCase
{
protected $fixtures;

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

public function getFixture($name)
{
if (!$this->fixtures) {
$loader = new \Nelmio\Alice\Fixtures\Loader();
$this->fixtures = $loader->load(__DIR__.'/fixtures.yml');
}
return $this->fixtures[$name];
}

/**
* @dataProvider provideDimensions
*/
public function testNormalize(DimensionsInterface $dimensions)
{
$math = new NativeMath();
$normalizer = new DimensionsNormalizer($math);
$result = $normalizer->normalize($dimensions);

$this->assertGreaterThanOrEqual($result->getWidth(), $result->getLength());
$this->assertGreaterThanOrEqual($result->getHeight(), $result->getWidth());
$this->assertSame($dimensions->getUnit(), $result->getUnit());
}

/**
* @return array
*/
public function provideDimensions()
{
return [
[
$this->getFixture('dimensions_11_10_10_in'),
],
[
$this->getFixture('dimensions_10_11_10_in'),
],
[
$this->getFixture('dimensions_10_10_11_in'),
],
[
$this->getFixture('dimensions_11_10_9_in'),
],
[
$this->getFixture('dimensions_9_11_10_in'),
],
[
$this->getFixture('dimensions_10_9_11_in'),
],
];
}
}
15 changes: 15 additions & 0 deletions tests/Tool/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,19 @@ EsteIt\ShippingCalculator\Model\Dimensions:
length: 10
width: 10
height: 10
unit: in
dimensions_11_10_9_in:
length: 11
width: 10
height: 9
unit: in
dimensions_9_11_10_in:
length: 9
width: 11
height: 10
unit: in
dimensions_10_9_11_in:
length: 10
width: 9
height: 11
unit: in

0 comments on commit 84a7605

Please sign in to comment.