daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Mar 15th, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace Test\Assertions;
  4.  
  5. /**
  6.  * Trait AssertSJson
  7.  * @package Test\Assertions
  8.  *
  9.  * @mixin \PHPUnit\Framework\TestCase
  10.  */
  11. trait AssertsJson
  12. {
  13.     /**
  14.      * Assert that the JSON data complies with our custom structure
  15.      *
  16.      * @param string $json
  17.      */
  18.     public function assertJsonData(string $json)
  19.     {
  20.         // First, let's check the data is indeed JSON
  21.         self::assertJson($json);
  22.  
  23.         // For the next parts, we need to convert the JSON to an array
  24.         $array = json_decode($json);
  25.  
  26.         // Now, we need to check if the array contains all the keys
  27.         foreach (['status', 'message'] as $key) {
  28.             self::assertArrayHasKey($key, $array, "JSON doesn't contains the $key key.");
  29.         }
  30.  
  31.         // Finally, check each key has the correct value
  32.         self::assertIsBool($array['status'], 'The status must be a boolean.');
  33.         self::assertIsString($array['message'], 'The message should be a string of text.');
  34.         self::assertNotEmpty($array['message'], 'The message should not be empty.');
  35.  
  36.     }
  37. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top