I am trying to unit test in a personal PHP project as a good little programmer, and I would like to do it right. From what I heard, you should check what you should test, this is just the method’s open interface, but I was wondering if this would apply below.
I have a method that generates a reset password token in case the user forgets his password. The method returns one of two things: nothing (null) if everything works fine, or an error code indicating that the user with the specified username does not exist.
If I test only the public interface, how can I be sure that the reset token will be in your database if the username is valid and DOES NOT go to the database if the username is NOT valid? Do I have to execute queries in my tests to verify this? Or am I just assuming my logic sounds?
Now this method is very simple, and this is not the point, but the fact that this situation applies to many other methods. What do you do in the unit test unit tests?
Code, for reference, if necessary:
public function generatePasswordReset($username) { $this->sql='SELECT id FROM users WHERE username = :username'; $this->addParam(':username', $username); $user=$this->query()->fetch(); if (!$user) return self::$E_USER_DOESNT_EXIST; else { $code=md5(uniqid()); $this->addParams(array(':uid' => $user['id'], ':code' => $code, ':duration' => 24
unit testing
ryeguy
source share