vendredi 30 octobre 2015

Tell phpunit to call public instead of private

My Question is. I have a class i want to test (myClass). In myClass there is a function wich calls a private function thats also in the myClass.

I know i can't test private functions. Is it possible to tell phpunit to call another function instead of the private?

Example

$this->testclass is now the class i want to test

$this->testclass = new \stdClass();
$this->testclass->mock = $this->getMockBuilder('myClass')->getMock();

Outside my testcase i have created a fake class.

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

in the $this->mockextended i have a public function.

the function i want to test

public function TestMe(){
$this->somePrivateFunctioniHate(); <==== this is the pain in my ass
}

and the private function

private function somePrivateFunctioniHate(){
   //come code
    }

What i want to do is. the function TestMe is calling a private function. Is it possible to tell phpunit to override the private function and call another function thats inside $this->mockextended; I have tried it like this.

$this->testclass->somePrivateFunctioniHate() = $this->mockextended->theWholeNewPrivateFunction()

only this gives me a nice error. I did this before and it worked. Called it like this

$this->testclass->chunks = new \stdClass();
//MOCKchunckTpl is the fake class outside my testcase
        $this->testclass->chunks->config = new MOCKchunkTpl("config", false, true); 

This works. Whenever a function wants to call $this->testclass->chunks->config->Somefunction(); it will be redirected to my fake class.

Aucun commentaire:

Enregistrer un commentaire