PHPUnit test for error thrown with wrong argument type
Posted
by
Spencer Mark
on Stack Overflow
See other posts from Stack Overflow
or by Spencer Mark
Published on 2014-08-20T10:09:16Z
Indexed on
2014/08/20
10:20 UTC
Read the original article
Hit count: 185
I'm just starting with PHPUnit and am ok with all assert* methods, but can't figure out how to test for error thrown when the wrong argument is provided to the method - say hinted with array
like so:
public function(array $list) { }
and then tested with null
as argument.
Could someone please provide an example of how to test for this sort of errors?
I've checked quite a few posts on stackoverflow, but couldn't find the answer to this specific issue.
Edit
Ok - just to give you an idea of what I'm testing - here's the ArrayHelper::removeIfValueIsEmpty() method:
public static function removeIfValueIsEmpty(array $array) {
if (empty($array)) {
return array();
}
return array_filter($array, function($value) {
return !Helper::isEmpty($value);
});
}
and now test:
class ArrayHelperTest extends PHPUnit_Framework_TestCase {
public function testRemoveIfValueIsEmpty() {
$this->assertEmpty(
\Cmd\Helper\ArrayHelper::removeIfValueIsEmpty(null),
'\Cmd\Helper\ArrayHelper::removeIfValueIsEmpty method failed (checking with null as argument)'
);
}
}
This throws an error:
PHPUnit_Framework_Error : Argument 1 passed to Cmd\Helper\ArrayHelper::removeIfValueIsEmpty() must be of the type array, null given
© Stack Overflow or respective owner