Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosomb committed Feb 21, 2023
1 parent 2efbf02 commit 93cf97e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
22 changes: 20 additions & 2 deletions tests/Resources/TestCase/ExtendedTestCaseMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
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

0 comments on commit 93cf97e

Please sign in to comment.