Question

I'm using SimpleTest to unit test my PHP classes. I added a custom base class which does a spl_autoload_register to autoload the classes i need.

But now mocking becomes a problem. How can i mock the classes that are loaded through a namespace?

This is basically what i have in my test class.

<?php
require_once('../../GGUnitTestCase.php');

Mock::generate('\Core\Routes\GGRoute');

class TestGGRouter extends GGUnitTestCase
{    
    function TestMethod()
    {
        $route = new \Core\Route\GGRoute(); // <-- This loads up fine!

        // But i need a Mock class, not the real one.
        // How to load a Mock in this case?
        $routeMock = new \Core\Routes\MockGGRoute(); // <-- Doesn't work!

        $this->assertTrue(false);
    }
}
?>

Anyone any idea how i can still mock the classes loaded through a namespace (autoloaded)?

Was it helpful?

Solution

Replace \ (all but preceding one) in the name of your namespaced Class with ::. In your example that'll be...

Mock::generate('Core::Routes::GGRoute');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top