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 tests in parallel with PHPUnit?
- How to use hooks in PHPUnit?
- What are PHPUnit required extensions
- How to use the PHPUnit Framework TestCase?
- How to run PHPUnit in quiet mode?
- How to show warnings in PHPUnit?
- How to load fixtures with PHPUnit?
- How to use a listener with PHPUnit?
- How to stop PHPUnit on failure?
See more codes...