In my tests, I created a test stub for a class and mock the output of the method "save" to return a default value. I have this in my setUp() method to initiate before each test runs:
// auth adapter
$this->authMock = $this->getMockBuilder('App\\Auth')
->disableOriginalConstructor()
->getMock();
// by defaut, we'll make authenticate return a FAIL result
$this->authMock
->method('authenticate')
->willReturn(0);
This is then inject into my service locator overwriting the one used by the app (the real App\Auth). However, during the actual tests, I may wish to change the output of that method
// here, we'll make authenticate return a SUCCESS result
$this->authMock
->method('authenticate')
->willReturn(1);
Anyway, once I identified the issue, for POC, I just put these one after the other and right enough it seems that PHPUnit doesn't allow me to overwrite the previously declared mocked method return value:
// auth adapter
$this->authMock = $this->getMockBuilder('App\\Auth')
->disableOriginalConstructor()
->getMock();
// by defaut, we'll make authenticate return a FAIL result
$this->authMock
->method('authenticate')
->willReturn(0);
// here, we'll make authenticate return a SUCCESS result
$this->authMock
->method('authenticate')
->willReturn(1);
var_dump($this->authMock->authenticate()); exit; // returns 0 :(
I'm sure in the past I was able to do this. Unless it was a previous version of PHPUnit. Currently I'm using 4.8.*. Any way I can do this? By default I want authenticate to return FAIL, but some tests I may want to override this with SUCCESS (so act as though the user is authenticated)