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 skip a PHPUnit test?
- How to run PHPUnit in quiet mode?
- How to use hooks in PHPUnit?
- How to show warnings in PHPUnit?
- How to stop PHPUnit on failure?
- How to run all PHPUnit tests?
- What are PHPUnit required extensions
- How to mock a query builder with PHPUnit?
- How to install PHPUnit with a PHAR file?
- How to order tests with PHPUnit?
See more codes...