I have the following code that creates a new user. I am using PHPUnit to prepare test cases but however my code coverage is unable to cover the exception case i.e.
throw new Exception(__CLASS__ . ': Invalid data');
.
Can someone please show me how to cover the exception case in phpunit using assertInstanceOf() or something else?
​
/**
* Creates a new user
*
* @param string $email
* @param UserType $UserType
* @param string $prefix
* @param string $customFromName
* @param string $customFromEmail
* @param string $signature
*
* @return UserID
* @throws Exception If Invalid Data is Provided
*/
static public function Create($email, UserType $UserType, $prefix, $customFromName, $customFromEmail, $signature)
{
if (!$UserType instanceof UserType) {
throw new Exception(__CLASS__ . ': Invalid data');
}
$UserID = parent::Create($email, $UserType, $prefix, $customFromName, $customFromEmail, $signature, true);
return $UserID;
}
That will never happen. PHP will complain if you try to pass in something that isn’t a UserType because you have the type hinting in the function signature. Remove that if() it is useless.
if (![color="red"][b]([/b][/color]$UserType instanceof UserType[color="red"][b])[/b][/color]) {
throw new Exception(__CLASS__ . ': Invalid data');
}
Otherwise you’re checking if the negation of the $UserType (which is false), is in instance of UserType, which of course it isn’t, so the if never ever fires)
Even if you fix this, it still won’t work because,
PHP enforces that you give it an instance of UserType, as @QMonkey; already said. If you don’t, you get an error like:
Catchable fatal error: Argument 2 passed to A::Create() must be an instance of UserType, instance of stdClass given
I don’t think you should be testing this at all, since you’re basically getting down to testing the PHP core, which is not what unit tests are for – they are for testing your application. Let the PHP core devs worry about testing the PHP core. If you were to test this you might as well start testing if PHP finds that 1 + 1 equals 2…
That’s what you took from what I said?
That is absolutely NOT what I’m saying! PHPUnit should definitely check for exceptions, but it should not check if PHP does indeed throw a fatal error when you pass an object that does not fit a type hint (in your case when you pass the Create function anything other than a UserType instance).
Your exception is not needed since PHP will check that the object you pass to the Create() function is indeed an instance of UserType. If it isn’t, PHP stops with a catchable fatal error. Just like that. No more code will be executed, because you said the function must get a UserType instance but you gave it something else. There is no need you for to throw an exception yourself, since you’re double checking something that PHP already checked, which makes no sense.
try catch blocks catch Exceptions, they do NOT catch fatal errors, since that’s something else entirely. So to answer to your question, it’s not a good idea, because it doesn’t do anything. Try it and see for yourself, I encourage you to!