PHPUnit, Interfaces and Namespaces (Symfony2) PHPUnit, Interfaces and Namespaces (Symfony2) symfony symfony

PHPUnit, Interfaces and Namespaces (Symfony2)


When mocking you need to use the full qualified class path as the mock functionality is not taking the namespace of the calling code or any "use" statements into consideration.

Try

->getMock('\\Symfony\\Component\\Routing\\RouterInterface'); 

and leave out the second parameter. Usually specifying the methods does a lot more worse than good.Only if you want all the other methods to work like before than you should need the second parameter.

Example

<?phpnamespace bar;class MyClass {}namespace foo;use \bar\MyClass;class MockingTest extends \PHPUnit_Framework_TestCase {    public function testMock() {        var_dump($this->getMock('MyClass') instanceOf MyClass);        var_dump($this->getMock('\\bar\\MyClass') instanceOf MyClass);    }   }

Produces:

/phpunit.sh --debug fiddleTestThree.php PHPUnit @package_version@ by Sebastian Bergmann.Starting test 'foo\MockingTest::testMock'..bool(false)bool(true)