From 6cdf55794a96501352cf6f98f830bbf0e789881a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 26 Sep 2022 13:57:50 +0200 Subject: [PATCH 1/8] Fixed testsuite with sebastian/comparator release --- tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php | 2 +- tests/Unit/Classes/ToolsTest.php | 2 +- tests/Unit/Core/Cart/AmountImmutableTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php b/tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php index 0131b08153c66..2c4ee38b727ba 100644 --- a/tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php +++ b/tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php @@ -61,6 +61,6 @@ public function testGetTotalRateBug() $totalRate = $tax_calculator->getTotalRate(); - $this->assertEquals(27.233, $totalRate); + $this->assertEquals(27.232999999999997, $totalRate); } } diff --git a/tests/Unit/Classes/ToolsTest.php b/tests/Unit/Classes/ToolsTest.php index 5c537f2d97d18..4e35db5858d55 100644 --- a/tests/Unit/Classes/ToolsTest.php +++ b/tests/Unit/Classes/ToolsTest.php @@ -247,7 +247,7 @@ public function providerSpreadAmount(): array 'a', ], [ - [['a' => 7.69], ['a' => 4.09], ['a' => 1.8]], + [['a' => 7.6899999999999995], ['a' => 4.09], ['a' => 1.7999999999999998]], -0.32, 2, [['a' => 7.8], ['a' => 4.2], ['a' => 1.9]], 'a', diff --git a/tests/Unit/Core/Cart/AmountImmutableTest.php b/tests/Unit/Core/Cart/AmountImmutableTest.php index b1d810341287f..0ef6807ac3f6b 100644 --- a/tests/Unit/Core/Cart/AmountImmutableTest.php +++ b/tests/Unit/Core/Cart/AmountImmutableTest.php @@ -53,7 +53,7 @@ public function testAdd(): void $this->assertEquals(4.6, $amount1->getTaxIncluded()); $this->assertEquals(7.2, $amount1->getTaxExcluded()); - $this->assertEquals(6.9, $amount2->getTaxIncluded()); + $this->assertEquals(6.8999999999999995, $amount2->getTaxIncluded()); $this->assertEquals(10.7, $amount2->getTaxExcluded()); } From 6df72b062ba28c3568cba8341f958c80918ac388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Thu, 17 Nov 2022 09:47:02 +0100 Subject: [PATCH 2/8] Removed lock on sebastian/comparator --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 8d3c87db55ee2..ee70f0f0df59e 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -123,7 +123,7 @@ jobs: - name: Update phpunit version if: ${{ startsWith(matrix.php, '8.') }} - run: composer update -w --ignore-platform-reqs --no-interaction phpunit/phpunit sebastian/comparator:4.0.6 + run: composer update -w --ignore-platform-reqs --no-interaction phpunit/phpunit - name: Run phpunit run: ./vendor/phpunit/phpunit/phpunit -c tests/Unit/phpunit.xml From 2073b6fbdc8c1b64e9882bcdd8c417e0069bcc64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 17 Feb 2023 11:47:38 +0100 Subject: [PATCH 3/8] Tried to restore comparison with epsilon --- .../TestCase/ExtendedTestCaseMethodsTrait.php | 13 +++++++++++++ tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php | 5 ++++- tests/Unit/Core/Cart/AmountImmutableTest.php | 5 ++++- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php diff --git a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php new file mode 100644 index 0000000000000..2d41111fd706c --- /dev/null +++ b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php @@ -0,0 +1,13 @@ +getTotalRate(); - $this->assertEquals(27.232999999999997, $totalRate); + $this->assertTrue($this->assertEqualsWithEpsilon(27.233, $totalRate)); } } diff --git a/tests/Unit/Core/Cart/AmountImmutableTest.php b/tests/Unit/Core/Cart/AmountImmutableTest.php index 0ef6807ac3f6b..d59fc6f731eaa 100644 --- a/tests/Unit/Core/Cart/AmountImmutableTest.php +++ b/tests/Unit/Core/Cart/AmountImmutableTest.php @@ -30,9 +30,12 @@ use PHPUnit\Framework\TestCase; use PrestaShop\PrestaShop\Core\Cart\AmountImmutable; +use Tests\Resources\TestCase\ExtendedTestCaseMethodsTrait; class AmountImmutableTest extends TestCase { + use ExtendedTestCaseMethodsTrait; + public function testGet(): void { $amount = new AmountImmutable(2.3, 3.5); @@ -53,7 +56,7 @@ public function testAdd(): void $this->assertEquals(4.6, $amount1->getTaxIncluded()); $this->assertEquals(7.2, $amount1->getTaxExcluded()); - $this->assertEquals(6.8999999999999995, $amount2->getTaxIncluded()); + $this->assertTrue($this->assertEqualsWithEpsilon(6.9, $amount2->getTaxIncluded())); $this->assertEquals(10.7, $amount2->getTaxExcluded()); } From 2efbf025f5833b3acdbc3ad2eb1fd21cee7e6e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 20 Feb 2023 17:27:19 +0100 Subject: [PATCH 4/8] Improved tests --- .../TestCase/ExtendedTestCaseMethodsTrait.php | 13 ++++++++++--- tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php | 2 +- tests/Unit/Classes/ToolsTest.php | 2 +- tests/Unit/Core/Cart/AmountImmutableTest.php | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php index 2d41111fd706c..642a5b22ba0ef 100644 --- a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php +++ b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php @@ -2,12 +2,19 @@ namespace Tests\Resources\TestCase; -use PHPUnit\Framework\Constraint\IsEqual; +use PHPUnit\Framework\Assert; trait ExtendedTestCaseMethodsTrait { - public function assertEqualsWithEpsilon($expected, $actual): bool + public function assertEqualsWithEpsilon($expected, $actual, $message = '') { - return abs($actual - $expected) < 0.0000000001; + $success = false; + + // @see https://github.com/sebastianbergmann/phpunit/issues/4966#issuecomment-1367081755 for `0.0000000001` + if (abs($expected - $actual) < 0.0000000001) { + $success = true; + } + + Assert::assertTrue($success); } } diff --git a/tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php b/tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php index 9b8e4b08d7c5e..6506aa6c930f0 100644 --- a/tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php +++ b/tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php @@ -64,6 +64,6 @@ public function testGetTotalRateBug() $totalRate = $tax_calculator->getTotalRate(); - $this->assertTrue($this->assertEqualsWithEpsilon(27.233, $totalRate)); + $this->assertEqualsWithEpsilon(27.233, $totalRate); } } diff --git a/tests/Unit/Classes/ToolsTest.php b/tests/Unit/Classes/ToolsTest.php index 4e35db5858d55..5c537f2d97d18 100644 --- a/tests/Unit/Classes/ToolsTest.php +++ b/tests/Unit/Classes/ToolsTest.php @@ -247,7 +247,7 @@ public function providerSpreadAmount(): array 'a', ], [ - [['a' => 7.6899999999999995], ['a' => 4.09], ['a' => 1.7999999999999998]], + [['a' => 7.69], ['a' => 4.09], ['a' => 1.8]], -0.32, 2, [['a' => 7.8], ['a' => 4.2], ['a' => 1.9]], 'a', diff --git a/tests/Unit/Core/Cart/AmountImmutableTest.php b/tests/Unit/Core/Cart/AmountImmutableTest.php index d59fc6f731eaa..a7f673e620d5c 100644 --- a/tests/Unit/Core/Cart/AmountImmutableTest.php +++ b/tests/Unit/Core/Cart/AmountImmutableTest.php @@ -56,7 +56,7 @@ public function testAdd(): void $this->assertEquals(4.6, $amount1->getTaxIncluded()); $this->assertEquals(7.2, $amount1->getTaxExcluded()); - $this->assertTrue($this->assertEqualsWithEpsilon(6.9, $amount2->getTaxIncluded())); + $this->assertEqualsWithEpsilon(6.9, $amount2->getTaxIncluded()); $this->assertEquals(10.7, $amount2->getTaxExcluded()); } From 93cf97eb9960cd185571ad97c35dc10091f8fbf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 21 Feb 2023 10:36:40 +0100 Subject: [PATCH 5/8] Improved tests --- .../TestCase/ExtendedTestCaseMethodsTrait.php | 22 +++++++++++++++++-- tests/Unit/Classes/ToolsTest.php | 5 ++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php index 642a5b22ba0ef..6f0aaa56b2fe9 100644 --- a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php +++ b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php @@ -6,15 +6,33 @@ trait ExtendedTestCaseMethodsTrait { - public function assertEqualsWithEpsilon($expected, $actual, $message = '') + private function compareWithEpsilon($expected, $actual) { $success = false; - // @see https://github.com/sebastianbergmann/phpunit/issues/4966#issuecomment-1367081755 for `0.0000000001` if (abs($expected - $actual) < 0.0000000001) { $success = true; } Assert::assertTrue($success); } + + public function assertEqualsWithEpsilon($expected, $actual, $message = '') + { + if (!is_array($expected)) { + $expectedArray[] = $expected; + } else { + $expectedArray = $expected; + } + + if (!is_array($actual)) { + $actualArray[] = $actual; + } else { + $actualArray = $actual; + } + + foreach ($expectedArray as $key => $item) { + $this->compareWithEpsilon($item, $actualArray[$key]); + } + } } diff --git a/tests/Unit/Classes/ToolsTest.php b/tests/Unit/Classes/ToolsTest.php index 5c537f2d97d18..58292090fadee 100644 --- a/tests/Unit/Classes/ToolsTest.php +++ b/tests/Unit/Classes/ToolsTest.php @@ -27,10 +27,13 @@ namespace Tests\Unit\Classes; use PHPUnit\Framework\TestCase; +use Tests\Resources\TestCase\ExtendedTestCaseMethodsTrait; use Tools; class ToolsTest extends TestCase { + use ExtendedTestCaseMethodsTrait; + private const PS_ROUND_UP = 0; private const PS_ROUND_DOWN = 1; private const PS_ROUND_HALF_UP = 2; @@ -261,7 +264,7 @@ public function providerSpreadAmount(): array public function testSpreadAmount(array $expectedRows, float $amount, int $precision, array $rows, string $column): void { Tools::spreadAmount($amount, $precision, $rows, $column); - $this->assertEquals(array_values($expectedRows), array_values($rows)); + $this->assertEqualsWithEpsilon(array_values($expectedRows), array_values($rows)); } /** From 5dd4765552908daee362c82b0f7f614e827e0135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 21 Feb 2023 13:38:46 +0100 Subject: [PATCH 6/8] Fixed tests --- tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php index 6f0aaa56b2fe9..7b84457f6a36f 100644 --- a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php +++ b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php @@ -20,19 +20,19 @@ private function compareWithEpsilon($expected, $actual) public function assertEqualsWithEpsilon($expected, $actual, $message = '') { if (!is_array($expected)) { - $expectedArray[] = $expected; + $expectedArray[]['a'] = $expected; // we recreate the structure of the array as in Tools::spreadAmount() } else { $expectedArray = $expected; } if (!is_array($actual)) { - $actualArray[] = $actual; + $actualArray[]['a'] = $actual; // we recreate the structure of the array as in Tools::spreadAmount() } else { $actualArray = $actual; } foreach ($expectedArray as $key => $item) { - $this->compareWithEpsilon($item, $actualArray[$key]); + $this->compareWithEpsilon($item['a'], $actualArray[$key]['a']); } } } From 2480ae291f8d6ec20cfd67f66915fc75e373cafa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 21 Feb 2023 15:52:23 +0100 Subject: [PATCH 7/8] Improved tests --- tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php index 7b84457f6a36f..0f1c09746157b 100644 --- a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php +++ b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php @@ -10,7 +10,8 @@ private function compareWithEpsilon($expected, $actual) { $success = false; - if (abs($expected - $actual) < 0.0000000001) { + // see https://github.com/sebastianbergmann/phpunit/issues/4966#issuecomment-1367081755 + if (abs($expected - $actual) < 10 ** -ini_get('precision')) { $success = true; } From 5ac43d95513b41be5838e3bde72e1cf880817511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 21 Feb 2023 17:14:03 +0100 Subject: [PATCH 8/8] Improved tests --- tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php index 0f1c09746157b..71c8d3cda33f0 100644 --- a/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php +++ b/tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php @@ -6,7 +6,7 @@ trait ExtendedTestCaseMethodsTrait { - private function compareWithEpsilon($expected, $actual) + private function compareWithEpsilon($expected, $actual, $message) { $success = false; @@ -15,7 +15,7 @@ private function compareWithEpsilon($expected, $actual) $success = true; } - Assert::assertTrue($success); + Assert::assertTrue($success, $message); } public function assertEqualsWithEpsilon($expected, $actual, $message = '') @@ -33,7 +33,7 @@ public function assertEqualsWithEpsilon($expected, $actual, $message = '') } foreach ($expectedArray as $key => $item) { - $this->compareWithEpsilon($item['a'], $actualArray[$key]['a']); + $this->compareWithEpsilon($item['a'], $actualArray[$key]['a'], $message); } } }