I’m a relatively new convert to unit testing in general and I’ve run into a stumbling block here:
How do I test code that connects to and performs operations on a remote FTP server using PHP’s built-in ftp functions? Some googling turned up a quick mocking option for Java (MockFtpServer), but nothing readily available for PHP.
I have a suspicion the answer may be to create a wrapper class for PHP’s ftp functions that can subsequently be stubbed/mocked to mimic successful/unsuccessful ftp operations, but I’d really appreciate some input from people who are smarter than me!
Please note that I’ve been working with PHPUnit and require assistance with that framework specifically.
As per the request from @hakre the simplified code I want to test looks like the below. I’m essentially asking the best way to test:
public function connect($conn_name, $opt=array())
{
if ($this->ping($conn_name)) {
return TRUE;
}
$r = FALSE;
try {
if ($this->conns[$conn_name] = ftp_connect($opt['host'])) {
ftp_login($this->conns[$conn_name], $opt['user'], $opt['pass']);
}
$r = TRUE;
} catch(FtpException $e) {
// there was a problem with the ftp operation and the
// custom error handler threw an exception
}
return $r;
}
UPDATE/SOLUTION SUMMARY
Problem Summary
I wasn’t sure how to test methods in isolation that required communicating with a remote FTP server. How are you supposed to test being able to connect to an outside resource you have no control over, right?
Solution Summary
Create an adapter class for the FTP operations, (methods: connect, ping, etc). This adapter class is then easily stubbed to return specific values when testing other code that uses the adapter to perform FTP operations.
UPDATE 2
I recently came across a nifty trick using namespaces in your tests that allows you to “mock” PHP’s built-in functions. While the adapter was the right way to go in my particular case, this may be helpful to others in similar situations:
Two approaches that come to mind:
Create two adapters for your FTP class:
A “mock” one that does not actually connect to anything and only returns seeded data.
The FTP class’
connect()method then looks like this:The mock adapter might look something like this:
In your test, you would then seed the adapter with a result and verify that
connect()is getting called appropriately:In the above test case,
setUp()injects the mock adapter so that tests can invoke the FTP class’connect()method without actually triggering an FTP connection. The test then seeds the adapter with a result that will only be returned if the adapter’sconnect()method were called with the correct parameters.The advantage to this method is that you can customize the behavior of the mock object, and it keeps all of the code in one place if you need to use the mock across several test cases.
The disadvantages to this method are that you have to duplicate a lot of functionality that has already been built (see approach #2), and arguably you’ve now introduced another class that you have to write tests for.
An alternative is to use PHPUnit’s mocking framework to create dynamic mock objects in your test. You’ll still need to inject an adapter, but you can create it on-the-fly:
Note that the above test mocks the adapter for the FTP class, not the FTP class itself, as that would be a rather silly thing to do.
This approach has advantages over the previous approach:
There are some disadvantages to this approach, however:
See http://phpunit.de/manual/current/en/test-doubles.html#test-doubles.mock-objects for more information.