php - Mocking a method within tested class -


i using phpunit 4.6.4

i having difficulty understanding on how can mock method within tested class, had @ other examples online non seem work.

i have following function in auth class

    public function check_that_user_is_admin() {         if ($this->get_user_role() !== '1') {             // admin allowed perform action             return false;         }          return true;     } 

'get_user_role' checks active session see user level current user has , returns it's value.

this test function above

    public function testauth() {             $mock = $this->getmockbuilder('auth')->disableoriginalconstructor()->getmock(array('get_user_role'));             $mock->expects($this->once())->method('get_user_role')->with($this->returnvalue(1));              $this->asserttrue($mock->check_that_user_is_admin());     } 

everytime run test receive: 'failed asserting null true.'

i happen find answer...

    $mock = $this->getmockbuilder('auth')         ->disableoriginalconstructor()         ->setmethods(array('get_user_role'))         ->getmock();      $mock->expects($this->once())         ->method('get_user_role')         ->willreturn("2");      $this->assertfalse($mock->check_that_user_is_admin()); 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

node.js - Express and Redis - If session exists for this user, don't allow access -

excel - I can't get the attachement of the email PHP -