Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Test\Assertions;
- /**
- * Trait AssertSJson
- * @package Test\Assertions
- *
- * @mixin \PHPUnit\Framework\TestCase
- */
- trait AssertsJson
- {
- /**
- * Assert that the JSON data complies with our custom structure
- *
- * @param string $json
- */
- public function assertJsonData(string $json)
- {
- // First, let's check the data is indeed JSON
- self::assertJson($json);
- // For the next parts, we need to convert the JSON to an array
- $array = json_decode($json);
- // Now, we need to check if the array contains all the keys
- foreach (['status', 'message'] as $key) {
- self::assertArrayHasKey($key, $array, "JSON doesn't contains the $key key.");
- }
- // Finally, check each key has the correct value
- self::assertIsBool($array['status'], 'The status must be a boolean.');
- self::assertIsString($array['message'], 'The message should be a string of text.');
- self::assertNotEmpty($array['message'], 'The message should not be empty.');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement