3

I want to test a method. This method used the static method verifyPassword() in an abstract class.

class Authentication {
...
public function onUserAuthenticate($credentials) {
….
$checkedIn    = UHelper::verifyPassword($credentials);
…
}

Now I created a mock object in my test method, because I want not test the verifyPassword() with this test.

class AuthenticationTest {
..
public function testonUserAuthenticate {
...
$uhelper = $this->getMockForAbstractClass(\UHelper::class);
$uhelper->expects($this->any())
        ->method('verifyPassword')
        ->with($mypassword)
        ->will($this->returnValue(true));       
$this->class = new \Authentication();
$this->class->onUserAuthenticate($credentials);
..
}

How can I accomplish, that my mock object is used in the test method when I run $this->class->onUserAuthenticate($credentials) ?

verifyPassword is a call to a static methode in the abstract class UHelper, so I do not have a setUHelper method anywhere.

hat I'm trying to do is make variable class Authentication in ther call of the method onUserAuthenticate use the stub $uhelper. That is not answered in the related question.

astridx
  • 6,581
  • 4
  • 17
  • 35
  • Possible duplicate of [How to unit test a method with dependencies in PHP?](http://stackoverflow.com/questions/5561702/how-to-unit-test-a-method-with-dependencies-in-php) – Schleis Jan 30 '17 at 20:34
  • You can't mock static methods. – John Joseph Jan 31 '17 at 10:45
  • Thank you @JohnJoseph You are right. In the past it was possible. But currently it is not possible. There is a duplicate Question here. I found it today: http://stackoverflow.com/questions/2357001/phpunit-mock-objects-and-static-methods – astridx Jan 31 '17 at 11:57

0 Answers0