-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2828 from magento-borg/MAGETWO-64854
[borg] MAGETWO-64854: Incorrect Refund Logic with Enterprise Rewards can allow for double-refunds
- Loading branch information
Showing
6 changed files
with
184 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\Math; | ||
|
||
/** | ||
* Contains methods to compare float digits. | ||
* | ||
* @api | ||
*/ | ||
class FloatComparator | ||
{ | ||
/** | ||
* Precision for floats comparing. | ||
* | ||
* @var float | ||
*/ | ||
private static $epsilon = 0.00001; | ||
|
||
/** | ||
* Compares two float digits. | ||
* | ||
* @param float $a | ||
* @param float $b | ||
* @return bool | ||
*/ | ||
public function equal(float $a, float $b): bool | ||
{ | ||
return abs($a - $b) <= self::$epsilon; | ||
} | ||
|
||
/** | ||
* Compares if the first argument greater than the second argument. | ||
* | ||
* @param float $a | ||
* @param float $b | ||
* @return bool | ||
*/ | ||
public function greaterThan(float $a, float $b): bool | ||
{ | ||
return ($a - $b) > self::$epsilon; | ||
} | ||
|
||
/** | ||
* Compares if the first argument greater or equal to the second. | ||
* | ||
* @param float $a | ||
* @param float $b | ||
* @return bool | ||
*/ | ||
public function greaterThanOrEqual(float $a, float $b): bool | ||
{ | ||
return $this->equal($a, $b) || $this->greaterThan($a, $b); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
lib/internal/Magento/Framework/Math/Test/Unit/FloatComparatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\Math\Test\Unit; | ||
|
||
use Magento\Framework\Math\FloatComparator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FloatComparatorTest extends TestCase | ||
{ | ||
/** | ||
* @var FloatComparator | ||
*/ | ||
private $comparator; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->comparator = new FloatComparator(); | ||
} | ||
|
||
/** | ||
* Checks a case when `a` and `b` are equal. | ||
* | ||
* @param float $a | ||
* @param float $b | ||
* @param bool $expected | ||
* @dataProvider eqDataProvider | ||
*/ | ||
public function testEq(float $a, float $b, bool $expected) | ||
{ | ||
self::assertEquals($expected, $this->comparator->equal($a, $b)); | ||
} | ||
|
||
/** | ||
* Gets list of variations to compare equal float. | ||
* | ||
* @return array | ||
*/ | ||
public function eqDataProvider(): array | ||
{ | ||
return [ | ||
[10, 10.00001, true], | ||
[10, 10.000001, true], | ||
[10.0000099, 10.00001, true], | ||
[1, 1.0001, false], | ||
[1, -1.00001, false], | ||
]; | ||
} | ||
|
||
/** | ||
* Checks a case when `a` > `b`. | ||
* | ||
* @param float $a | ||
* @param float $b | ||
* @param bool $expected | ||
* @dataProvider gtDataProvider | ||
*/ | ||
public function testGt(float $a, float $b, bool $expected) | ||
{ | ||
self::assertEquals($expected, $this->comparator->greaterThan($a, $b)); | ||
} | ||
|
||
/** | ||
* Gets list of variations to compare if `a` > `b`. | ||
* | ||
* @return array | ||
*/ | ||
public function gtDataProvider(): array | ||
{ | ||
return [ | ||
[10, 10.00001, false], | ||
[10, 10.000001, false], | ||
[10.0000099, 10.00001, false], | ||
[1.0001, 1, true], | ||
[1, -1.00001, true], | ||
]; | ||
} | ||
|
||
/** | ||
* Checks a case when `a` >= `b`. | ||
* | ||
* @param float $a | ||
* @param float $b | ||
* @param bool $expected | ||
* @dataProvider gteDataProvider | ||
*/ | ||
public function testGte(float $a, float $b, bool $expected) | ||
{ | ||
self::assertEquals($expected, $this->comparator->greaterThanOrEqual($a, $b)); | ||
} | ||
|
||
/** | ||
* Gets list of variations to compare if `a` >= `b`. | ||
* | ||
* @return array | ||
*/ | ||
public function gteDataProvider(): array | ||
{ | ||
return [ | ||
[10, 10.00001, true], | ||
[10, 10.000001, true], | ||
[10.0000099, 10.00001, true], | ||
[1.0001, 1, true], | ||
[1, -1.00001, true], | ||
[1.0001, 1.001, false], | ||
]; | ||
} | ||
} |