vendredi 30 octobre 2015

Phpunit mock private function

My question is. I have a class i want to test only thing the function i want to test is calling a private function in the class i want to test. The private function calls another class function.

I want to mock the private function so i can return value to the public functions that calls the private. I try it by creating a new class with the same function but with the value i want the function to return.

Here is my code

//function i want to test
public function checksomedata($objectID){
        $config = $this->theprivatefunction($objectID,'id');
        return $config;
    }

the private function i want to mock

private function theprivatefunction($objectID,'id'){
//do some code here. nothing special
//return the value here
}

here is my test

public function testCheckObjectAcces() {
        $objectID = '12';
      $this->testclass->checksomedata($objectID);
    }

here is my class i want to call to return some value.

public function theprivatefunction(){
        $result = "THIS VALUE NEEDS TO BE RETURNED";
        return $result;
    }

and my setUP where i try to mock the priavte function

$this->mockextended = new \stdClass();
        $this->mockextended = new MOCKEXTENDEDCLASSES();
        $this->testclass->theprivatefunction() = $this->mockextended->theprivatefunction();

In my setUp i want the code to think $this->testclass->theprivatefunction() is $this->mockextended->theprivatefunction();

So when a function is calling the private function it needs to be redirected to $this->mockextended->theprivatefunction();

Aucun commentaire:

Enregistrer un commentaire