I am trying to write a function which its purpose is to test for a database connection. What it does is it instantiates a Database
object, attempts to connect to the database and return the result.
The pseudo code looks like this
<?php
function testDatabaseLogin() {
// Get the database connection information from a file
// ...
// Database is a class which stores the connection information passed,
// When its connect() method is invoked, it tries to connect. nothing fancy
$conn = new Database($host, $port, $username, $password);
return $conn->connect(); // TRUE on success, FALSE otherwise
}
?>
This code snippet is a simplified version of my situation, but it should be suffice to illustrate my question.
I would like to know how I could use PHPUnit to mock the Database
class in this example. What I want is I can
- Assert that the arguments when instantiating
Database
from this function is correct - Stub the
Database
to controlconnect()
to returnTRUE
orFALSE
, so that I don't need to actually attempt to connect to a real database
I have searched around but I found that it seems like PHPUnit Mock object does not work in this way. Any help is appreciate. And at the bottom line, it is acceptable to change the function implementation if it helps to make the testing easier.
Thank you.