Messing with PHPUnit and Namespaces

Messing with PHPUnit and Namespaces

Preparing the latest release of the Composer authstore plugin I ran into an issue with PHPUnit which at first glance was not easy to debug. However in the end it turned out to be my own fault ;)

I was trying to mock an existing class to be able to overwrite own method and still be able to call the original methods. So I tried this:

$this->authPlugin = $this->getMock('AuthStorePlugin', array('getProjectDir'));

I was able to the call getProjectDir() method but I was not able to call any other methods. After debugging quite a while and diving deep into the PHPUnit code I was able to identify the problem: I did not pass the full qualified classname to the getMock() method. Even though the test class resides in the same namespace as the AuthStorePlugin it seems that I have to pass the full qualified classname. So I had to change the call to this:

$this->authPlugin = $this->getMock('\bitExpert\Composer\Auth\AuthStorePlugin', array('getProjectDir'));

Now everything works like a charme ;)

Eintrag von Stephan Hochdörfer am 10.04.2014

Tags: PHPUnit, Mocking, Namespace

Diese Webseite verwendet Cookies, um die Bedienfreundlichkeit zu erhöhen. Mit der Nutzung unserer Webseite wird das Einverständnis erklärt, dass wir Cookies verwenden. Weitere Informationen.