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 order tests with PHPUnit?
- How to mock an interface in PHPUnit?
- How to install PHPUnit with a PHAR file?
- How to log to the console with PHPUnit?
- How to stop PHPUnit on failure?
- How to expect an error with PHPUnit?
- How to expect an exception in PHPUnit?
- How to use the PHPUnit Framework ExceptionWrapper?
- How to increase memory limit in PHPUnit?
- How to use an inline dataprovider in PHPUnit?
See more codes...