php - Can PHPUnit test stub methods not be overwritten? -
in tests, created test stub class , mock output of method "save" return default value. have in setup() method initiate before each test runs:
// auth adapter $this->authmock = $this->getmockbuilder('app\\auth') ->disableoriginalconstructor() ->getmock(); // defaut, we'll make authenticate return fail result $this->authmock ->method('authenticate') ->willreturn(0); this inject service locator overwriting 1 used app (the real app\auth). however, during actual tests, may wish change output of method
// here, we'll make authenticate return success result $this->authmock ->method('authenticate') ->willreturn(1); anyway, once identified issue, poc, put these 1 after other , right enough seems phpunit doesn't allow me overwrite declared mocked method return value:
// auth adapter $this->authmock = $this->getmockbuilder('app\\auth') ->disableoriginalconstructor() ->getmock(); // defaut, we'll make authenticate return fail result $this->authmock ->method('authenticate') ->willreturn(0); // here, we'll make authenticate return success result $this->authmock ->method('authenticate') ->willreturn(1); var_dump($this->authmock->authenticate()); exit; // returns 0 :(
i'm sure in past able this. unless previous version of phpunit. i'm using 4.8.*. way can this? default want authenticate return fail, tests may want override success (so act though user authenticated)
i can't recall ever being able mock same method twice, define helper function:
protected function mockauthresult($result = 0) {...} another option define:
protected $authresult; and in setup:
$this->authresult = 0; // fail default $this->authmock ->method('authenticate') ->willreturn($this->authresult); and can override individual tests.
Comments
Post a Comment