i was wondering on how i can mock a method on the same class that this class have a dependency itself.
Let's say an example:
Class SomeClassA {
protected $dependency;
function __construct(Dependency $dependency) {
$this->dependency = $dependency;
}
public function methodToTest() {
if ( !is_null( $this->methodCalled() ) )
return $this->methodCalled();
else
return "it\'s not ok!"
// need to test this method mocking the methodCalled()
}
public function methodCalled($message) {
return $message;
}
}
if this class did not have the dependency i could do something like so:
public function test_methodToTest() {
$this->getMock('SomeClassA',['methodCalled'])
->with('message')
->expects($this->once())
->will($this->returnValue('message'));
$someClassA = new SomeClassA();
$this->assertEquals('message',$someClassA->methodToTest('message'));
}
Edit:
Let's say this example to improve the meaning of my question i just changed a little bit the previous example:
Class SomeClassA {
protected $dependency;
function __construct(Dependency $dependency) {
$this->dependency = $dependency;
}
public function methodToTest() {
if ( $this->methodCalled() ) // i was wondering how mock this method
return $this->dependency->someDependencyMethod(); // not this
else
return "it\'s not ok!"
}
public function methodCalled() {
return true;
}
}
I just didn't understand how I can mock the method $this->methodClass()
. I found hard it because the class SomeClassA have a dependency. And i'm testing a method of that class.