9951 explained code solutions for 126 technologies


phpunitHow to use JSON in PHPUnit?


JSON can be used in PHPUnit to test the output of a function or method.

Example code

<?php

class MyTest extends \PHPUnit\Framework\TestCase
{
    public function testJson()
    {
        $data = array('name' => 'John', 'age' => 30);
        $json = json_encode($data);
        $this->assertJson($json);
    }
}

Output example

OK (1 test, 1 assertion)

Code explanation

  • $data: An array containing data to be encoded into JSON
  • json_encode($data): Encodes the data into JSON
  • $this->assertJson($json): Asserts that the given string is valid JSON

Helpful links

Edit this code on GitHub