I've been considering how you can add unit testing coverage to some large, existing codebase designed in PHP. Many functions both in static and instantiable classes create a call to some library or instantiate an item to be able to obtain connections to memcache and also the database. They typically look something similar to this:

public function getSomeData() {
    $key = "SomeMemcacheKey";
    $cache = get_memcache();

    $results = $cache->get($key);
    if (!$results) {
        $database = new DatabaseObject();
        $sql = "SELECT * from someDatabase.someTable";
        $results = $database->query($sql);

        $cache->set($key, $results);

    return $results;

My co-workers and that i are presently attempting to implement coverage via PHPUnit for any couple of from the new classes we're writing. I've attempted to find away out to produce unit tests within an isolated manner for functions within our existing codebase that resemble the pseudo-code above, but happen to be not successful.

The good examples I have observed in the PHPUnit documentation all depend on getting some method within the class through which a mock object could be mounted on it, for example: $objectBeingTested->attach($mockObject); I checked out SimpleUnit, and saw exactly the same factor there, the mock objects had been passed in to the class via its constructor. This does not leave much room for functions which instantiate their very own database objects.

Can there be in whatever way to mock out these kinds of calls? Can there be another unit testing framework we are able to use? Or shall we be going to need to alter the designs we're using later on to be able to facilitate unit testing?

What Let me do is have the ability to swap out a whole class having a mock class when running tests. For example, the DatabaseObject class might be changed having a mock class, and then any time it's instantiated throughout an evaluation, it might really be a clear case of the mock version.

There's been talk during my team of refactoring our techniques of being able to access the database and memcache in new code, possibly using singletons. I guess that may help when we would write the singleton in a way that it is own demonstration of itself might be changed having a mock object...

This really is my first foray into unit testing. If I am doing the work wrong, please let them know. :)