Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed testsuite with sebastian/comparator release #29751

Merged
merged 8 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\Resources\TestCase;

use PHPUnit\Framework\Assert;

trait ExtendedTestCaseMethodsTrait
{
private function compareWithEpsilon($expected, $actual, $message)
{
$success = false;

// see https://github.com/sebastianbergmann/phpunit/issues/4966#issuecomment-1367081755
if (abs($expected - $actual) < 10 ** -ini_get('precision')) {
$success = true;
}

Assert::assertTrue($success, $message);
}

public function assertEqualsWithEpsilon($expected, $actual, $message = '')
{
if (!is_array($expected)) {
$expectedArray[]['a'] = $expected; // we recreate the structure of the array as in Tools::spreadAmount()
} else {
$expectedArray = $expected;
}

if (!is_array($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['a'], $actualArray[$key]['a'], $message);
}
}
}
5 changes: 4 additions & 1 deletion tests/Unit/Classes/Tax/TaxCalculatorCoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
use PHPUnit\Framework\TestCase;
use Tax;
use TaxCalculator;
use Tests\Resources\TestCase\ExtendedTestCaseMethodsTrait;

class TaxCalculatorCoreTest extends TestCase
{
use ExtendedTestCaseMethodsTrait;

public function testGetTotalRateOK()
{
$tax = new Tax();
Expand Down Expand Up @@ -61,6 +64,6 @@ public function testGetTotalRateBug()

$totalRate = $tax_calculator->getTotalRate();

$this->assertEquals(27.233, $totalRate);
FabienPapet marked this conversation as resolved.
Show resolved Hide resolved
$this->assertEqualsWithEpsilon(27.233, $totalRate);
}
}
5 changes: 4 additions & 1 deletion tests/Unit/Classes/ToolsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/Core/Cart/AmountImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -53,7 +56,7 @@ public function testAdd(): void
$this->assertEquals(4.6, $amount1->getTaxIncluded());
$this->assertEquals(7.2, $amount1->getTaxExcluded());

$this->assertEquals(6.9, $amount2->getTaxIncluded());
$this->assertEqualsWithEpsilon(6.9, $amount2->getTaxIncluded());
$this->assertEquals(10.7, $amount2->getTaxExcluded());
}

Expand Down