Skip to content

Commit

Permalink
[Testing] Use assertEqualsWithDelta() when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Nov 6, 2023
1 parent 94e13a4 commit 5e396df
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion tests/system/Config/BaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function testUseDefaultValueTypeIntAndFloatValues(): void
$dotenv->load();
$config = new SimpleConfig();

Check failure on line 81 in tests/system/Config/BaseConfigTest.php

View workflow job for this annotation

GitHub Actions / Psalm Analysis

UndefinedClass

tests/system/Config/BaseConfigTest.php:81:23: UndefinedClass: Class, interface or enum named SimpleConfig does not exist (see https://psalm.dev/019)

$this->assertSame(0.0, $config->float);
$this->assertEqualsWithDelta(0.0, $config->float, PHP_FLOAT_EPSILON);
$this->assertSame(999, $config->int);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/system/Database/Live/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public function testSelectAvg(): void
{
$result = $this->db->table('job')->selectAvg('id')->get()->getRow();

$this->assertSame(2.5, (float) $result->id);
$this->assertEqualsWithDelta(2.5, (float) $result->id, PHP_FLOAT_EPSILON);
}

public function testSelectAvgWithAlias(): void
{
$result = $this->db->table('job')->selectAvg('id', 'xam')->get()->getRow();

$this->assertSame(2.5, (float) $result->xam);
$this->assertEqualsWithDelta(2.5, (float) $result->xam, PHP_FLOAT_EPSILON);
}

public function testSelectSum(): void
Expand Down
8 changes: 4 additions & 4 deletions tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,12 @@ public function testCastFloat(): void
$entity->second = 3;

$this->assertIsFloat($entity->second);
$this->assertSame(3.0, $entity->second);
$this->assertEqualsWithDelta(3.0, $entity->second, PHP_FLOAT_EPSILON);

$entity->second = '3.6';

$this->assertIsFloat($entity->second);
$this->assertSame(3.6, $entity->second);
$this->assertEqualsWithDelta(3.6, $entity->second, PHP_FLOAT_EPSILON);
}

public function testCastDouble(): void
Expand All @@ -388,12 +388,12 @@ public function testCastDouble(): void
$entity->third = 3;

$this->assertIsFloat($entity->third);
$this->assertSame(3.0, $entity->third);
$this->assertEqualsWithDelta(3.0, $entity->third, PHP_FLOAT_EPSILON);

$entity->third = '3.6';

$this->assertIsFloat($entity->third);
$this->assertSame(3.6, $entity->third);
$this->assertEqualsWithDelta(3.6, $entity->third, PHP_FLOAT_EPSILON);
}

public function testCastString(): void
Expand Down
10 changes: 5 additions & 5 deletions tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ public function testHeaderContentLengthNotSharedBetweenClients(): void
public function testOptionsDelay(): void
{
$request = $this->getRequest();
$this->assertSame(0.0, $request->getDelay());
$this->assertEqualsWithDelta(0.0, $request->getDelay(), PHP_FLOAT_EPSILON);

$options = [
'delay' => 2000,
'headers' => ['fruit' => 'apple'],
];
$request = $this->getRequest($options);
$this->assertSame(2.0, $request->getDelay());
$this->assertEqualsWithDelta(2.0, $request->getDelay(), PHP_FLOAT_EPSILON);
}

public function testPatchSetsCorrectMethod(): void
Expand Down Expand Up @@ -356,10 +356,10 @@ public function testRequestSetsBasicCurlOptions(): void
$this->assertTrue($options[CURLOPT_FRESH_CONNECT]);

$this->assertArrayHasKey(CURLOPT_TIMEOUT_MS, $options);
$this->assertSame(0.0, $options[CURLOPT_TIMEOUT_MS]);
$this->assertEqualsWithDelta(0.0, $options[CURLOPT_TIMEOUT_MS], PHP_FLOAT_EPSILON);

$this->assertArrayHasKey(CURLOPT_CONNECTTIMEOUT_MS, $options);
$this->assertSame(150000.0, $options[CURLOPT_CONNECTTIMEOUT_MS]);
$this->assertEqualsWithDelta(150000.0, $options[CURLOPT_CONNECTTIMEOUT_MS], PHP_FLOAT_EPSILON);
}

public function testAuthBasicOption(): void
Expand Down Expand Up @@ -734,7 +734,7 @@ public function testSendWithDelay(): void
$request->get('products');

// we still need to check the code coverage to make sure this was done
$this->assertSame(0.1, $request->getDelay());
$this->assertEqualsWithDelta(0.1, $request->getDelay(), PHP_FLOAT_EPSILON);
}

