P
P
pvshnik2013-12-16 10:28:17
Unit testing
pvshnik, 2013-12-16 10:28:17

PHPUnit. Mock objects. Stub for another class method?

Situation:
Yii project, PHPUnit tests.
Model:

class Class1 {
 static function func1(){
  if(Class2::funcOfClass2)
        .....
 }
....
}

Test:
class Class1Test extends PHPUnit_Framework_TestCase {
  public function testFunc1(){
    $this->assertEquals(Class1::func1());
 }

Is it possible to stub Class2::funcOfClass2 (so that testFunc1 returns true instead)?
PS Tried
$mock = $this->getMock('Class1', array('Class2::funcOfClass2'));
$mock->expects($this->once()->method('Class2::funcOfClass2')->will($this->returnValue(true)));

but it didn't work out that way. I don't know if this is correct...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vit, 2013-12-16
@fornit1917

You can access Class2 not explicitly, but through DI (Yii::app()->class2->funcOfClass2()). Then in the config for tests as a component of Class2 it will be enough to specify another class and that's it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question