phpunitHow to check if a JSON contains a value in PHPUnit?
To check if a JSON contains a value in PHPUnit, you can use the assertJson()
method. This method takes two parameters, the first being the actual JSON string and the second being the expected JSON string.
Example code
$actualJson = '{"name":"John","age":30}';
$expectedJson = '{"name":"John"}';
$this->assertJson($actualJson, $expectedJson);
Output example
OK
The assertJson()
method will compare the two JSON strings and check if the actual JSON string contains the expected JSON string. If the expected JSON string is found in the actual JSON string, the test will pass.
Code explanation
assertJson()
: The method used to check if a JSON contains a value in PHPUnit.$actualJson
: The actual JSON string to be tested.$expectedJson
: The expected JSON string to be found in the actual JSON string.
Helpful links
More of Phpunit
- How to use the PHPUnit Framework TestCase?
- How to stop PHPUnit on failure?
- How to skip a PHPUnit test?
- How to mock a method with different arguments in PHPUnit?
- How to mock a query builder with PHPUnit?
- How to install PHPUnit with a PHAR file?
- How to run tests in parallel with PHPUnit?
- How to disable color output in PHPUnit?
- How to mock an interface in PHPUnit?
- How to run PHPUnit in quiet mode?
See more codes...