public function testSendContinued(): void
Expand Down
10 changes: 5 additions & 5 deletions tests/system/HTTP/CURLRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ public function testHeaderContentLengthNotSharedBetweenClients(): void
public function testOptionsDelay(): void
{
$request = $this->getRequest();
$this->assertSame(0.0, $request->getDelay());
$this->assertEqualsWithDelta(0.0, $request->getDelay(), PHP_FLOAT_EPSILON);

$options = [
'delay' => 2000,
'headers' => ['fruit' => 'apple'],
];
$request = $this->getRequest($options);
$this->assertSame(2.0, $request->getDelay());
$this->assertEqualsWithDelta(2.0, $request->getDelay(), PHP_FLOAT_EPSILON);
}

public function testPatchSetsCorrectMethod(): void
Expand Down Expand Up @@ -339,10 +339,10 @@ public function testRequestSetsBasicCurlOptions(): void
$this->assertTrue($options[CURLOPT_FRESH_CONNECT]);

$this->assertArrayHasKey(CURLOPT_TIMEOUT_MS, $options);
$this->assertSame(0.0, $options[CURLOPT_TIMEOUT_MS]);
$this->assertEqualsWithDelta(0.0, $options[CURLOPT_TIMEOUT_MS], PHP_FLOAT_EPSILON);

$this->assertArrayHasKey(CURLOPT_CONNECTTIMEOUT_MS, $options);
$this->assertSame(150000.0, $options[CURLOPT_CONNECTTIMEOUT_MS]);
$this->assertEqualsWithDelta(150000.0, $options[CURLOPT_CONNECTTIMEOUT_MS], PHP_FLOAT_EPSILON);
}

public function testAuthBasicOption(): void
Expand Down Expand Up @@ -717,7 +717,7 @@ public function testSendWithDelay(): void
$request->get('products');

// we still need to check the code coverage to make sure this was done
$this->assertSame(0.1, $request->getDelay());
$this->assertEqualsWithDelta(0.1, $request->getDelay(), PHP_FLOAT_EPSILON);
}

public function testSendContinued(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public function testCanGetAVariableFromJson(): void
$this->assertSame('buzz', $jsonVar->fizz);
$this->assertSame('buzz', $request->getJsonVar('baz.fizz'));
$this->assertSame(123, $request->getJsonVar('int'));
$this->assertSame(3.14, $request->getJsonVar('float'));
$this->assertEqualsWithDelta(3.14, $request->getJsonVar('float'), PHP_FLOAT_EPSILON);
$this->assertTrue($request->getJsonVar('true'));
$this->assertFalse($request->getJsonVar('false'));
$this->assertNull($request->getJsonVar('null'));
Expand Down Expand Up @@ -379,7 +379,7 @@ public function testGetJsonVarAsArray(): void
$this->assertSame('buzz', $jsonVar['fizz']);
$this->assertSame('bar', $jsonVar['foo']);
$this->assertSame(123, $jsonVar['int']);
$this->assertSame(3.14, $jsonVar['float']);
$this->assertEqualsWithDelta(3.14, $jsonVar['float'], PHP_FLOAT_EPSILON);
$this->assertTrue($jsonVar['true']);
$this->assertFalse($jsonVar['false']);
$this->assertNull($jsonVar['null']);
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Session/Handlers/Database/RedisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function testSavePathTimeoutFloat(): void

$savePath = $this->getPrivateProperty($handler, 'savePath');

$this->assertSame(2.5, $savePath['timeout']);
$this->assertEqualsWithDelta(2.5, $savePath['timeout'], PHP_FLOAT_EPSILON);
}

public function testSavePathTimeoutInt(): void
Expand All @@ -104,7 +104,7 @@ public function testSavePathTimeoutInt(): void

$savePath = $this->getPrivateProperty($handler, 'savePath');

$this->assertSame(10.0, $savePath['timeout']);
$this->assertEqualsWithDelta(10.0, $savePath['timeout'], PHP_FLOAT_EPSILON);
}

public function testOpen(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Throttle/ThrottleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function testFlooding(): void
$throttler = $throttler->setTestTime($time + 10);

$this->assertTrue($throttler->check('127.0.0.1', $rate, MINUTE, 0));
$this->assertSame(10.0, round($this->cache->get('throttler_127.0.0.1')));
$this->assertEqualsWithDelta(10.0, round($this->cache->get('throttler_127.0.0.1')), PHP_FLOAT_EPSILON);
}

/**
Expand Down

0 comments on commit 5e396df

Please sign in to comment.