phpunitHow to assert that key exists in an array using PHPUnit?
Asserting that a key exists in an array using PHPUnit can be done using the assertArrayHasKey()
method. This method takes two parameters, the first being the array to check and the second being the key to check for.
$array = array('key1' => 'value1', 'key2' => 'value2');
$this->assertArrayHasKey('key1', $array);
The output of the above code will be a success if the key exists in the array, and a failure if it does not.
Code explanation
assertArrayHasKey()
- The method used to assert that a key exists in an array.$array
- The array to check for the key.'key1'
- The key to check for in the array.$this
- The object used to call the method.
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...