vendredi 29 janvier 2016

Bypassing mysqli_connect() error to write unit test

I'm trying to write a set of unit tests using PHPUnit (DISCALIMER: This is my first project attempting to use unit tests for improving code quality).

I'm currently have the a "ConnectionManager" class which is managing the connection to the mysql databases. This class has two member functions: GetDBSetLinks and ConnectWithMysqli.

GetDBSetLinks is a function that takes in an array of connection information for multiple mysql databases (each element contains Hostname, Username, etc. and some other info), and passes this information into ConnectWithMysqli.

ConnectWithMySqli() calculates which overload of the mysqli_connect() function the program should use to connect the the mysql database and then calls that overload using the passed in parameters.

When I pass bad connection information (such as a bad password) into GetDBSetLinks() using my unit test, the bad information is handled properly makes it to the ConnectWithMySqli function as expected. When the bad information hits mysqli_connect(), the function fails and triggers the expected "mysql_connect(): Access denied for user 'root'@'192.168.1.113' (using password:YES)" error. The problem is that this also terminates execution of ConnectWithMySqli(), thus ConnectWithMysqli() will not complete and return the variable that I am trying to test against.

What I would like to do is to keep the program running but trigger an exception that can be handled.

Problem is I just started using try{}catch{} and I'm not really sure the best way to do this. Anyone have any ideas?

My two php files can be seen here

Thanks!

Aucun commentaire:

Enregistrer un commentaire