Skip to content

Instantly share code, notes, and snippets.

@DarkGhostHunter
Last active March 11, 2019 20:43
Show Gist options
  • Select an option

  • Save DarkGhostHunter/079b7ce7b2f1030127898db92f34d132 to your computer and use it in GitHub Desktop.

Select an option

Save DarkGhostHunter/079b7ce7b2f1030127898db92f34d132 to your computer and use it in GitHub Desktop.
Testing a new assertion
<?php
namespace Tests;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\AssertionFailedError;
class AssertsJsonTest extends TestCase
{
use Assertions\AssertsJson;
public function testAssertJson()
{
$json = json_encode([
'status' => true,
'message' => 'Everything is fine'.
]);
$this->assertJsonData($json);
}
public function testAssertJsonFailsOnInvalidJson()
{
$json = 'this_is_invalid_json';
$this->expectException(AssertionFailedError::class);
$this->assertJsonData($json);
}
/**
* @expectedExceptionMessage JSON doesn't contains the status key.
*/
public function testAssertJsonFailsOnAbsentKeys()
{
$json = json_encode([
'these' => 'are',
'not' => 'valid keys'
]);
$this->expectException(AssertionFailedError::class);
$this->assertJsonData($json);
}
/**
* @expectedExceptionMessage The status must be a boolean.
*/
public function testAssertJsonFailsOnStatusNotBool()
{
$json = json_encode([
'status' => 'ok',
'message' => 'Everything is fine'.
]);
$this->expectException(AssertionFailedError::class);
$this->assertJsonData($json);
}
/**
* @expectedExceptionMessage The message should be a string of text.
*/
public function testAssertJsonFailsOnMessageNotString()
{
$json = json_encode([
'status' => true,
'message' => ['Everything', 'is', 'fine']
]);
$this->expectException(AssertionFailedError::class);
$this->assertJsonData($json);
}
/**
* @expectedExceptionMessage The message should not be empty.
*/
public function testAssertJsonFailsOnMessageEmpty()
{
$json = json_encode([
'status' => true,
'message' => null
]);
$this->expectException(AssertionFailedError::class);
$this->assertJsonData($json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment