Sprawdzam sobie działanie mocków w PHPUnit 5.2.3. Mam pewien problem z "zagnieżdżonymi" mockami, kod wygląda tak:
interface EntityInterface
{
public function setAncestor(EntityInterface $parent);
public function getAncestor();
public function addDescendant(EntityInterface $descendant);
public function getName();
}
abstract class Entity implements EntityInterface
{
protected $name;
/** @var EntityInterface Ancestor */
protected $ancestor = null;
/** @var EntityInterface Descendants */
protected
$descendants = array();
public function __construct($name)
{
$this->name = $name;
}
public function getAncestor()
{
return $this->ancestor;
}
public function setAncestor(EntityInterface $ancestor)
{
$this->ancestor = $ancestor;
}
public function addDescendant(EntityInterface $descendant)
{
$descendant->setAncestor($this);
$this->descendants[$descendant->getName()] = $descendant;
}
public function getName()
{
return $this->name;
}
}
Test wygląda tak:
class EntityTest extends PHPUnit_Framework_TestCase
{
public function testEntityDescendantCanBeSet()
{
$entity = $this->getMockBuilder(Entity::class)
->setMethods(array('addDescendant')) ->disableOriginalConstructor()
->getMock();
$descendant_entity = $this->getMockBuilder(Entity::class)
->setMethods(array('getName', 'setAncestor')) ->disableOriginalConstructor()
->getMock();
$entity->expects($this->once())
->method('addDescendant');
$descendant_entity->expects($this->once())
->method('setAncestor');
$descendant_entity->expects($this->once())
->method('getName')
->will($this->returnValue('entity_name'));
$entity->addDescendant($descendant_entity);
}
}
Dostaję taki komunikat:
Cytat
1) EntityTest::testEntityDescendantCanBeSet
Expectation failed for method name is equal to <string:setAncestor> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
Podobny komunikat jest dla getName jeśli test setAncestor jest zakomentowany. Co jest nie tak i jak to można przetestować?