From 84a760525df329f1ca903a7e1e3eb733350a8d18 Mon Sep 17 00:00:00 2001 From: moriony Date: Thu, 3 Mar 2016 10:41:42 +0300 Subject: [PATCH] fix dimensions normalizer --- src/Tool/DimensionsNormalizer.php | 28 +++++----- tests/Tool/DimensionsNormalizerTest.php | 70 +++++++++++++++++++++++++ tests/Tool/fixtures.yml | 15 ++++++ 3 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 tests/Tool/DimensionsNormalizerTest.php diff --git a/src/Tool/DimensionsNormalizer.php b/src/Tool/DimensionsNormalizer.php index d962e27..9492875 100644 --- a/src/Tool/DimensionsNormalizer.php +++ b/src/Tool/DimensionsNormalizer.php @@ -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()); @@ -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; + } } diff --git a/tests/Tool/DimensionsNormalizerTest.php b/tests/Tool/DimensionsNormalizerTest.php new file mode 100644 index 0000000..1881991 --- /dev/null +++ b/tests/Tool/DimensionsNormalizerTest.php @@ -0,0 +1,70 @@ +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'), + ], + ]; + } +} diff --git a/tests/Tool/fixtures.yml b/tests/Tool/fixtures.yml index 5f1779c..6a4401b 100644 --- a/tests/Tool/fixtures.yml +++ b/tests/Tool/fixtures.yml @@ -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 \ No newline at end of file