Skip to content

Instantly share code, notes, and snippets.

@geshan

geshan/phpunit-assertions.php Secret

Created Jan 2, 2021
Embed
What would you like to do?
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
class AssertionsTest extends TestCase
{
public function testAssertJsonStringEqualsJsonString()
{
$this->assertJsonStringEqualsJsonString('{"message": "ok"}', json_encode(["message" => "ok"]), 'Check message ok json');
}
public function testRegex()
{
$this->assertRegExp('/Exception 40\d/', 'Exception 501', 'Check if it is exception 40x');
}
public function testAssertStringContainsString()
{
$this->assertStringContainsString('Error', 'Error id is reqired, quote is required', 'Check if word Error is present');
}
public function testAssetEqualsWithDelta()
{
$this->assertEqualsWithDelta(34.3, 34.1, 0.5, '0.5 degree variance in temprature is allwoed');
}
public function testAssertArrayHasKey()
{
$exchangeRates = ['usd' => 0.77, 'euro' => 0.63];
$this->assertArrayHasKey('usd', $exchangeRates, 'Check if USD is available');
$this->assertEquals(0.77, $exchangeRates['usd'], 'Test if USD rate is 0.77');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment