Frank W. Frank W. - 6 months ago 9
PHP Question

PHP unit returns after PHP ends proces

I'm still messing around with php Unit where I found the following scenario:

I have the following code to update a simple user profile which works fine.

public function updateUserData($sessionId, $first_name, $last_name, $email) {
$result = $this->_instance->executeQuery('
UPDATE users SET email=?, first_name=?, last_name=? WHERE users_id=?'
, array($email, ucfirst($first_name), ucfirst($last_name), $sessionId)
);

if($result){
Session::set('detailsUpdated', '<div class="alert alert-success"><strong>Succes! </strong>Your details have been updated!</div');
Redirect::to('profile.php');
}
}


The problem is, I can't really return anything that I can base my UnitTests on. I'm unable to return a true or false because if I do so, after the redirect it doesn't execute and if I do it before the redirect it doesn't execute the redirect anymore.

How can I make a return that I can pickup with a test?

Thanks in advance!

Answer

Your problem arises because the method does things that is not supposed to do. If the method is supposed to update the user data, why would it do a redirection afterwards?

Is it impossible that in the future you will need to do this data update but instead of redirecting, do another action?

Just get rid of the if part and return the update result. Redirections should be performed from controllers, not from data libraries.