Siva
Siva

Reputation: 1

php, mysql Db with unittesting

i have a class in login page for check the user, here how can i implement the unit testing? This is my login page php code:

class login {
function checkuserlogin() {


      $username = $_REQUEST['username'];
      $pwd = $_REQUEST['pwd'];

    $result = mysql_query($selQry);
    $resultset = mysql_num_rows($result);

    while($row = mysql_fetch_array($result))
      {

            if(($row['username']==$username && $row['password']==$password )|| ($row['email'] == $username && $row['password']==$password)) {
              do.. codes
            }

            else {
                                do.. codes
            }

          } 

   }

}

$login = new login();
$login->checkuserlogin();

Help me, Thanks in advance.

Upvotes: 0

Views: 68

Answers (1)

socha23
socha23

Reputation: 10239

Have you tried phpUnit? http://www.phpunit.de/manual/current/en/

It has some support for database testing.

You can test your class by refactoring it and moving authorization code to a method, like this:

class login {
    function checkuserlogin() {

        if (isUserAuthorized($_REQUEST['username'], $_REQUEST['pwd']) {
           do...codes
        } else {
           do...codes
        }
    }

    function isUserAuthorized($username, $pwd) {
        $result = mysql_query($selQry);
        $resultset = mysql_num_rows($result);

        while($row = mysql_fetch_array($result)) {

            if(($row['username']==$username && $row['password']==$pwd )|| ($row['email'] == $username && $row['password']==$pwd)) {
                return true;
            }
        }
        return false;
      } 
}

and then testing isUserAuthorized in isolation, without worrying about mocking a request.

Upvotes: 2

Related Questions